首先呢 购物车分好多种 这里我是用一个ExpandableList来实现二级的一个购物车
因为ExpandableList它可以很轻松的实现一个二级购物车的展示
那么先来看一下购物车的适配器
//适配器
public class MyShopAdapterextends BaseExpandableListAdapter{
private Contextcontext;
private ListbeanList;
public MyShopAdapter(Context context, List beanList) {
this.context = context;
this.beanList = beanList;
}
@Override
public int getGroupCount() {
return beanList.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return beanList.get(groupPosition).getList().size();
}
@Override
public Object getGroup(int groupPosition) {
return null;
}
@Override
public Object getChild(int groupPosition,int childPosition) {
return null;
}
@Override
public long getGroupId(int groupPosition) {
return 0;
}
@Override
public long getChildId(int groupPosition,int childPosition) {
return 0;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public View getGroupView(final int groupPosition,boolean isExpanded, View convertView, ViewGroup parent) {
GroupViewHolder groupViewHolder;
if (convertView ==null) {
groupViewHolder =new GroupViewHolder();
convertView = View.inflate(context, R.layout.layout,null);
groupViewHolder.name = convertView.findViewById(R.id.name);
groupViewHolder.checkBox = convertView.findViewById(R.id.select_item);
convertView.setTag(groupViewHolder);
}else {
groupViewHolder = (GroupViewHolder) convertView.getTag();
}
groupViewHolder.name.setText(beanList.get(groupPosition).getSellerName());
groupViewHolder.checkBox.setChecked(beanList.get(groupPosition).isIscheck());
groupViewHolder.checkBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CheckBox checkB = (CheckBox) v;
boolean checked = checkB.isChecked();
groupselect(checked,groupPosition);
}
});
return convertView;
}
private void groupselect(boolean checked,int groupPosition) {
ShoppBean.DataBean dataBean =beanList.get(groupPosition);
dataBean.setIscheck(checked);
for (int j =0; j < dataBean.getList().size(); j++) {
ShoppBean.DataBean.ListBean listBean = dataBean.getList().get(j);
listBean.setIschecks(checked);
}
notifyDataSetChanged();
}
@Override
public View getChildView(final int groupPosition,final int childPosition,boolean isLastChild, View convertView, ViewGroup parent) {
final ChildViewHolder childViewHolder;
if (convertView ==null) {
childViewHolder =new ChildViewHolder();
convertView = View.inflate(context, R.layout.item1,null);
childViewHolder.name = convertView.findViewById(R.id.name);
childViewHolder.checkBox = convertView.findViewById(R.id.select_item);
childViewHolder.image = convertView.findViewById(R.id.image);
convertView.setTag(childViewHolder);
}else {
childViewHolder = (ChildViewHolder) convertView.getTag();
}
childViewHolder.name.setText(beanList.get(groupPosition).getList().get(childPosition).getTitle());
childViewHolder.checkBox.setChecked(beanList.get(groupPosition).getList().get(childPosition).isIschecks());
Uri parse = Uri.parse(beanList.get(groupPosition).getList().get(childPosition).getDetailUrl());
childViewHolder.image.setImageURI(parse);
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition,int childPosition) {
return true;
}
private CheckBoxmcheckBox;
public void setCheckBox(final CheckBox checkBox) {
this.mcheckBox = checkBox;
mcheckBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CheckBox checkB = (CheckBox) v;
boolean checked = checkB.isChecked();
selectAll(checked);
}
});
}
private void selectAll(boolean checked) {
for (int i =0; i
ShoppBean.DataBean dataBean =beanList.get(i);
dataBean.setIscheck(checked);
for (int j =0; j
beanList.get(j);
dataBean.setIscheck(checked);
}
}
notifyDataSetChanged();
}
class GroupViewHolder {
TextViewname;
CheckBoxcheckBox;
}
class ChildViewHolder {
TextViewname;
CheckBoxcheckBox;
SimpleDraweeViewimage;
}
}