二级购物车

xiaoxiao2021-02-28  20

//布局main <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" 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" tools:context="com.example.expand.MainActivity"> <TextView android:layout_width="match_parent" android:layout_height="40dp" android:text="购物车" android:background="#ff3660" android:gravity="center" /> <ExpandableListView android:id="@+id/main_elv" android:layout_weight="1" android:layout_width="match_parent" android:layout_height="match_parent"></ExpandableListView> <RelativeLayout android:layout_width="match_parent" android:layout_height="50dp"> <CheckBox android:id="@+id/main_cb" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:text="全选"/> <TextView android:id="@+id/mian_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:text="合计:" /> </RelativeLayout> </LinearLayout> //Group布局 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content"> <CheckBox android:id="@+id/cb" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="aa"/> </LinearLayout> //child布局 <?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="match_parent" > <CheckBox android:layout_marginLeft="15dp" android:id="@+id/cb" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="aa"/> </LinearLayout> //mianActivity package com.example.expand; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Adapter; 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 List<GroupBean> groupList=new ArrayList<>(); private List<List<ChildBean>> childList=new ArrayList<>(); private MyAdapert myAdapert; private CheckBox cb; private TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); EventBus.getDefault().register(this); initView(); initData(); myAdapert = new MyAdapert(this, groupList, childList); elv.setGroupIndicator(null); elv.setAdapter(myAdapert); for (int i=0;i<groupList.size();i++){ elv.expandGroup(i); } cb.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { myAdapert.allChecked(cb.isChecked()); } }); } @Override protected void onDestroy() { super.onDestroy(); EventBus.getDefault().unregister(this); } @Subscribe public void messageCountEvent(MessageCounEvent msg) { tv.setText("总计:" + msg.getCount() + "个"); } @Subscribe public void messageEvent(MessageEvent msg) { cb.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() { elv = (ExpandableListView) findViewById(R.id.main_elv); cb = (CheckBox) findViewById(R.id.main_cb); tv = (TextView) findViewById(R.id.mian_tv); } } //适配器 package com.example.expand; 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.ArrayList; import java.util.List; /** * Created by Helloworld on 2017/10/24. */ public class MyAdapert extends BaseExpandableListAdapter { private Context context; private List<GroupBean> groupList; private List<List<ChildBean>> childList; private int count; public MyAdapert(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.tv = view.findViewById(R.id.tv); holder.cb = view.findViewById(R.id.cb); view.setTag(holder); }else { view = convertView; holder = (GroupViewHolder) view.getTag(); } GroupBean groupBean = groupList.get(groupPosition); holder.cb.setChecked(groupBean.isChecked()); holder.tv.setText(groupBean.getName()); 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.layout, null); holder.tv = view.findViewById(R.id.tv); holder.cb = view.findViewById(R.id.cb); 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.getName()); 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 GroupCbOnClickListener implements View.OnClickListener{ private int groupPostion; private int childPostion; public GroupCbOnClickListener(int groupPostion) { this.groupPostion = groupPostion; this.childPostion = childPostion; } @Override public void onClick(View v) { if (v instanceof CheckBox) { //多态,因为我是给checkbox设置的点击事件,所以可以强转成checkbox CheckBox cb = (CheckBox) v; //根据cb.isChecked()是否选中,给一级列的checkbox改变状态 groupList.get(groupPostion).setChecked(cb.isChecked()); List<ChildBean> childBeenList = childList.get(groupPostion); for (ChildBean childBean : childBeenList) { childBean.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(); } } } 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 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 msgCount = new MessageCounEvent(); msgCount.setCount(totalCount()); EventBus.getDefault().post(msgCount); //判断该商家的所有商品的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(); } } } } 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 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()){ count++; } } } return count; } } //封装 EventyBus package com.example.expand; /** * Created by Helloworld on 2017/10/24. */ public class MessageEvent { private boolean flag; public boolean isFlag() { return flag; } public void setFlag(boolean flag) { this.flag = flag; } } //封装EventyBus package com.example.expand; /** * Created by Helloworld on 2017/10/24. */ public class MessageCounEvent { private int count; public int getCount() { return count; } public void setCount(int count) { this.count = count; } } //GroupBean package com.example.expand; /** * Created by Helloworld on 2017/10/24. */ public class MessageCounEvent { private int count; public int getCount() { return count; } public void setCount(int count) { this.count = count; } } //childBean package com.example.expand; /** * Created by Helloworld on 2017/10/24. */ public class ChildBean { private String name; private boolean checked; public ChildBean(String name, boolean checked) { this.name = name; this.checked = checked; } public String getName() { return name; } public void setName(String name) { this.name = name; } public boolean isChecked() { return checked; } public void setChecked(boolean checked) { this.checked = checked; } }
转载请注明原文地址: https://www.6miu.com/read-1999980.html

最新回复(0)