MyActivity===========================代码如下
package com.example.ListViewBoxDemo; import android.app.Activity;import android.content.Context;import android.os.Bundle;import android.view.View;import android.widget.CheckBox;import android.widget.CompoundButton;import android.widget.ListView; import java.util.ArrayList; /** * * 模仿淘宝购物车中的ListView 中嵌套CheckBox * 实现每个店铺全选 已经不选中的功能 *实现原理 在bean中添加一个标记 通过CheckBox的改变标记来变化bean中的标记 * 具体不明白的加Q 513599210 */public class MyActivity extends Activity { /** * Called when the activity is first created. */ ArrayList<ArrayList<ShopBean>> arrayList = new ArrayList<>(); // 代表所有的加入购物车中的店铺 ArrayList<ShopBean> arrayListone = new ArrayList<>(); //代表每个店铺 ArrayList<ShopBean> arrayListtwo = new ArrayList<>(); ArrayList<ShopBean> arrayListfow = new ArrayList<>(); ListView shopping_listview; Context context; CheckBox all_chekbox; ItemAdapter itemAdapter; int size; // 统计有多少个商品 int oksize; // 统计bean中的是否选中 boolean boxoutstate =true;// 表示CheckBox是外部的点击还是内部的方法 默认执行外部全选方法 boolean boxoutclickstate =true;// 是内部的item CheckBox 选中或者取消传递出来的参数接收 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); this.context = this; shopping_listview = (ListView) findViewById(R.id.shopping_listview); all_chekbox = (CheckBox) findViewById(R.id.all_chekbox); arrayListone.add(new ShopBean()); arrayListone.add(new ShopBean()); arrayListone.add(new ShopBean()); arrayListtwo.add(new ShopBean()); arrayListtwo.add(new ShopBean()); arrayListtwo.add(new ShopBean()); arrayListfow.add(new ShopBean()); arrayListfow.add(new ShopBean()); arrayListfow.add(new ShopBean()); arrayList.add(arrayListone); arrayList.add(arrayListtwo); arrayList.add(arrayListfow); itemAdapter = new ItemAdapter(this, arrayList, new ItemAdapter.Iparentbox() { @Override public void clickbox(boolean boxstate) { // 选中店铺中的CheckBox回调 boxoutclickstate=boxstate;// all_chekbox.setChecked(boxstate); size = 0; oksize = 0; for (ArrayList<ShopBean> arrbean : arrayList) { for (ShopBean bean : arrbean) { size++; // 统计有 多少个元素 } } for (ArrayList<ShopBean> arrbean : arrayList) { for (ShopBean bean : arrbean) { if (bean.parentbox) { oksize++;// 统计元素中有多少个是已经选中 } } } System.out.println("====saze===" + size + "===oksaze====" + oksize); // 如何bean中的表示选中 与 arrayList相等设置外部box全选 if (size == oksize) { all_chekbox.setChecked(true); } else { // 不成立设置box不是全选 同时改变 boxoutstate 标记 为外内部box执行方法 boxoutstate = false; all_chekbox.setChecked(false); } } }, R.layout.item_shop); nextAdate(); all_chekbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { System.out.println("boxoutstate==="+boxoutstate); if (isChecked) { for (ArrayList<ShopBean> arrbean : arrayList) { for (ShopBean bean : arrbean) { bean.parentbox = isChecked; } } itemAdapter.setBoxState(isChecked); } else { // 这里是执行了内部itembox 的执行 if((boxoutstate==false)&&(boxoutclickstate==false)){ System.out.println("内部的box点击"); }else { for (ArrayList<ShopBean> arrbean : arrayList) { for (ShopBean bean : arrbean) { bean.parentbox = isChecked; } } itemAdapter.setBoxState(isChecked); } } } }); } // 更新数据 private void nextAdate() { shopping_listview.setAdapter(itemAdapter); }} -------------------------------------------------------------------------------------------------------------------------------------------item的代码 ===========================================================================================================================================
package com.example.ListViewBoxDemo; import android.content.Context;import android.view.View;import android.widget.CheckBox;import android.widget.CompoundButton;import android.widget.LinearLayout; import java.util.ArrayList;import java.util.List; /** * Created by ADMIN on 2015/12/16. */public class ItemAdapter extends CommonAdapter<ArrayList<ShopBean>> { Context contextData; List<ArrayList<ShopBean>> datas; Iparentbox iparentbox; // 内部box 的选中 取消接口回调 public ItemAdapter(Context context, List<ArrayList<ShopBean>> datas, Iparentbox iparentbox, int layoutId) { super(context, datas, layoutId); this.contextData = context; this.datas = datas; this.iparentbox = iparentbox; } public interface Iparentbox { public void clickbox(boolean boxstate); } @Override public void convert(ViewHolder holder, ArrayList<ShopBean> shopBeans) { CheckBox determine_chekbox = holder.getView(R.id.determine_chekbox); // 这是判断是否 int h = 0; boolean statechildbox = false; for (ShopBean shoppingCartBean : datas.get(holder.getPosition())) { if (shoppingCartBean.parentbox) { h++; } } System.out.println("====h===" + h); // 判断加入购物车中的每个店铺的多个商品是否全选 全选 店铺的box 也选中 if (datas.get(holder.getPosition()).size() == h) { determine_chekbox.setChecked(true); System.out.println("执行过来了"); } else if (h == 0) { System.out.println("执行过来了==="); determine_chekbox.setChecked(false); } else { } // 店铺 Box选中或者未选中时候刷新 里面的商品 determine_chekbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { ArrayList<ShopBean> shoppingCartBeans1 = datas.get(holder.getPosition()); if (isChecked) { for (ShopBean databean : shoppingCartBeans1) { databean.parentbox = isChecked; } notifyDataSetChanged(); iparentbox.clickbox(isChecked); } else { for (ShopBean databean : shoppingCartBeans1) { databean.parentbox = isChecked; } notifyDataSetChanged(); iparentbox.clickbox(isChecked); } } }); LinearLayout add_shoping_item = holder.getView(R.id.add_shoping_item); add_shoping_item.removeAllViews(); for (int i = 0; i < shopBeans.size(); i++) { View inflate = View.inflate(contextData, R.layout.add_item_shoppingcart, null); CheckBox checkBox = (CheckBox) inflate.findViewById(R.id.add_chekbox); checkBox.setChecked(shopBeans.get(i).parentbox); add_shoping_item.addView(inflate); final int finalI = i; checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (isChecked) { shopBeans.get(finalI).parentbox = isChecked; System.out.println("子控件选中执行过来"); notifyDataSetChanged(); iparentbox.clickbox(isChecked); } else { shopBeans.get(finalI).parentbox = isChecked; iparentbox.clickbox(isChecked); notifyDataSetChanged(); } } }); } } public void setBoxState(boolean state) { System.out.println("state===" + state); if (state) { notifyDataSetChanged(); } else { notifyDataSetChanged(); } } } ===============================================================
源码 http://download.csdn.net/detail/qq_15696769/9364407
时间: 2024-10-05 20:29:09