二级列表和全选
2017-10-25 07:37
363 查看
compile 'org.greenrobot:eventbus:3.0.0'
主布局代码
package com.example.cll.erjigouwuche; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.CheckBox; import android.widget.ExpandableListView; import android.widget.TextView; import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.Subscribe; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity { private ExpandableListView mElv; /** * 全选 */ private CheckBox mCbAll; /** * 合计: */ private TextView mTvTotal; private List<Groupbean> groupList = new ArrayList<>(); private List<List<Childbean>> childList = new ArrayList<>(); private MyAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); EventBus.getDefault().register(this); initView(); //给设置ExpandableListView设置数据 //模拟数据 initData(); adapter = new MyAdapter(this, groupList, childList); mElv.setGroupIndicator(null); mElv.setAdapter(adapter); //全部展开 for (int i = 0; i < groupList.size(); i++) { mElv.expandGroup(i); } //给全选设置点击事件 mCbAll.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { adapter.allChecked(mCbAll.isChecked()); } }); } @Override protected void onDestroy() { super.onDestroy(); EventBus.getDefault().unregister(this); } @Subscribe public void messageCountEvent(MessageCounEvent msg) { mTvTotal.setText("总计:" + msg.getCount() + "个"); } @Subscribe public void messageEvent(MessageEvent msg) { mCbAll.setChecked(msg.isFlag()); } private void initData() { for (int i = 0; i < 3; i++) { Groupbean groupBean = new Groupbean(false,"商家" + i); groupList.add(groupBean); List<Childbean> list = new ArrayList<>(); for (int j = 0; j < 2; j++) { Childbean childBean = new Childbean(false,"商品" + j ); list.add(childBean); } childList.add(list); } } private void initView() { mElv = (ExpandableListView) findViewById(R.id.elv); mCbAll = (CheckBox) findViewById(R.id.cbAll); mTvTotal = (TextView) findViewB 4000 yId(R.id.tvTotal); } } 主布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.cll.erjigouwuche.MainActivity"> <TextView android:layout_width="match_parent" android:layout_height="40dp" android:background="#ff3660" android:gravity="center" android:text="购物车" android:textColor="#ffffff" android:textSize="20sp" /> <ExpandableListView android:id="@+id/elv" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" /> <RelativeLayout android:layout_width="match_parent" android:layout_height="50dp" android:background="#33000000"> <CheckBox android:id="@+id/cbAll" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:text="全选" /> <TextView android:id="@+id/tvTotal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:text="合计:" /> </RelativeLayout> </LinearLayout> 控制器
package com.example.cll.erjigouwuche; import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.widget.BaseExpandableListAdapter; import android.widget.CheckBox; import android.widget.TextView; import org.greenrobot.eventbus.EventBus; import java.util.List; import static com.example.cll.erjigouwuche.R.id.cb; /** * Created by peng on 2017/10/24. */ public class MyAdapter extends BaseExpandableListAdapter { private Context context; private List<Groupbean> groupList; private List<List<Childbean>> childList; private int count; public MyAdapter(Context context, List<Groupbean> groupList, List<List<Childbean>> childList) { this.context = context; this.groupList = groupList; this.childList = childList; } @Override public int getGroupCount() { return groupList.size(); } @Override public int getChildrenCount(int groupPosition) { return childList.get(groupPosition).size(); } @Override public Object getGroup(int groupPosition) { return groupList.get(groupPosition); } @Override public Object getChild(int groupPosition, int childPosition) { return childList.get(groupPosition).get(childPosition); } @Override public long getGroupId(int groupPosition) { return groupPosition; } @Override public long getChildId(int groupPosition, int childPosition) { return childPosition; } @Override public boolean hasStableIds() { return false; } @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { View view; GroupViewHolder holder; if (convertView == null) { holder = new GroupViewHolder(); view = View.inflate(context, R.layout.item, null); holder.cb = view.findViewById(R.id.cb); holder.tv = view.findViewById(R.id.tvname); view.setTag(holder); } else { view = convertView; holder = (GroupViewHolder) view.getTag(); } //赋值 Groupbean groupBean = groupList.get(groupPosition); holder.tv.setText(groupBean.getGroupname()); holder.cb.setChecked(groupBean.isChoked()); //给group的checkbox设置点击事件 holder.cb.setOnClickListener(new GroupCbOnClickListener(groupPosition)); return view; } @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { View view; ChildViewHolder holder; if (convertView == null) { holder = new ChildViewHolder(); view = View.inflate(context, R.layout.childitem, null); holder.cb = view.findViewById(cb); holder 16045 .tv = view.findViewById(R.id.tvName); view.setTag(holder); } else { view = convertView; holder = (ChildViewHolder) view.getTag(); } //赋值 Childbean childBean = childList.get(groupPosition).get(childPosition); holder.cb.setChecked(childBean.isChoked()); holder.tv.setText(childBean.getChildname()); holder.cb.setOnClickListener(new ChildCbOnClickListener(groupPosition, childPosition)); return view; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } class GroupViewHolder { CheckBox cb; TextView tv; } class ChildViewHolder { CheckBox cb; TextView tv; } class ChildCbOnClickListener implements View.OnClickListener { private int groupPosition; private int childPosition; public ChildCbOnClickListener(int groupPosition, int childPosition) { this.groupPosition = groupPosition; this.childPosition = childPosition; } @Override public void onClick(View v) { if (v instanceof CheckBox) { CheckBox cb = (CheckBox) v; List<Childbean> childBeen = childList.get(groupPosition); Childbean childBean = childBeen.get(childPosition); childBean.setChoked(cb.isChecked()); //计算选中的商品数,并发送到主界面进行显示 MessageCounEvent msgCount = new MessageCounEvent(); msgCount.setCount(totalCount()); EventBus.getDefault().post(msgCount); //判断该商家的所有商品的checkbox是否都选中 if (isChildChecked(childBeen)) { groupList.get(groupPosition).setChoked(true); // //如果商品全部选中的话,则去判断所有商家是否都选中 // if (isGroupChecked()) { // //发送消息去改变全选的状态,变成选中状态 // } else { // //发送消息去改变全选的状态,变成未选中状态 // } MessageEvent msg = new MessageEvent(); msg.setFlag(isGroupChecked()); EventBus.getDefault().post(msg); notifyDataSetChanged(); } else { groupList.get(groupPosition).setChoked(false); MessageEvent msg = new MessageEvent(); msg.setFlag(false); EventBus.getDefault().post(msg); notifyDataSetChanged(); notifyDataSetChanged(); } } } } /** * 判断该商家的所有商品的checkbox是否都选中 * * @return */ private boolean isChildChecked(List<Childbean> childBeen) { for (int i = 0; i < childBeen.size(); i++) { Childbean childBean = childBeen.get(i); if (!childBean.isChoked()) { return false; } } return true; } class GroupCbOnClickListener implements View.OnClickListener { private int groupPostion; public GroupCbOnClickListener(int groupPostion) { this.groupPostion = groupPostion; } @Override public void onClick(View v) { if (v instanceof CheckBox) { //多态,因为我是给checkbox设置的点击事件,所以可以强转成checkbox CheckBox cb = (CheckBox) v; //根据cb.isChecked()是否选中,给一级列的checkbox改变状态 groupList.get(groupPostion).setChoked(cb.isChecked()); List<Childbean> childBeenList = childList.get(groupPostion); for (Childbean childBean : childBeenList) { childBean.setChoked(cb.isChecked()); } //计算选中的商品数,并发送到主界面进行显示 MessageCounEvent msgCount = new MessageCounEvent(); msgCount.setCount(totalCount()); EventBus.getDefault().post(msgCount); MessageEvent msg = new MessageEvent(); msg.setFlag(isGroupChecked()); EventBus.getDefault().post(msg); notifyDataSetChanged(); } } } /** * 判断其它的商家是否选中 * * @return */ private boolean isGroupChecked() { for (Groupbean groupBean : groupList) { if (!groupBean.isChoked()) { return false; } } return true; } /** * 主界面全选按钮的操作 * * @param bool */ public void allChecked(boolean bool) { for (int i = 0; i < groupList.size(); i++) { groupList.get(i).setChoked(bool); for (int j = 0; j < childList.get(i).size(); j++) { childList.get(i).get(j).setChoked(bool); } } //计算选中的商品数,并发送到主界面进行显示 MessageCounEvent msgCount = new MessageCounEvent(); msgCount.setCount(totalCount()); EventBus.getDefault().post(msgCount); notifyDataSetChanged(); } private int totalCount() { count = 0; for (int i = 0; i < groupList.size(); i++) { for (int j = 0; j < childList.get(i).size(); j++) { if (childList.get(i).get(j).isChoked()) { //遍历所有的商品,只要是选中状态的,就加1 count++; } } } return count; } } 子bean
package com.example.cll.erjigouwuche; import android.content.Context; import android.view.View; import android.view.ViewGroup; import android.widget.BaseExpandableListAdapter; import android.widget.CheckBox; import android.widget.TextView; import org.greenrobot.eventbus.EventBus; import java.util.List; import static com.example.cll.erjigouwuche.R.id.cb; /** * Created by peng on 2017/10/24. */ public class MyAdapter extends BaseExpandableListAdapter { private Context context; private List<Groupbean> groupList; private List<List<Childbean>> childList; private int count; public MyAdapter(Context context, List<Groupbean> groupList, List<List<Childbean>> childList) { this.context = context; this.groupList = groupList; this.childList = childList; } @Override public int getGroupCount() { return groupList.size(); } @Override public int getChildrenCount(int groupPosition) { return childList.get(groupPosition).size(); } @Override public Object getGroup(int groupPosition) { return groupList.get(groupPosition); } @Override public Object getChild(int groupPosition, int childPosition) { return childList.get(groupPosition).get(childPosition); } @Override public long getGroupId(int groupPosition) { return groupPosition; } @Override public long getChildId(int groupPosition, int childPosition) { return childPosition; } @Override public boolean hasStableIds() { return false; } @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { View view; GroupViewHolder holder; if (convertView == null) { holder = new GroupViewHolder(); view = View.inflate(context, R.layout.item, null); holder.cb = view.findViewById(R.id.cb); holder.tv = view.findViewById(R.id.tvname); view.setTag(holder); } else { view = convertView; holder = (GroupViewHolder) view.getTag(); } //赋值 Groupbean groupBean = groupList.get(groupPosition); holder.tv.setText(groupBean.getGroupname()); holder.cb.setChecked(groupBean.isChoked()); //给group的checkbox设置点击事件 holder.cb.setOnClickListener(new GroupCbOnClickListener(groupPosition)); return view; } @Override public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { View view; ChildViewHolder holder; if (convertView == null) { holder = new ChildViewHolder(); view = View.inflate(context, R.layout.childitem, null); holder.cb = view.findViewById(cb); holder.tv = view.findViewById(R.id.tvName); view.setTag(holder); } else { view = convertView; holder = (ChildViewHolder) view.getTag(); } //赋值 Childbean childBean = childList.get(groupPosition).get(childPosition); holder.cb.setChecked(childBean.isChoked()); holder.tv.setText(childBean.getChildname()); holder.cb.setOnClickListener(new ChildCbOnClickListener(groupPosition, childPosition)); return view; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } class GroupViewHolder { CheckBox cb; TextView tv; } class ChildViewHolder { CheckBox cb; TextView tv; } class ChildCbOnClickListener implements View.OnClickListener { private int groupPosition; private int childPosition; public ChildCbOnClickListener(int groupPosition, int childPosition) { this.groupPosition = groupPosition; this.childPosition = childPosition; } @Override public void onClick(View v) { if (v instanceof CheckBox) { CheckBox cb = (CheckBox) v; List<Childbean> childBeen = childList.get(groupPosition); Childbean childBean = childBeen.get(childPosition); childBean.setChoked(cb.isChecked()); //计算选中的商品数,并发送到主界面进行显示 MessageCounEvent msgCount = new MessageCounEvent(); msgCount.setCount(totalCount()); EventBus.getDefault().post(msgCount); //判断该商家的所有商品的checkbox是否都选中 if (isChildChecked(childBeen)) { groupList.get(groupPosition).setChoked(true); // //如果商品全部选中的话,则去判断所有商家是否都选中 // if (isGroupChecked()) { // //发送消息去改变全选的状态,变成选中状态 // } else { // //发送消息去改变全选的状态,变成未选中状态 // } MessageEvent msg = new MessageEvent(); msg.setFlag(isGroupChecked()); EventBus.getDefault().post(msg); notifyDataSetChanged(); } else { groupList.get(groupPosition).setChoked(false); MessageEvent msg = new MessageEvent(); msg.setFlag(false); EventBus.getDefault().post(msg); notifyDataSetChanged(); notifyDataSetChanged(); } } } } /** * 判断该商家的所有商品的checkbox是否都选中 * * @return */ private boolean isChildChecked(List<Childbean> childBeen) { for (int i = 0; i < childBeen.size(); i++) { Childbean childBean = childBeen.get(i); if (!childBean.isChoked()) { return false; } } return true; } class GroupCbOnClickListener implements View.OnClickListener { private int groupPostion; public GroupCbOnClickListener(int groupPostion) { this.groupPostion = groupPostion; } @Override public void onClick(View v) { if (v instanceof CheckBox) { //多态,因为我是给checkbox设置的点击事件,所以可以强转成checkbox CheckBox cb = (CheckBox) v; //根据cb.isChecked()是否选中,给一级列的checkbox改变状态 groupList.get(groupPostion).setChoked(cb.isChecked()); List<Childbean> childBeenList = childList.get(groupPostion); for (Childbean childBean : childBeenList) { childBean.setChoked(cb.isChecked()); } //计算选中的商品数,并发送到主界面进行显示 MessageCounEvent msgCount = new MessageCounEvent(); msgCount.setCount(totalCount()); EventBus.getDefault().post(msgCount); MessageEvent msg = new MessageEvent(); msg.setFlag(isGroupChecked()); EventBus.getDefault().post(msg); notifyDataSetChanged(); } } } /** * 判断其它的商家是否选中 * * @return */ private boolean isGroupChecked() { for (Groupbean groupBean : groupList) { if (!groupBean.isChoked()) { return false; } } return true; } /** * 主界面全选按钮的操作 * * @param bool */ public void allChecked(boolean bool) { for (int i = 0; i < groupList.size(); i++) { groupList.get(i).setChoked(bool); for (int j = 0; j < childList.get(i).size(); j++) { childList.get(i).get(j).setChoked(bool); } } //计算选中的商品数,并发送到主界面进行显示 MessageCounEvent msgCount = new MessageCounEvent(); msgCount.setCount(totalCount()); EventBus.getDefault().post(msgCount); notifyDataSetChanged(); } private int totalCount() { count = 0; for (int i = 0; i < groupList.size(); i++) { for (int j = 0; j < childList.get(i).size(); j++) { if (childList.get(i).get(j).isChoked()) { //遍历所有的商品,只要是选中状态的,就加1 count++; } } } return count; } } 子布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="40dp" android:background="#330000ff" android:paddingLeft="20dp" android:gravity="center_vertical" android:orientation="horizontal"> <CheckBox android:id="@+id/cb" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/tvName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" /> </LinearLayout>
父bean
package com.example.cll.erjigouwuche; /** * Created by cll on 2017/10/24. */ public class Groupbean { private boolean choked; private String groupname; public Groupbean(boolean choked, String groupname) { this.choked = choked; this.groupname = groupname; } public boolean isChoked() { return choked; } public void setChoked(boolean choked) { this.choked = choked; } public String getGroupname() { return groupname; } public void setGroupname(String groupname) { this.groupname = groupname; } }父布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="40dp" android:background="#330000ff" android:paddingLeft="20dp" android:gravity="center_vertical" android:orientation="horizontal"> <CheckBox android:id="@+id/cb" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/tvName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="5dp" /> </LinearLayout>
CounEvent
package com.example.cll.erjigouwuche; /** * Created by peng on 2017/10/24. */ public class MessageCounEvent { private int count; public int getCount() { return count; } public void setCount(int count) { this.count = count; } }Event
package com.example.cll.erjigouwuche; /** * Created by peng on 2017/10/24. */ public class MessageEvent { private boolean flag; public boolean isFlag() { return flag; } public void setFlag(boolean flag) { this.flag = flag; } }
相关文章推荐
- 二级列表单选,全选
- Android_二级列表购物车之增删改查,全选反选,加减器,价钱数量计算
- 二级列表实现CheckBox单选及全选
- 二级列表购物车的全选反选
- Android仿京东App购物车 二级列表+全选反选+Ok封装+拦截器+结算+商品数量计算
- 购物车的二级列表全选单选
- Android仿京东App购物车 二级列表+全选反选+Ok封装+拦截器+结算+商品数量计算
- Android_二级列表购物车之增删改查,全选反选,加减器,价钱数量计算
- Jquery实现列表(隔行换色,全选,鼠标滑过当前行)效果实例
- 二级列表的使用,上拉刷新,下拉加载
- 分享个极好的无刷新二级联动下拉列表,同样适用与firefox
- (datagridview)如果单击列表头,全选.(第一列为复选框)
- 好友二级列表(强化版)
- Dom-全选商品列表示范
- studio二级列表查询
- excel建立下拉列表二级级联
- JQuery实现列表中复选框全选反选功能封装
- jquery带动画的二级下拉列表
- android 二级联动列表,仿eleme点餐页面
- Android Studio 购物车,二级目录,全选反选,计数