不说话,先上图:
项目中经常需要实现这样的逻辑:
点击全选按钮,实时显示物料种类数和总数;
点击加减按钮,总数实时增减;
再次点击全选按钮,全部不选。
主要的难点在于接口回调的使用,上代码:
activity代码:
package com.example.michael.cartdemo.activity; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.CheckBox; import android.widget.EditText; import android.widget.ListView; import android.widget.TextView; import com.example.michael.cartdemo.adapter.CartAdapter; import com.example.michael.cartdemo.R; import com.example.michael.cartdemo.bean.ItemBean; import com.example.michael.cartdemo.interf.CheckInterface; import com.example.michael.cartdemo.interf.ModifyCountInterface; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity implements View.OnClickListener,CheckInterface,ModifyCountInterface{ private ListView lvCart; private CheckBox cbChooseAll; private TextView tvMaterialNum; private TextView tvAllNum; private int totalCount; private int totalNum; private List<ItemBean> infos = new ArrayList<>(); private CartAdapter cartAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); } private void init() { initView(); initData(); initAdapter(); initListener(); } private void initListener() { cbChooseAll.setOnClickListener(this); } private void initAdapter() { cartAdapter = new CartAdapter(infos); cartAdapter.setCheckInterface(this); cartAdapter.setModifyCountInterface(this); lvCart.setAdapter(cartAdapter); } private void initData() { for (int i = 0; i < 3; i++) { ItemBean itemBean = new ItemBean(); itemBean.setInvName("家电家具大数据跨境电商大数据技术监督局环境多说几句方式见附件1电视电话环境"); itemBean.setInvCode("1999266555"); itemBean.setNum(9); itemBean.setSpecification("(4*25)"); infos.add(itemBean); } } private void initView() { lvCart = (ListView) findViewById(R.id.lv_cart); cbChooseAll = (CheckBox) findViewById(R.id.cb_choose_all); tvMaterialNum = (TextView) findViewById(R.id.tv_material_num); tvAllNum = (TextView) findViewById(R.id.tv_all_num); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.cb_choose_all: if(infos.size() != 0){ if(cbChooseAll.isChecked()){ for (int i = 0; i < infos.size(); i++) { infos.get(i).setChecked(true); } cartAdapter.notifyDataSetChanged(); }else{ for (int i = 0; i < infos.size(); i++) { infos.get(i).setChecked(false); } cartAdapter.notifyDataSetChanged(); } } statistics(); break; } } private void statistics() { totalCount = 0; totalNum = 0; for (int i = 0; i < infos.size(); i++) { ItemBean itemBean = infos.get(i); if(itemBean.getChecked()){ totalNum++; totalCount += itemBean.getNum(); } } tvMaterialNum.setText(totalNum + ""); tvAllNum.setText(totalCount + ""); } @Override public void checkGroup(int position, boolean isChecked) { infos.get(position).setChecked(isChecked); if(isAllCheck()){ cbChooseAll.setChecked(true); }else{ cbChooseAll.setChecked(false); } cartAdapter.notifyDataSetChanged(); statistics(); } @Override public void doIncrease(int position, View showCountView, boolean isChecked) { ItemBean itemBean = infos.get(position); int currentCount = itemBean.getNum(); currentCount++; itemBean.setNum(currentCount); ((EditText)showCountView).setText(currentCount + ""); cartAdapter.notifyDataSetChanged(); statistics(); } @Override public void doDecrease(int position, View showCountView, boolean isChecked) { ItemBean itemBean = infos.get(position); int currentCount = itemBean.getNum(); if(currentCount == 1){ return; } currentCount--; itemBean.setNum(currentCount); ((EditText)showCountView).setText(currentCount + ""); cartAdapter.notifyDataSetChanged(); statistics(); } public boolean isAllCheck() { for (int i = 0; i < infos.size(); i++) { if(!infos.get(i).getChecked()){ return false; } } return true; } }
adapter代码:
package com.example.michael.cartdemo.adapter; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.CheckBox; import android.widget.EditText; import android.widget.TextView; import com.example.michael.cartdemo.R; import com.example.michael.cartdemo.bean.ItemBean; import com.example.michael.cartdemo.interf.CheckInterface; import com.example.michael.cartdemo.interf.ModifyCountInterface; import java.util.List; /** * Created by Michael on 2017/6/1. */ public class CartAdapter extends BaseAdapter { private CheckInterface checkInterface; private ModifyCountInterface modifyCountInterface; public void setCheckInterface(CheckInterface checkInterface) { this.checkInterface = checkInterface; } public void setModifyCountInterface(ModifyCountInterface modifyCountInterface) { this.modifyCountInterface = modifyCountInterface; } private List<ItemBean> mList; public CartAdapter(List<ItemBean> list) { mList = list; } @Override public int getCount() { return mList.size(); } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } @Override public View getView(final int position, View convertView, ViewGroup parent) { final ViewHolder holder; if(convertView == null){ convertView = LayoutInflater .from(parent.getContext()) .inflate(R.layout.item_cart_adapter,null); holder = new ViewHolder(); holder.tvInvName = (TextView) convertView.findViewById(R.id.tv_inv_name); holder.tvInvCode = (TextView) convertView.findViewById(R.id.tv_inv_code); holder.tvSpecification = (TextView) convertView.findViewById(R.id.tv_specification); holder.etNum = (EditText) convertView.findViewById(R.id.et_num); holder.cbItemCheck = (CheckBox) convertView.findViewById(R.id.cb_item_check); holder.etMinus = (EditText) convertView.findViewById(R.id.et_minus); holder.etAdd = (EditText) convertView.findViewById(R.id.et_add); convertView.setTag(holder); }else{ holder = (ViewHolder) convertView.getTag(); } holder.etNum.setText(mList.get(position).getNum() + ""); holder.cbItemCheck.setChecked(mList.get(position).getChecked()); holder.cbItemCheck.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mList.get(position).setChecked(((CheckBox)v).isChecked()); checkInterface.checkGroup(position,((CheckBox)v).isChecked()); } }); holder.etMinus.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { modifyCountInterface .doDecrease(position,holder.etNum,holder.cbItemCheck.isChecked()); } }); holder.etAdd.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { modifyCountInterface .doIncrease(position,holder.etNum,holder.cbItemCheck.isChecked()); } }); return convertView; } class ViewHolder { public TextView tvInvName; public TextView tvInvCode; public TextView tvSpecification; public EditText etNum; public CheckBox cbItemCheck; public EditText etMinus; public EditText etAdd; } }
bean代码:
package com.example.michael.cartdemo.bean; /** * Created by Michael on 2017/6/25. */ public class ItemBean { private String invName; private String invCode; private String specification; private int num; private boolean isChecked; public void setInvName(String invName){ this.invName = invName; } public String getInvName(){ return invName; } public void setInvCode(String invCode){ this.invCode = invCode; } public String getInvCode(){ return invCode; } public void setSpecification(String specification){ this.specification = specification; } public String getSpecification(){ return specification; } public void setNum(int num){ this.num = num; } public int getNum(){ return num; } public void setChecked(boolean isChecked){ this.isChecked = isChecked; } public boolean getChecked(){ return isChecked; } }
接口定义:
package com.example.michael.cartdemo.interf; /** * Created by Michael on 2017/6/25. */ public interface CheckInterface { void checkGroup(int position, boolean isChecked); }
package com.example.michael.cartdemo.interf; import android.view.View; /** * Created by Michael on 2017/6/25. */ public interface ModifyCountInterface { void doIncrease(int position, View showCountView, boolean isChecked); void doDecrease(int position, View showCountView, boolean isChecked); }
布局代码:
<?xml version="1.0" encoding="utf-8"?> <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="@dimen/default_margin_50dp" android:background="@color/white" > <!--<View android:layout_marginLeft="@dimen/default_margin_45dp" android:layout_width="@dimen/default_margin_1.5dp" android:layout_height="@dimen/default_margin_30dp" android:layout_centerVertical="true" android:background="@color/color_cccccc" > </View>--> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="购物车" android:textSize="@dimen/default_text_size_25sp" android:textStyle="bold" /> <View android:layout_marginRight="@dimen/default_margin_50dp" android:layout_alignParentRight="true" android:layout_width="@dimen/default_margin_1.5dp" android:layout_height="@dimen/default_margin_30dp" android:layout_centerVertical="true" android:background="@color/eeeeee" > </View> <ImageButton android:id="@+id/ib_del" android:layout_alignParentRight="true" android:layout_width="@dimen/default_margin_50dp" android:layout_height="match_parent" android:src="@mipmap/delete_menu" android:scaleType="center" android:onClick="onPopupClick" android:background="@null" /> </RelativeLayout> <View android:layout_width="match_parent" android:layout_height="@dimen/default_margin_1dp" android:background="@color/color_333333" > </View> <ListView android:id="@+id/lv_cart" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" > </ListView> <RelativeLayout android:layout_width="match_parent" android:layout_height="0.8dp" android:background="#00000000" > </RelativeLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="0.8dp" android:background="#03000000" > </RelativeLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="0.8dp" android:background="#05000000" > </RelativeLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="0.8dp" android:background="#08000000" > </RelativeLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="0.8dp" android:background="#0A000000" > </RelativeLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="0.8dp" android:background="#0D000000" > </RelativeLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="0.8dp" android:background="#0F000000" > </RelativeLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="0.8dp" android:background="#12000000" > </RelativeLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="0.8dp" android:background="#14000000" > </RelativeLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="0.8dp" android:background="#17000000" > </RelativeLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="0.8dp" android:background="#1A000000" > </RelativeLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="@dimen/default_margin_60dp"> <RelativeLayout android:layout_width="@dimen/default_margin_50dp" android:layout_height="match_parent"> <CheckBox android:id="@+id/cb_choose_all" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" /> </RelativeLayout> <RelativeLayout android:layout_marginLeft="45dp" android:layout_width="@dimen/default_margin_40dp" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="全选" /> </RelativeLayout> <View android:layout_marginLeft="91dp" android:layout_width="@dimen/default_margin_1dp" android:layout_height="@dimen/default_margin_40dp" android:layout_centerVertical="true" android:background="@color/eeeeee" > </View> <RelativeLayout android:layout_marginLeft="91dp" android:layout_width="@dimen/default_margin_50dp" android:layout_height="match_parent"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" android:layout_centerInParent="true" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="物料" /> <TextView android:id="@+id/tv_material_num" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="0" android:textColor="#D90000" android:layout_gravity="center_horizontal" /> </LinearLayout> </RelativeLayout> <View android:layout_marginLeft="141dp" android:layout_width="@dimen/default_margin_1dp" android:layout_height="@dimen/default_margin_40dp" android:background="@color/eeeeee" android:layout_centerVertical="true" > </View> <RelativeLayout android:layout_marginLeft="141dp" android:layout_width="@dimen/default_margin_65dp" android:layout_height="match_parent"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="数量" android:layout_gravity="center_horizontal" /> <TextView android:id="@+id/tv_all_num" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="0" android:textColor="#D90000" android:layout_gravity="center_horizontal" /> </LinearLayout> </RelativeLayout> <RelativeLayout android:id="@+id/rl_balance" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginRight="@dimen/default_margin_10dp" android:clickable="true" android:focusable="true" android:layout_width="150dp" android:layout_height="@dimen/default_margin_40dp" android:background="@drawable/shape_sure" > <RelativeLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_centerInParent="true" > <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/ok" android:layout_centerVertical="true" /> <TextView android:layout_marginLeft="@dimen/default_margin_25dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="结算" android:textSize="@dimen/default_text_size_18sp" android:layout_centerVertical="true" android:textColor="@color/white" /> </RelativeLayout> </RelativeLayout> </RelativeLayout> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:weightSum="12" > <RelativeLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="2" > <CheckBox android:id="@+id/cb_item_check" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" /> </RelativeLayout> <View android:layout_width="@dimen/default_margin_1dp" android:layout_height="match_parent" android:background="@color/eeeeee" > </View> <LinearLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="10" android:orientation="vertical" > <RelativeLayout android:layout_width="match_parent" android:layout_height="@dimen/default_margin_45dp" android:padding="@dimen/default_margin_5dp" > <TextView android:id="@+id/tv_inv_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerInParent="true" android:ellipsize="end" android:lines="2" android:singleLine="false" android:text="户都是大家都是科技第三季度技术监督局苦上加苦待机时间副教授积分开始的解放军家具家电司法局是打发时间" /> </RelativeLayout> <View android:layout_width="match_parent" android:layout_height="@dimen/default_margin_1dp" android:background="@color/eeeeee" > </View> <RelativeLayout android:layout_width="match_parent" android:layout_height="120dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:weightSum="3" > <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:orientation="horizontal" android:weightSum="10" > <RelativeLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="编" android:layout_centerInParent="true" /> </RelativeLayout> <View android:layout_width="@dimen/default_margin_1dp" android:layout_height="match_parent" android:background="@color/eeeeee" > </View> <RelativeLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="9" > <TextView android:id="@+id/tv_inv_code" android:layout_marginLeft="@dimen/default_margin_5dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="17992" android:layout_centerVertical="true" /> </RelativeLayout> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="@dimen/default_margin_1dp" android:background="@color/eeeeee" > </View> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:orientation="horizontal" android:weightSum="10" > <RelativeLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="规" android:layout_centerInParent="true" /> </RelativeLayout> <View android:layout_width="@dimen/default_margin_1dp" android:layout_height="match_parent" android:background="@color/eeeeee" > </View> <RelativeLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="9" > <TextView android:id="@+id/tv_specification" android:layout_marginLeft="@dimen/default_margin_5dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="(4*25)" android:layout_centerVertical="true" /> </RelativeLayout> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="@dimen/default_margin_1dp" android:background="@color/eeeeee" > </View> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:orientation="horizontal" android:weightSum="10" > <RelativeLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="数" android:layout_centerInParent="true" /> </RelativeLayout> <View android:layout_width="@dimen/default_margin_1dp" android:layout_height="match_parent" android:background="@color/eeeeee" > </View> <RelativeLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="9" > <LinearLayout android:layout_marginLeft="@dimen/default_margin_5dp" android:layout_width="wrap_content" android:layout_height="@dimen/default_margin_30dp" android:layout_centerVertical="true" android:orientation="horizontal" > <EditText android:id="@+id/et_minus" android:clickable="true" android:focusable="false" android:layout_width="@dimen/default_margin_30dp" android:layout_height="match_parent" android:editable="false" android:gravity="center" android:text="-" android:background="@drawable/shape_set" /> <EditText android:id="@+id/et_num" android:layout_width="@dimen/default_margin_45dp" android:layout_height="match_parent" android:background="@drawable/shape_set" android:text="145" android:textColor="#5394C9" android:gravity="center" /> <EditText android:id="@+id/et_add" android:clickable="true" android:focusable="false" android:layout_width="@dimen/default_margin_30dp" android:layout_height="match_parent" android:editable="false" android:gravity="center" android:text="+" android:background="@drawable/shape_set" /> </LinearLayout> </RelativeLayout> </LinearLayout> </LinearLayout> </RelativeLayout> </LinearLayout> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" > <solid android:color="@android:color/transparent" /> <stroke android:width="@dimen/default_margin_0.5dp" android:color="@color/plugin_camera_black" > </stroke> </shape>
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > <corners android:radius="@dimen/default_margin_4dp"> </corners> <solid android:color="#5394C9"> </solid> </shape>