这是一个仿的购物车
首先把布局文件贴出来
这个是自定义的加减器
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:orientation="horizontal"
android:gravity="center_vertical"
android:layout_gravity="center_vertical"
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="@dimen/dp_60"
android:layout_height="@dimen/dp_30">
<TextView
android:background="#ffffff"
android:layout_weight="1"
android:id="@+id/sub_tv"
android:layout_width="0dp"
android:layout_height="match_parent"
android:gravity="center"
android:text="-"
android:textSize="16sp" />
<TextView
android:text="1"
android:layout_marginLeft="2dp"
android:background="#ffffff"
android:layout_weight="1"
android:id="@+id/product_number_tv"
android:layout_width="0dp"
android:layout_height="match_parent"
android:gravity="center"
/>
<TextView
android:id="@+id/add_tv"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="2dp"
android:layout_weight="1"
android:background="#ffffff"
android:gravity="center"
android:text="+"
android:textSize="16sp" />
</LinearLayout>
购物车的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
tools:context=".fragment.GouWuFragment">
<RelativeLayout
android:layout_marginTop="@dimen/dp_10"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<TextView
android:text="购物车"
android:textSize="@dimen/sp_18"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:text="编辑"
android:textSize="@dimen/sp_16"
android:layout_marginRight="@dimen/dp_20"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
<ExpandableListView
android:id="@+id/liebiao"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="8"></ExpandableListView>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<CheckBox
android:id="@+id/quanxuan"
android:layout_marginLeft="@dimen/dp_15"
android:layout_centerVertical="true"
android:layout_width="@dimen/dp_16"
android:layout_height="@dimen/dp_16" />
<TextView
android:id="@+id/zanyong"
android:textSize="@dimen/sp_14"
android:text="全选"
android:layout_marginLeft="@dimen/dp_5"
android:layout_centerVertical="true"
android:layout_toRightOf="@id/quanxuan"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/price"
android:textSize="@dimen/sp_14"
android:text="¥666.00"
android:layout_marginLeft="@dimen/dp_55"
android:layout_centerVertical="true"
android:layout_toRightOf="@id/zanyong"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/qujiesuan"
android:text="去结算"
android:textColor="#ffffff"
android:background="#e93229"
android:layout_alignParentRight="true"
android:layout_width="@dimen/dp_100"
android:layout_height="match_parent" />
</RelativeLayout>
</LinearLayout>
两个条目的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:orientation="horizontal"
android:descendantFocusability="blocksDescendants"
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<CheckBox
android:id="@+id/fu_check"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/fu_title"
android:layout_width="wrap_content"
android:text="玩具店"
android:layout_height="wrap_content" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:orientation="horizontal"
android:descendantFocusability="blocksDescendants"
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<CheckBox
android:id="@+id/zi_check"
android:layout_marginTop="@dimen/dp_50"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/iv_head_background"
android:layout_width="@dimen/dp_120"
android:layout_height="@dimen/dp_120"/>
<TextView
android:id="@+id/zi_title"
android:text="sssss"
android:layout_toRightOf="@id/iv_head_background"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/zi_price"
android:text="sssss"
android:layout_marginTop="@dimen/dp_100"
android:layout_toRightOf="@id/iv_head_background"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<bwie.com.lixiaoliang20181217.AddView
android:id="@+id/zi_jiajian"
android:layout_width="wrap_content"
android:layout_marginTop="@dimen/dp_90"
android:layout_alignParentRight="true"
android:layout_height="wrap_content">
</bwie.com.lixiaoliang20181217.AddView>
<TextView
android:id="@+id/zi_zeng"
android:text="ssdfsfdsfd"
android:layout_marginTop="@dimen/dp_120"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</RelativeLayout>
</LinearLayout>
fragment的代码
public class GouWuFragment extends Fragment implements V_GoWu {
@BindView(R.id.liebiao)
ExpandableListView liebiao;
@BindView(R.id.quanxuan)
CheckBox quanxuan;
@BindView(R.id.zanyong)
TextView zanyong;
@BindView(R.id.price)
TextView price;
@BindView(R.id.qujiesuan)
Button qujiesuan;
Unbinder unbinder;
private P_GoWuCheIm mWuCheIm;
private Sadaptrer mSadaptrer;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_gou_wu, container, false);
unbinder = ButterKnife.bind(this, view);
EventBus.getDefault().register(this);
mWuCheIm = new P_GoWuCheIm(this);
mWuCheIm.setGoWu();
initData();
return view;
}
private void initData() {
}
@Override
public void onDestroyView() {
super.onDestroyView();
unbinder.unbind();
}
@Override
public void setGo(List<GoWuBean.DataBean> data1) {
//判断数据是否非空
if (data1 != null) {
//给适配器赋值
mSadaptrer = new Sadaptrer(data1, getContext());
liebiao.setAdapter(mSadaptrer);
//展开二级列表
for(int x=0; x<data1.size(); x++){
liebiao.expandGroup(x);
}
}
}
//当全选的按钮被点击的时候
@OnClick(R.id.quanxuan)
public void onViewClicked() {
//获取当前条目的状态
boolean allProductsSelected = mSadaptrer.isAllProductsSelected();
//给你的条目设置值
mSadaptrer.changeStatus(!allProductsSelected);
//刷新适配器
mSadaptrer.notifyDataSetChanged();
}
//接收适配器传过来的值
@Subscribe
public void onMessageEvent(Message event) {
//接收适配器(Myadpader)传递过来的值 ,将全选的状态设置为true或者false
quanxuan.setChecked(event.isIsboon());
}
//接收适配器传过来的数量和总价
@Subscribe
public void onMessageEvent(CountPrice event) {
qujiesuan.setText("去结算{" + event.getNum() + "}");
price.setText("¥" + event.getPrice());
}
@Override
public void onDestroy() {
super.onDestroy();
//注销EventBus
if(EventBus.getDefault().isRegistered(this)){
EventBus.getDefault().unregister(this);
}
}
}
最后是Adapter适配器里的代码
public class Sadaptrer extends BaseExpandableListAdapter {
private List<GoWuBean.DataBean> mList;
private Context mContext;
public Sadaptrer(List<GoWuBean.DataBean> goWuBeans, Context context) {
mList = goWuBeans;
mContext = context;
}
@Override
public int getGroupCount() {
return mList==null ?0 :mList.size();
}
@Override
public int getChildrenCount(int i) {
return mList.get(i).getList()==null ? 0:mList.get(i).getList().size();
}
@Override
public View getGroupView(final int i, boolean b, View view, ViewGroup viewGroup) {
GoWuBean.DataBean dataBean = mList.get(i);
final ViewHolderFu vh;
if (view==null){
//
vh=new ViewHolderFu();
view=View.inflate(mContext,R.layout.item,null);
vh.fut1=view.findViewById(R.id.fu_title);
vh.fucheck=view.findViewById(R.id.fu_check);
view.setTag(vh);
}else {
vh= (ViewHolderFu) view.getTag();
}
boolean b1 = ziTiaoMuSelected(i);
vh.fucheck.setChecked(b1);
vh.fut1.setText(dataBean.getSellerName());
vh.fucheck.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//获取当前的条目是否选中
boolean checked = vh.fucheck.isChecked();
//让下面的子条目也选中
ziXuanZong(i,checked);
//使用EventBus进行传值
EventBus.getDefault().post(setJiSuan());
//刷新适配器
notifyDataSetChanged();
}
});
return view;
}
@Override
public View getChildView(final int i, final int i1, boolean b, View view, ViewGroup viewGroup) {
GoWuBean.DataBean dataBean = mList.get(i);
final GoWuBean.DataBean.ShuJu shuJu = dataBean.getList().get(i1);
final ViewHolderZI vh;
if (view==null){
//获取控件
vh=new ViewHolderZI();
view=View.inflate(mContext,R.layout.ziitem,null);
vh.zikan=view.findViewById(R.id.zi_check);
vh.title=view.findViewById(R.id.zi_title);
vh.mDraweeView=view.findViewById(R.id.iv_head_background);
vh.mAddView=view.findViewById(R.id.zi_jiajian);
vh.price=view.findViewById(R.id.zi_price);
vh.hua=view.findViewById(R.id.zi_zeng);
view.setTag(vh);
}else {
vh= (ViewHolderZI) view.getTag();
}
//给控件赋值
vh.zikan.setChecked(shuJu.getSelected()==1);
vh.title.setText(shuJu.getTitle());
vh.price.setText(shuJu.getPrice()+"");
vh.hua.setText(shuJu.getSubhead());
String images = shuJu.getImages();
String strs=images.split("\\|")[0];
vh.mDraweeView.setImageURI(Uri.parse(strs));
vh.mAddView.setNumber(shuJu.getNum());
vh.mAddView.setLinJianTin(new AddView.LinJianTin() {
@Override
public void kanYiKan(int num) {
//点击加减号,加减的时候进行赋值
changeNumber(i,i1,num);
//使用EventBus进行传值
EventBus.getDefault().post(setJiSuan());
notifyDataSetChanged();
}
});
vh.zikan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//mOnCartList.onProduct(i,i1);
//判断当前的条目是否选中
boolean checked = vh.zikan.isChecked();
//选中给一个1否给0
if (checked){
shuJu.setSelected(1);
}else {
shuJu.setSelected(0);
}
//使用EventBus进行传值
EventBus.getDefault().post(setJiSuan());
notifyDataSetChanged();
}
});
return view;
}
//这个方法就是判断子条目是否全部选中
public boolean ziTiaoMuSelected(int i){
GoWuBean.DataBean dataBean = mList.get(i);
List<GoWuBean.DataBean.ShuJu> list = dataBean.getList();
for (GoWuBean.DataBean.ShuJu data:list) {
if (data.getSelected()==0){
return false;
}
}
return true;
}
//判断是否全部选中
public boolean isAllProductsSelected(){
for (int i = 0; i < mList.size(); i++) {
GoWuBean.DataBean dataBean = mList.get(i);
List<GoWuBean.DataBean.ShuJu> list = dataBean.getList();
for (int j = 0; j < list.size(); j++) {
GoWuBean.DataBean.ShuJu shuJu = list.get(j);
if (shuJu.getSelected()==0){
return false;
}
}
}
return true;
}
/**
* 改变全选的状态
*/
private void QuanxuangZT(boolean flag){
Message message = new Message(flag);
EventBus.getDefault().post(message);
}
//计算总量
public CountPrice setJiSuan(){
int number=0;
double price=0;
//这个值是记录你当前状态是否全选,默认为全选
int s=1;
for (int i = 0; i < mList.size(); i++) {
GoWuBean.DataBean dataBean = mList.get(i);
List<GoWuBean.DataBean.ShuJu> list = dataBean.getList();
for (int j = 0; j < list.size(); j++) {
GoWuBean.DataBean.ShuJu shuJu = list.get(j);
if (shuJu.getSelected()==1){
number+=shuJu.getNum();
price+=shuJu.getNum()*shuJu.getPrice();
}else {
s=0;
}
}
}
//判断如果是1全选,否则则不选
if (s==0){
QuanxuangZT(false);
}else {
QuanxuangZT(true);
}
CountPrice countPrice = new CountPrice(price, number);
return countPrice;
}
//点击父条目时让你的字条目全部选中
public void ziXuanZong(int i,boolean bo){
GoWuBean.DataBean dataBean = mList.get(i);
List<GoWuBean.DataBean.ShuJu> list = dataBean.getList();
for (int j = 0; j < list.size(); j++) {
GoWuBean.DataBean.ShuJu shuJu = list.get(j);
shuJu.setSelected(bo ? 1:0);
}
}
//全选
public void changeStatus(boolean bole){
for (int i = 0; i < mList.size(); i++) {
GoWuBean.DataBean dataBean = mList.get(i);
List<GoWuBean.DataBean.ShuJu> list = dataBean.getList();
for (int j = 0; j < list.size(); j++) {
GoWuBean.DataBean.ShuJu shuJu = list.get(j);
shuJu.setSelected(bole?1:0);
}
}
EventBus.getDefault().post(setJiSuan());
}
//点击加减号给数量进行赋值
public void changeNumber(int poist,int po,int number){
GoWuBean.DataBean dataBean = mList.get(poist);
List<GoWuBean.DataBean.ShuJu> list = dataBean.getList();
GoWuBean.DataBean.ShuJu shuJu = list.get(po);
shuJu.setNum(number);
}
class ViewHolderFu{
CheckBox fucheck;
TextView fut1;
}
class ViewHolderZI{
CheckBox zikan;
AddView mAddView;
SimpleDraweeView mDraweeView;
TextView title,price,hua;
}
@Override
public boolean isChildSelectable(int i, int i1) {
return false;
}
@Override
public Object getGroup(int i) {
return null;
}
@Override
public Object getChild(int i, int i1) {
return null;
}
@Override
public long getGroupId(int i) {
return 0;
}
@Override
public long getChildId(int i, int i1) {
return 0;
}
@Override
public boolean hasStableIds() {
return false;
}
}