//Activity主頁面佈局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"> <RelativeLayout android:id="@+id/topRea" android:layout_width="match_parent" android:layout_height="40dp" android:background="#4A90E2"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="购物车" /> RelativeLayout> <LinearLayout android:id="@+id/bottemLin" android:layout_width="match_parent" android:layout_height="60dp" android:layout_alignParentBottom="true" android:orientation="horizontal"> <RelativeLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1"> <CheckBox android:id="@+id/allcheck" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="10dp" android:text="全选" android:textColor="#000000" /> <TextView android:id="@+id/heji" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="20dp" android:layout_toRightOf="@+id/allcheck" android:text="合计" android:textColor="#000000" /> <TextView android:id="@+id/hejivalue" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="10dp" android:layout_toRightOf="@+id/heji" android:textColor="#000000" android:textStyle="bold" /> RelativeLayout> <RelativeLayout android:id="@+id/jiesuanRea" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="0.8" android:background="#4A90E2"> <TextView android:id="@+id/jiesuan" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:layout_centerVertical="true" android:layout_marginLeft="10dp" android:text="结算" android:textColor="#ffffff" /> <TextView android:id="@+id/zuo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_toRightOf="@+id/jiesuan" android:text="(" android:textColor="#ffffff" android:textStyle="bold" /> <TextView android:id="@+id/jiesuanvalue" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_toRightOf="@+id/zuo" android:text="0" android:textColor="#ffffff" android:textStyle="bold" /> <TextView android:id="@+id/you" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_toRightOf="@+id/jiesuanvalue" android:text=")" android:textColor="#ffffff" android:textStyle="bold" /> RelativeLayout> LinearLayout> <LinearLayout android:id="@+id/center" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@+id/bottemLin" android:layout_below="@+id/topRea" android:orientation="vertical"> <ListView android:id="@+id/MyList" android:layout_width="match_parent" android:layout_height="match_parent">ListView> LinearLayout> <ImageView android:layout_width="match_parent" android:layout_height="1dp" android:layout_below="@+id/center" android:background="#9c9c9c" /> RelativeLayout>
//item佈局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <RelativeLayout android:layout_width="match_parent" android:layout_height="100dp"> <CheckBox android:id="@+id/checkone" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_centerVertical="true"/> <RelativeLayout android:layout_toRightOf="@+id/checkone" android:layout_width="match_parent" android:layout_height="wrap_content"> <ImageView android:background="@mipmap/ic_launcher" android:id="@+id/img" android:layout_margin="10dp" android:layout_width="80dp" android:layout_height="80dp" /> <TextView android:id="@+id/good_name" android:layout_toRightOf="@+id/img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="15dp" android:textColor="#000000" android:singleLine="true" android:textStyle="bold" android:textSize="16sp" /> <TextView android:id="@+id/good_fname" android:layout_width="wrap_content" android:layout_marginTop="2dp" android:layout_toRightOf="@+id/img" android:layout_below="@+id/good_name" android:layout_height="wrap_content" /> <RelativeLayout android:layout_width="wrap_content" android:layout_toRightOf="@+id/img" android:layout_marginTop="4dp" android:layout_below="@+id/good_fname" android:layout_height="wrap_content"> <TextView android:id="@+id/good_money" android:layout_centerVertical="true" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <LinearLayout android:layout_centerVertical="true" android:layout_width="wrap_content" android:layout_marginRight="10dp" android:orientation="horizontal" android:layout_alignParentRight="true" android:layout_height="wrap_content"> <Button android:id="@+id/jian" android:layout_width="30dp" android:background="#f2f1f1" android:text="--" android:layout_weight="1" android:layout_height="wrap_content" /> <TextView android:id="@+id/nu" android:layout_width="30dp" android:layout_height="match_parent" android:text="1" android:layout_weight="1" android:gravity="center" android:textColor="#000000" /> <Button android:id="@+id/jia" android:layout_weight="1" android:layout_width="30dp" android:background="#f2f1f1" android:text="+" android:layout_height="wrap_content" /> LinearLayout> RelativeLayout> RelativeLayout> RelativeLayout> LinearLayout>
//Activity代碼
package com.bobcfc.shopdemo; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.CheckBox; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.RelativeLayout; import android.widget.TextView; import java.util.ArrayList; import java.util.List; public class CarActivity extends Activity implements CarAdapter.CheckInterface,CarAdapter.ModifyCountInterface{ private ListView MyList; private CheckBox allcheck; private TextView hejivalue,jiesuanvalue; private Listlist; public static LinearLayout bottemLin; private CarAdapter adapter; private double totalPrice = 0.00;// 购买的商品总价 private int totalCount = 0;// 购买的商品总数量 private RelativeLayout jiesuanRea; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_car); initView(); initList(); c(); } private void initList() { list = new ArrayList<>(); for(int i =0;i<10;i++){ CarInfo info = new CarInfo(); info.setSpnum("1"); info.setSpprice("22"); info.setSptype("nicka"); info.setSpname("似耐克"); list.add(info); } adapter = new CarAdapter(this,list); MyList.setAdapter(adapter); } private void initView() { jiesuanRea = (RelativeLayout) findViewById(R.id.jiesuanRea); bottemLin = (LinearLayout)findViewById(R.id.bottemLin); MyList = (ListView) findViewById(R.id.MyList); allcheck = (CheckBox) findViewById(R.id.allcheck); hejivalue = (TextView)findViewById(R.id.hejivalue); jiesuanvalue = (TextView)findViewById(R.id.jiesuanvalue); allcheck.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { doCheckAll(); } }); jiesuanRea.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { } }); } private void c(){ adapter.setCheckInterface(this);// 关键步骤1,设置复选框接口 adapter.setModifyCountInterface(this);// 关键步骤2,设置数量增减接口 } /** 全选与反选 */ private void doCheckAll() { for (int i = 0; i < list.size(); i++) { list.get(i).setChoosed(allcheck.isChecked()); } adapter.notifyDataSetChanged(); calculate(); } @Override protected void onDestroy() { super.onDestroy(); jiesuanvalue = null; hejivalue = null; allcheck = null; } private boolean isAllCheck() { for (CarInfo info : list) { if (!info.isChoosed()) return false; } return true; } private void calculate() { totalCount = 0; totalPrice = 0.00; for (int j = 0; j < list.size(); j++) { CarInfo product = list.get(j); if (product.isChoosed()) { totalCount++; totalPrice += Double.parseDouble(product.getSpprice()) * Double.parseDouble(product.getSpnum()); } } hejivalue.setText("¥" + totalPrice); jiesuanvalue.setText("" + totalCount); } @Override public void checkChild( int childPosition, boolean isChecked) { if (isAllCheck()) allcheck.setChecked(true); else allcheck.setChecked(false); adapter.notifyDataSetChanged(); calculate(); } @Override public void doIncrease(int childPosition, View showCountView, boolean isChecked) { CarInfo info = (CarInfo) adapter.getItem(childPosition); int currentCount = Integer.parseInt(info.getSpnum()); currentCount++; info.setSpnum(String.valueOf(currentCount)); ((TextView) showCountView).setText(currentCount + ""); adapter.notifyDataSetChanged(); calculate(); } @Override public void doDecrease( int childPosition, View showCountView, boolean isChecked) { CarInfo info = (CarInfo) adapter.getItem(childPosition); int currentCount = Integer.parseInt(info.getSpnum()); if (currentCount == 1) return; currentCount--; info.setSpnum(String.valueOf(currentCount)); ((TextView) showCountView).setText(currentCount + ""); adapter.notifyDataSetChanged(); calculate(); } }
//適配器代碼
package com.bobcfc.shopdemo; import android.app.Dialog; import android.content.Context; import android.os.Handler; import android.os.Message; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.Button; import android.widget.CheckBox; import android.widget.ImageView; import android.widget.RelativeLayout; import android.widget.TextView; import android.widget.Toast; import org.json.JSONException; import org.json.JSONObject; import java.util.List; /** * Created by Android on 2017/5/15. */ public class CarAdapter extends BaseAdapter { private Context context; private Listlist; private CheckInterface checkInterface; private ModifyCountInterface modifyCountInterface; // private ImageLoader imageLoader; //private RequestQueue mQueue; // private com.android.volley.toolbox.ImageLoader mImageLoader; //商品id private int carid; //商品数量 private String num; public CarAdapter(Context context, List list){ this.context = context; this.list = list; // imageLoader = ImageLoader.getInstance(); // mQueue = Volley.newRequestQueue(context); // mImageLoader = new com.android.volley.toolbox.ImageLoader(mQueue, new BitmapCache());// } public void setCheckInterface(CheckInterface checkInterface) { this.checkInterface = checkInterface; } public void setModifyCountInterface(ModifyCountInterface modifyCountInterface) { this.modifyCountInterface = modifyCountInterface; } @Override public int getCount() { return list.size(); } @Override public Object getItem(int position) { return list.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(final int position, View convertView, ViewGroup parent) { final ViewHolder holder; if(convertView == null){ convertView = View.inflate(context, R.layout.car_item, null); holder = new ViewHolder(); holder.good_name = (TextView) convertView.findViewById(R.id.good_name); holder.good_fname = (TextView) convertView.findViewById(R.id.good_fname); holder.img = (ImageView) convertView.findViewById(R.id.img); holder.good_money = (TextView) convertView.findViewById(R.id.good_money); holder.nu = (TextView) convertView.findViewById(R.id.nu); holder.checkone = (CheckBox) convertView.findViewById(R.id.checkone); holder.jia = (Button) convertView.findViewById(R.id.jia); holder.jian = (Button) convertView.findViewById(R.id.jian); convertView.setTag(holder); }else{ holder = (ViewHolder) convertView.getTag(); } final CarInfo info = list.get(position); if(info != null){ holder.checkone.setChecked(info.isChoosed()); holder.checkone.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { info.setChoosed(((CheckBox) v).isChecked()); holder.checkone.setChecked(((CheckBox) v).isChecked()); checkInterface.checkChild(position, ((CheckBox) v).isChecked());// 暴露子选接口 } }); holder.good_name.setText(list.get(position).getSpname()); holder.good_fname.setText("型号: "+info.getSptype()); holder.good_money.setText("¥ "+list.get(position).getSpprice()); holder.nu.setText(list.get(position).getSpnum()); holder.jia.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { modifyCountInterface.doIncrease(position, holder.nu, holder.checkone.isChecked());// 暴露增加接口 } }); holder.jian.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { modifyCountInterface.doDecrease(position, holder.nu, holder.checkone.isChecked());// 暴露删减接口 } }); } return convertView; } class ViewHolder{ ImageView img; Button jia,jian; TextView good_name,good_fname,good_money,nu; CheckBox checkone; } /** * 复选框接口 */ public interface CheckInterface { // /** // * 组选框状态改变触发的事件 // * // * @param groupPosition 组元素位置 // * @param isChecked 组元素选中与否 // */ // public void checkGroup(int groupPosition, boolean isChecked); /** * 子选框状态改变时触发的事件 * * @param childPosition 子元素位置 * @param isChecked 子元素选中与否 */ public void checkChild(int childPosition, boolean isChecked); } /** * 改变数量的接口 */ public interface ModifyCountInterface { /** * 增加操作 * * @param childPosition 子元素位置 * @param showCountView 用于展示变化后数量的View * @param isChecked 子元素选中与否 */ public void doIncrease(int childPosition, View showCountView, boolean isChecked); /** * 删减操作 * * @param childPosition 子元素位置 * @param showCountView 用于展示变化后数量的View * @param isChecked 子元素选中与否 */ public void doDecrease(int childPosition, View showCountView, boolean isChecked); } }
//兩個bean實體類
package com.bobcfc.shopdemo; public class BaseInfo { protected String Id; protected String name; protected boolean isChoosed; public BaseInfo() { super(); } public BaseInfo(String id, String name) { super(); Id = id; this.name = name; } public String getId() { return Id; } public void setId(String id) { Id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public boolean isChoosed() { return isChoosed; } public void setChoosed(boolean isChoosed) { this.isChoosed = isChoosed; } }//還是bean類
package com.bobcfc.shopdemo; import java.io.Serializable; /** * Created by Android on 2017/5/25. */ public class CarInfo extends BaseInfo implements Serializable { private String carid; private String spsn; private String spname; private String sptype; private String spprice; private String spnum; private String spimg; public String getCarid() { return carid; } public void setCarid(String carid) { this.carid = carid; } public String getSpsn() { return spsn; } public void setSpsn(String spsn) { this.spsn = spsn; } public String getSpname() { return spname; } public void setSpname(String spname) { this.spname = spname; } public String getSptype() { return sptype; } public void setSptype(String sptype) { this.sptype = sptype; } public String getSpprice() { return spprice; } public void setSpprice(String spprice) { this.spprice = spprice; } public String getSpnum() { return spnum; } public void setSpnum(String spnum) { this.spnum = spnum; } public String getSpimg() { return spimg; } public void setSpimg(String spimg) { this.spimg = spimg; } }