Android Studio 购物车,二级目录,全选反选,计数


主页面布局
<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="baway.com.shopcartdemo20171024.MainActivity">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:background="#ff3660"
        android:text="购物车"
        android:textColor="#fff"
        android:textSize="20sp"
        android:gravity="center"/>
    <ExpandableListView
        android:id="@+id/elv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"></ExpandableListView>
    <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/tvSum"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:text="合计:"
            android:layout_centerVertical="true"
            android:textSize="20sp"/>
    </RelativeLayout>
</LinearLayout>
 
条目布局:
父条目:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:background="#330000ff"
    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>
子条目
<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包
 
 
public class ChildBean {
    private String childName;
    private boolean checked;

    public ChildBean() {
    }

    public ChildBean(String childName, boolean checked) {
        this.childName = childName;
        this.checked = checked;
    }

    public String getChildName() {
        return childName;
    }

    public void setChildName(String childName) {
        this.childName = childName;
    }

    public boolean isChecked() {
        return checked;
    }

    public void setChecked(boolean checked) {
        this.checked = checked;
    }
}
public class GroupBean {
    private String groupName;
    private boolean checked;

    public GroupBean() {
    }

    public GroupBean(String groupName, boolean checked) {
        this.groupName = groupName;
        this.checked = checked;
    }

    public String getGroupName() {
        return groupName;
    }

    public void setGroupName(String groupName) {
        this.groupName = groupName;
    }

    public boolean isChecked() {
        return checked;
    }

    public void setChecked(boolean checked) {
        this.checked = checked;
    }
}
计数和选中状态
计数:
public class MessageCounEvent {
    private int count;

    public int getCount() {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }
}
选中状态:
public class MessageEvent {
    private boolean flag;

    public boolean isFlag() {
        return flag;
    }

    public void setFlag(boolean flag) {
        this.flag = flag;
    }
}
二级目录适配器
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;

/**
 * Created by admin on 2017/10/24/024.
 */

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 i) {
        return childlist.get(i).size();
    }

    @Override
    public Object getGroup(int i) {
        return grouplist.get(i);
    }

    @Override
    public Object getChild(int i, int i1) {
        return childlist.get(i).get(i1);
    }

    @Override
    public long getGroupId(int i) {
        return i;
    }

    @Override
    public long getChildId(int i, int i1) {
        return i1;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
        View v = null;
        GroupViewHolder holder;
        if (view == null) {
            holder = new GroupViewHolder();
            v = View.inflate(context, R.layout.item, null);
            holder.cb = v.findViewById(R.id.cb);
            holder.tv = v.findViewById(R.id.tvName);
            v.setTag(holder);
        } else {
            v = view;
            holder = (GroupViewHolder) v.getTag();
        }
        //赋值
        GroupBean bean = grouplist.get(i);
        holder.cb.setChecked(bean.isChecked());
        holder.tv.setText(bean.getGroupName());
        //给group的checkbox设置点击事件
        holder.cb.setOnClickListener(new GroupCbOnClick(i));
        return v;
    }

    @Override
    public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
        View v=null;
        ChildViewHolder holder;
        if(view==null){
            holder=new ChildViewHolder();
            v=View.inflate(context,R.layout.child_item,null);
            holder.cb=v.findViewById(R.id.cb);
            holder.tv=v.findViewById(R.id.tvName);
            v.setTag(holder);
        }else{
            v=view;
            holder= (ChildViewHolder) v.getTag();
        }
        //赋值
        ChildBean childBean = childlist.get(i).get(i1);
        holder.cb.setChecked(childBean.isChecked());
        holder.tv.setText(childBean.getChildName());

        holder.cb.setOnClickListener(new ChildCbOnClick(i, i1));

        return v;
    }

    @Override
    public boolean isChildSelectable(int i, int i1) {
        return true;
    }

    class GroupViewHolder {
        CheckBox cb;
        TextView tv;
    }

    class ChildViewHolder {
        CheckBox cb;
        TextView tv;
    }

    class ChildCbOnClick implements View.OnClickListener {

        private int groupPosition;
        private int childPosition;

        public ChildCbOnClick(int groupPosition, int childPosition) {
            this.groupPosition = groupPosition;
            this.childPosition = childPosition;
        }

        @Override
        public void onClick(View view) {
            if (view instanceof CheckBox) {
                CheckBox cb = (CheckBox) view;
                List<ChildBean> cBean = childlist.get(groupPosition);
                ChildBean cbean = cBean.get(childPosition);
                cbean.setChecked(cb.isChecked());
                //计算选中的商品数,并发送到主界面进行显示
                MessageCounEvent msgCount = new MessageCounEvent();
                msgCount.setCount(totalCount());
                EventBus.getDefault().post(msgCount);
                //判断该商家的所有商品的checkbox是否都选中
                if(isChildChecked(cBean)){
                    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();
                }

            }
        }
    }

    class GroupCbOnClick implements View.OnClickListener{

        private int groupPostion;

        public GroupCbOnClick(int groupPostion){
            this.groupPostion=groupPostion;
        }

        @Override
        public void onClick(View view) {
            if (view instanceof CheckBox){
                //多态,因为我是给checkbox设置的点击事件,所以可以强转成checkbox
                CheckBox cb= (CheckBox) view;
                //根据cb.isChecked()是否选中,给一级列的checkbox改变状态\
                grouplist.get(groupPostion).setChecked(cb.isChecked());
                List<ChildBean> cbList=childlist.get(groupPostion);
                for (ChildBean cbean:cbList){
                    cbean.setChecked(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();
            }

        }
    }

    /**
     * 判断该商家的所有商品的checkbox是否都选中
     *
     * @return
     */
    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;
    }

    /**
     * 判断其它的商家是否选中
     *
     * @return
     */
    private boolean isGroupChecked() {
        for (GroupBean groupBean : grouplist) {
            if (!groupBean.isChecked()) {
                return false;
            }
        }
        return true;
    }

    /**
     * 主界面全选按钮的操作
     *
     * @param bool
     */
    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 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).isChecked()) {
                    //遍历所有的商品,只要是选中状态的,就加1
                    count++;
                }
            }
        }
        return count;
    }
}

主界面显示:
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 elv;
    private CheckBox cbAll;
    private TextView tvSum;
    private List<GroupBean> grouoplist = 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,grouoplist,childlist);
        elv.setGroupIndicator(null);
        elv.setAdapter(adapter);
        //全部展开
        for (int i=0;i<grouoplist.size();i++){
            elv.expandGroup(i);
        }
        //给全选设置点击事件
        cbAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                adapter.allChecked(cbAll.isChecked());
            }
        });

    }

    private void initData() {
        for (int i=0;i<3;i++){
            GroupBean gBaen=new GroupBean("商家"+i,false);
            grouoplist.add(gBaen);
            List<ChildBean> cList=new ArrayList<>();
            for (int j=0;j<2;j++){
                ChildBean cBean=new ChildBean("商品"+i,false);
                cList.add(cBean);
            }
            childlist.add(cList);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
    @Subscribe
    public void messageCountEvent(MessageCounEvent msg){
        tvSum.setText("总计:"+msg.getCount()+"个");
    }
    @Subscribe
    public void messageEvent(MessageEvent msg){
        cbAll.setChecked(msg.isFlag());
    }

    private void initView() {
        elv = (ExpandableListView) findViewById(R.id.elv);
        cbAll = (CheckBox) findViewById(R.id.cbAll);
        tvSum = (TextView) findViewById(R.id.tvSum);
    }
}

 
 
 

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值