购物车二级列表
来源:互联网 发布:ceic数据库免费账号 编辑:程序博客网 时间:2024/04/29 04:22
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;
import test.baway.com.shoppingexam.net.MessageCountEvent;
import test.baway.com.shoppingexam.net.MessageEvent;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private ExpandableListView mElvMain;
/**
* 全选
*/
private CheckBox mCbMainAll;
/**
* 合计:
*/
private String[] group = {"射手", "刺客", "法师"};
private String[] child = {"射手", "刺客", "法师"};
private TextView mTvMainTotal;
private List<GroupBean> grouplist = new ArrayList<>();
private List<List<ChildBean>> childlist = new ArrayList<>();
private MyExAdapter myExAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EventBus.getDefault().register(this);
setContentView(R.layout.activity_main);
initView();
init();
myExAdapter = new MyExAdapter(this, grouplist, childlist);
mElvMain.setAdapter(myExAdapter);
}
@Subscribe
public void MessageEvent(MessageEvent msg) {
mCbMainAll.setChecked(msg.isFlag());
}
@Subscribe
public void MessageCountEvent(MessageCountEvent msgcount) {
mTvMainTotal.setText("总计:" + msgcount.getCount() + "个");
}
@Override
protected void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
}
private void init() {
for (int i = 0; i < 3; i++) {
GroupBean groupBean = new GroupBean(group[i], false);
grouplist.add(groupBean);
List<ChildBean> list = new ArrayList<>();
for (int j = 0; j < 2; j++) {
ChildBean childBean = new ChildBean(child[j], false);
list.add(childBean);
}
childlist.add(list);
}
}
private void initView() {
mElvMain = (ExpandableListView) findViewById(R.id.elv_main);
mCbMainAll = (CheckBox) findViewById(R.id.cb_main_All);
mCbMainAll.setOnClickListener(this);
mTvMainTotal = (TextView) findViewById(R.id.tv_main_Total);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.cb_main_All:
boolean checked = mCbMainAll.isChecked();
myExAdapter.allchecked(checked);
break;
}
}
}
package test.baway.com.shoppingexam;
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 test.baway.com.shoppingexam.net.MessageCountEvent;
import test.baway.com.shoppingexam.net.MessageEvent;
public class MyExAdapter extends BaseExpandableListAdapter {
private Context context;
private List<GroupBean> groupBeanList;
private List<List<ChildBean>> childBeanList;
private int count;
public MyExAdapter(Context context, List<GroupBean> groupBeanList, List<List<ChildBean>> childBeanList) {
this.context = context;
this.groupBeanList = groupBeanList;
this.childBeanList = childBeanList;
}
@Override
public int getGroupCount() {
return groupBeanList.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return childBeanList.get(groupPosition).size();
}
@Override
public Object getGroup(int groupPosition) {
return groupBeanList.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return childBeanList.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(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
GroupViewHolder groupViewHolder = null;
if (convertView == null) {
groupViewHolder = new GroupViewHolder();
convertView = View.inflate(context, R.layout.item, null);
groupViewHolder.cb_item_checked = convertView.findViewById(R.id.cb_item_checked);
groupViewHolder.tv_item_Name = convertView.findViewById(R.id.tv_item_Name);
convertView.setTag(groupViewHolder);
} else {
groupViewHolder = (GroupViewHolder) convertView.getTag();
}
GroupBean groupBean = groupBeanList.get(groupPosition);
groupViewHolder.cb_item_checked.setChecked(groupBean.isChecked());
groupViewHolder.tv_item_Name.setText(groupBean.getGroupName());
final GroupViewHolder finalGroupViewHolder = groupViewHolder;
groupViewHolder.cb_item_checked.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean checked = finalGroupViewHolder.cb_item_checked.isChecked();
groupBeanList.get(groupPosition).setChecked(checked);
List<ChildBean> childBeen = childBeanList.get(groupPosition);
for (ChildBean arr : childBeen) {
arr.setChecked(checked);
}
changecount();
MessageEvent msg = new MessageEvent();
msg.setFlag(isGroupChecked());
EventBus.getDefault().post(msg);
notifyDataSetChanged();
}
});
return convertView;
}
@Override
public View getChildView(final int groupPosition, int childPosition, final boolean isLastChild, View convertView, ViewGroup parent) {
ChildViewHolder childViewHolder = null;
if (convertView == null) {
childViewHolder = new ChildViewHolder();
convertView = View.inflate(context, R.layout.childitem, null);
childViewHolder.cb_childitem = convertView.findViewById(R.id.cb_childitem);
childViewHolder.tv_childitem_Name = convertView.findViewById(R.id.tv_childitem_Name);
convertView.setTag(childViewHolder);
} else {
childViewHolder = (ChildViewHolder) convertView.getTag();
}
final ChildBean childBean = childBeanList.get(groupPosition).get(childPosition);
childViewHolder.cb_childitem.setChecked(childBean.isChecked());
childViewHolder.tv_childitem_Name.setText(childBean.getChildName());
final ChildViewHolder finalChildViewHolder = childViewHolder;
childViewHolder.cb_childitem.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean checked = finalChildViewHolder.cb_childitem.isChecked();
childBean.setChecked(checked);
if (checked) {
if (isChildChecked(childBeanList.get(groupPosition))) {
groupBeanList.get(groupPosition).setChecked(true);
MessageEvent msg = new MessageEvent();
msg.setFlag(isGroupChecked());
EventBus.getDefault().post(msg);
}
} else {
groupBeanList.get(groupPosition).setChecked(false);
MessageEvent msg = new MessageEvent();
msg.setFlag(false);
EventBus.getDefault().post(msg);
}
changecount();
notifyDataSetChanged();
}
});
return convertView;
}
/**
* 判断其它的商家是否选中
*
* @return
*/
private boolean isGroupChecked() {
for (GroupBean groupBean : groupBeanList) {
if (!groupBean.isChecked()) {
return false;
}
}
return true;
}
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;
}
public void allchecked(boolean result) {
for (int i = 0; i < groupBeanList.size(); i++) {
groupBeanList.get(i).setChecked(result);
for (int j = 0; j < childBeanList.get(i).size(); j++) {
childBeanList.get(i).get(j).setChecked(result);
}
}
changecount();
notifyDataSetChanged();
}
private void changecount() {
//计算所有的总个数
MessageCountEvent msgcount = new MessageCountEvent();
msgcount.setCount(totalCount());
EventBus.getDefault().post(msgcount);
}
private int totalCount() {
count = 0;
for (int i = 0; i < groupBeanList.size(); i++) {
for (int j = 0; j < childBeanList.get(i).size(); j++) {
if (childBeanList.get(i).get(j).isChecked()) {
//遍历所有的商品,只要是选中状态的,就加1
count++;
}
}
}
return count;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
class GroupViewHolder {
CheckBox cb_item_checked;
TextView tv_item_Name;
}
class ChildViewHolder {
CheckBox cb_childitem;
TextView tv_childitem_Name;
}
}
package test.baway.com.shoppingexam;
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;
}
}
package test.baway.com.shoppingexam;
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;
}
}
<?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="test.baway.com.shoppingexam.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_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#33000000">
<CheckBox
android:id="@+id/cb_main_All"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="全选" />
<TextView
android:id="@+id/tv_main_Total"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:text="合计:" />
</RelativeLayout>
</LinearLayout>
<?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:descendantFocusability="blocksDescendants"
android:gravity="center_vertical"
android:orientation="horizontal">
<CheckBox
android:id="@+id/cb_item_checked"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/tv_item_Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp" />
</LinearLayout>
<?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:gravity="center_vertical"
android:orientation="horizontal"
android:paddingLeft="20dp">
<CheckBox
android:id="@+id/cb_childitem"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/tv_childitem_Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp" />
</LinearLayout>
- 二级列表购物车
- 购物车二级列表
- 购物车(二级列表)
- 二级列表购物车
- 购物车二级列表
- 购物车二级列表
- 二级列表购物车
- 二级列表购物车
- 购物车二级列表
- 二级列表购物车
- 二级列表购物车
- 二级列表 购物车
- 购物车二级列表
- 二级列表购物车++
- 购物车二级列表
- 二级列表购物车
- 二级列表仿购物车
- Android 二级列表购物车
- Spring boot logback自定义配置
- handler轮播网上请求的图片
- double类型的精确计算工具
- muduo库源码学习(base)Condition
- HLS 协议详解
- 购物车二级列表
- 乐观锁与悲观锁
- SCP不用密码传输文件
- 图像压缩-数字图像处理
- 如果你还没冒犯过别人,说明你可能还没做过一件重要的事
- 《面筋:拿到美团点评和去哪儿网的offer》
- 常用 OpenSSL命令汇总
- HTML 超链接,锚点,定位
- 基于字符串的分离链接hash算法