购物车(二级列表)
来源:互联网 发布:手机屏幕动物软件 编辑:程序博客网 时间:2024/03/29 18:20
package com.example.week;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 com.example.week.adapter.MyAdapter;import com.example.week.bean.ChildBean;import com.example.week.bean.GroupBean;import java.util.ArrayList;import java.util.List;import de.greenrobot.event.EventBus;import de.greenrobot.event.Subscribe;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(); 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("商家"+i,false); grouplist.add(groupBean); List<ChildBean> list = new ArrayList<>(); for (int j = 0;j<2;j++){ ChildBean childBean = new ChildBean("商品"+i,false); list.add(childBean); } childlist.add(list); } } private void initView() { mElv = (ExpandableListView) findViewById(R.id.elv); mCbAll = (CheckBox) findViewById(R.id.cbAll); mTvTotal = (TextView) findViewById(R.id.tvTotal); }}//适配器package com.example.week.adapter;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 com.example.week.MessageCounEvent;import com.example.week.MessageEvent;import com.example.week.R;import com.example.week.bean.ChildBean;import com.example.week.bean.GroupBean;import java.security.acl.Group;import java.util.List;import de.greenrobot.event.EventBus;/** * Created by FLOWER 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.cb.setChecked(groupBean.isChecked()); holder.tv.setText(groupBean.getGroupName()); //给group的checkbox设置点击事件 holder.cb.setOnClickListener(new GroupOnClickListener(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(R.id.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.isChecked()); 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.setChecked(cb.isChecked()); //计算选中的商品数,并发送到主界面进行显示 MessageCounEvent messageCounEvent = new MessageCounEvent(); messageCounEvent.setCount(totalCount()); EventBus.getDefault().post(messageCounEvent); //判断该商家的所有商品的CheckBox是否都选中 if (isChildChecked(childBeen)){ grouplist.get(groupPosition).setChecked(true); MessageEvent msg = new MessageEvent(); msg.setFlag(isGroupChecked()); EventBus.getDefault().post(msg); notifyDataSetChanged(); }else { grouplist.get(groupPosition).setChecked(false); MessageEvent msg = new MessageEvent(); msg.setFlag(false); EventBus.getDefault().post(msg); notifyDataSetChanged(); notifyDataSetChanged(); } } } } //判断该商家的所有商品的checkbox是否都选中 private boolean isChildChecked(List<ChildBean> childBeen){ for (int i = 0;i<childBeen.size();i++){ ChildBean childBean = childBeen.get(i); if (!childBean.isChecked()){ return false; } } return true; } class GroupOnClickListener implements View.OnClickListener{ private int groupPosition; public GroupOnClickListener(int groupPosition) { this.groupPosition = groupPosition; } @Override public void onClick(View v) { if (v instanceof CheckBox){ //多态,因为我是给CheckBox设置的点击事件,所有可以强转成CheckBox CheckBox cb = (CheckBox) v; //genjucb.isChecked()是否选中,给一级列表的CheckBox改变状态 grouplist.get(groupPosition).setChecked(cb.isChecked()); List<ChildBean> childBeenList = childlist.get(groupPosition); for (ChildBean childBean : childBeenList){ childBean.setChecked(cb.isChecked()); } //计算选中的商品,并发送到主界面进行显示 MessageCounEvent messageCounEvent = new MessageCounEvent(); messageCounEvent.setCount(totalCount()); EventBus.getDefault().post(messageCounEvent); MessageEvent msg = new MessageEvent(); msg.setFlag(isGroupChecked()); EventBus.getDefault().post(msg); notifyDataSetChanged(); notifyDataSetChanged(); } } } //判断其他的商家是否选中 private boolean isGroupChecked(){ for (GroupBean groupBean : grouplist){ if (!groupBean.isChecked()){ return false; } } return true; } //主界面全选按钮的操作 public void allChecked(boolean bool){ for (int i = 0; i<grouplist.size();i++){ grouplist.get(i).setChecked(bool); for (int j = 0;j<childlist.get(i).size();j++){ childlist.get(i).get(j).setChecked(bool); } } //计算选中的商品数,并发送到主界面进行显示 MessageCounEvent messageCounEvent = new MessageCounEvent(); messageCounEvent.setCount(totalCount()); EventBus.getDefault().post(messageCounEvent); 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).isChecked()){ //遍历所有的商品,只要是选中状态的,就加1; count++; } } } return count; }}//MessageCounEventpublic class MessageCounEvent { private int count; public int getCount() { return count; } public void setCount(int count) { this.count = count; }//MessageEventpublic class MessageEvent { private boolean flag; public boolean isFlag() { return flag; } public void setFlag(boolean flag) { this.flag = flag; }}
阅读全文
0 0
- 二级列表购物车
- 购物车二级列表
- 购物车(二级列表)
- 二级列表购物车
- 购物车二级列表
- 购物车二级列表
- 二级列表购物车
- 二级列表购物车
- 购物车二级列表
- 二级列表购物车
- 二级列表购物车
- 二级列表 购物车
- 购物车二级列表
- 二级列表购物车++
- 购物车二级列表
- 二级列表购物车
- 二级列表仿购物车
- Android 二级列表购物车
- Ubuntu16.04中将python3设置为默认
- CodeForces292C Beautiful IP Addresses(简单搜索加大暴力乱搞)
- [转载]SQL语句中LEFT JOIN ON WHERE和LEFT JOIN ON AND的区别
- 10. Regular Expression Matching
- Idea Intellij加载项目之后端口号修改
- 购物车(二级列表)
- etherem创世块的创建、创建账户以及账户间的建立通道
- Android Recyclerview适配器 加载头部 以及自定义View
- 大话设计模式----开放-封闭原则
- nginx 服务器上Laravel5.1框架的一些配置
- 腾讯云 centos7 配置服务器所需要知道的
- 面试Java web,有关于hibernate一般都问什么问题?
- 自定义浏览器协议,实现web页面调用本地程序
- EA&UML日拱一卒-状态图::状态机