仿京东购物车适配器(二级列表)

//布局页面
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        >


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="购物车"
            android:textSize="30sp"
            android:layout_gravity="center_horizontal"
            />
        <View
            android:layout_width="match_parent"
            android:layout_height="0.75dp"
            android:background="#000"
            />
    </LinearLayout>

    <ExpandableListView
        android:id="@+id/cart_expand"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="60dp"
        android:layout_marginTop="45dp"
        >
    </ExpandableListView>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        >
    <CheckBox
        android:id="@+id/cart_quanxuan"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="全选"
            />

        <TextView
            android:id="@+id/cart_totalPrice"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="总价格:¥"
            android:layout_marginLeft="25dp"
            />

        <TextView
            android:id="@+id/cart_he"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="总数量:"
            android:layout_marginLeft="50dp"
            />

        <Button
            android:id="@+id/cart_buy"
            android:layout_width="80dp"
            android:layout_height="50dp"
            android:layout_marginLeft="55dp"
            android:text="去结算"
            />
    </LinearLayout>

</RelativeLayout>
//加减器布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="60dp"
    android:layout_height="30sp"
    android:layout_gravity="center_vertical"
    android:gravity="center_horizontal">


    <TextView
        android:id="@+id/jian"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="-"
        android:textSize="25dp"
        />

    <TextView
        android:id="@+id/product_num"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="1"
        android:textSize="25dp"
        />

    <TextView
        android:id="@+id/jia"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="+"
        android:textSize="25dp"
        />
</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="60dp"
    android:layout_gravity="center_vertical"
    >


    <CheckBox
        android:id="@+id/cart_parent_seller_cb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

    <TextView
        android:id="@+id/cart_parent_seller_name_tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="文字"
        />
</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="120dp"
    android:layout_gravity="center_vertical"
    >


<CheckBox
    android:id="@+id/cart_child_cb"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    />

<com.facebook.drawee.view.SimpleDraweeView
    android:id="@+id/cart_child_img"
    android:layout_width="80dp"
    android:layout_height="80dp"
    android:background="@mipmap/ic_launcher"
    />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        >

        <TextView
            android:id="@+id/cart_child_title_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="商品标题"
            />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/cart_child_price"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="¥0.0"
            />
    </LinearLayout>


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <com.bw.yuekao_demo.mvp.cart.view.activity.MySubAdd
                android:id="@+id/cart_child_my_sub_add"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
            </com.bw.yuekao_demo.mvp.cart.view.activity.MySubAdd>
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

 

//加减按钮自定义view

public class MySubAdd extends LinearLayout {


    @BindView(R.id.jian)
    TextView jian;
    @BindView(R.id.product_num)
    TextView productNum;
    @BindView(R.id.jia)
    TextView jia;

    int number=0;

    public MySubAdd(Context context) {
        this(context, null);
    }

    public MySubAdd(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        View view = View.inflate(getContext(), R.layout.mysubadd, this);
        //初始化
        ButterKnife.bind(view);
    }


    @OnClick({R.id.jian, R.id.jia})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.jian:
               if (number>1){
                   --number;
                   productNum.setText(number+"");
                   if (onNumChangeListener!=null){
                       onNumChangeListener.onSubNumClickChange(number);
                   }
               }else{
                   Toast.makeText(getContext(),"数量最少为1",Toast.LENGTH_SHORT).show();
               }

                break;
            case R.id.jia:
                ++number;
                    productNum.setText(number+"");
                    if (onNumChangeListener!=null){
                        onNumChangeListener.onAddNumClickChange(number);
                    }
                break;
        }
    }

    public void setNumber(int number) {
        this.number = number;
    }

    onNumChangeListener onNumChangeListener;

    public void setOnNumChangeListener(MySubAdd.onNumChangeListener onNumChangeListener) {
        this.onNumChangeListener = onNumChangeListener;
    }

    public interface onNumChangeListener{
        void onAddNumClickChange(int productNum);
        void onSubNumClickChange(int productNum);
    }
}
//购物车适配器

public class MyAdapter extends BaseExpandableListAdapter {

    private List<CartBean.DataBean> seller;

    public MyAdapter(List<CartBean.DataBean> seller) {
        this.seller = seller;
    }

    @Override
    public int getGroupCount() {
        return seller == null ? 0 : seller.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return seller.get(groupPosition).getList().size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return seller.get(groupPosition);
    }

    @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 false;
    }

    @Override
    public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

        CartBean.DataBean dataBean = seller.get(groupPosition);
        ParentHolder parentHolder;
        if (convertView == null) {
            convertView = View.inflate(parent.getContext(), R.layout.cart_parent, null);
            parentHolder = new ParentHolder(convertView);
            convertView.setTag(parentHolder);
        } else {
            parentHolder = (ParentHolder) convertView.getTag();
        }

        //赋值
        parentHolder.cartParentSellerNameTv.setText(dataBean.getSellerName());

        boolean b = productStatus(groupPosition);
        parentHolder.cartParentSellerCb.setChecked(b);

        parentHolder.cartParentSellerCb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onCartListChangeListener != null) {
                    onCartListChangeListener.SellerCheckChange(groupPosition);
                }
            }
        });
        return convertView;
    }

    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        CartBean.DataBean.ListBean listBean = seller.get(groupPosition).getList().get(childPosition);
        ChildHolder childHolder;
        if (convertView == null) {
            convertView = View.inflate(parent.getContext(), R.layout.cart_child, null);
            childHolder = new ChildHolder(convertView);
            convertView.setTag(childHolder);
        } else {
            childHolder = (ChildHolder) convertView.getTag();
        }
        //赋值
        childHolder.cartChildTitleTv.setText(listBean.getTitle());
        childHolder.cartChildPrice.setText(listBean.getPrice() + "");
        String[] pic = listBean.getImages().split("\\|");
        Uri uri = Uri.parse(pic[0]);
        childHolder.cartChildImg.setImageURI(uri);

        boolean flag = listBean.getSelected() == 1 ? true : false;
        childHolder.cartChildCb.setChecked(flag);
        //点击事件
        childHolder.cartChildCb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onCartListChangeListener != null) {
                    onCartListChangeListener.onProductCheckedChange(groupPosition, childPosition);
                }
            }
        });
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }

    //关于商家按钮
    public boolean productStatus(int groupPosition) {
        List<CartBean.DataBean.ListBean> list = seller.get(groupPosition).getList();
        for (CartBean.DataBean.ListBean listBean : list) {
            if (listBean.getSelected() == 0) {
                return false;
            }
        }
        return true;
    }

    //子类按钮跟着改变
    public void noProductStatus(int groupPosition, boolean b) {
        List<CartBean.DataBean.ListBean> list = seller.get(groupPosition).getList();
        for (int i = 0; i < list.size(); i++) {
            list.get(i).setSelected(b ? 1 : 0);
        }
    }

    //判断是否全部选中
    public boolean isAllProductSeleted() {
        for (int i = 0; i < seller.size(); i++) {
            List<CartBean.DataBean.ListBean> list = seller.get(i).getList();
            for (int j = 0; j < list.size(); j++) {
                if (list.get(j).getSelected() == 0) {
                    return false;
                }
            }
        }
        return true;
    }

    //改变当前商品状态
    public void changeCurentProductStatus(int groupPosition, int childPosition) {

        List<CartBean.DataBean.ListBean> list = seller.get(groupPosition).getList();
        CartBean.DataBean.ListBean listBean = list.get(childPosition);
        listBean.setSelected(listBean.getSelected() == 0 ? 1 : 0);
    }

    //底部全选按钮
    public void changeAllProductStatus(boolean selected) {
        for (int i = 0; i < seller.size(); i++) {
            List<CartBean.DataBean.ListBean> list = seller.get(i).getList();
            for (int j = 0; j < list.size(); j++) {
                list.get(j).setSelected(selected ? 1 : 0);
            }
        }
    }

    //总价格
    public float calcuteTotalPrice() {
        float totalPrice = 0;
        for (int i = 0; i < seller.size(); i++) {
            List<CartBean.DataBean.ListBean> list = seller.get(i).getList();
            for (int j = 0; j < list.size(); j++) {
                if (list.get(j).getSelected() == 1) {
                    float bargainPrice = list.get(j).getBargainPrice();
                    int num = list.get(j).getNum();
                    totalPrice += num * bargainPrice;
                }
            }
        }
        return totalPrice;
    }


    //总数量

    public int calcuteTotalNum() {
        int totalNum = 0;
        for (int i = 0; i < seller.size(); i++) {
            List<CartBean.DataBean.ListBean> list = seller.get(i).getList();
            for (int j = 0; j < list.size(); j++) {
                if (list.get(j).getSelected() == 1) {
                    int num = list.get(j).getNum();
                    totalNum += num;
                }
            }
        }
        return totalNum;
    }


    static class ParentHolder {
        @BindView(R.id.cart_parent_seller_cb)
        CheckBox cartParentSellerCb;
        @BindView(R.id.cart_parent_seller_name_tv)
        TextView cartParentSellerNameTv;

        ParentHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }

    static class ChildHolder {
        @BindView(R.id.cart_child_cb)
        CheckBox cartChildCb;
        @BindView(R.id.cart_child_img)
        SimpleDraweeView cartChildImg;
        @BindView(R.id.cart_child_title_tv)
        TextView cartChildTitleTv;
        @BindView(R.id.cart_child_price)
        TextView cartChildPrice;
        @BindView(R.id.cart_child_my_sub_add)
        MySubAdd cartChildMySubAdd;

        ChildHolder(View view) {
            ButterKnife.bind(this, view);
        }


    }


    onCartListChangeListener onCartListChangeListener;

    public void setOnCartListChangeListener(MyAdapter.onCartListChangeListener onCartListChangeListener) {
        this.onCartListChangeListener = onCartListChangeListener;
    }

    //接口回调
    public interface onCartListChangeListener {
        void SellerCheckChange(int groupPosition);

        void onProductCheckedChange(int groupPosition, int childPosition);

        void onProductNumberChange(int groupPosition, int childPosition, int number);

    }

}

 

//下方为主页面

public class CartFragment extends BaseFragment<CartPresenter> implements CartView{


    private TextView tv_he;
    private ExpandableListView expand;
    private Button btn_buy;
    private CheckBox quanxuan;
    private TextView tv_total_price;
    private MyAdapter myAdapter;
    float totalPrice;
    @Override
    protected void initListener() {

        quanxuan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //底部全选按钮
                boolean allProductSeleted = myAdapter.isAllProductSeleted();
                myAdapter.changeAllProductStatus(!allProductSeleted);
                //适配器刷新
                myAdapter.notifyDataSetChanged();
                reRefreshSelectedAndTotalAllNumber();
            }
        });
    }

    @Override
    protected void initData() {
        presenter.getCart(156);
    }

    @Override
    protected void initViews(View view) {
        expand = view.findViewById(R.id.cart_expand);
        tv_he = view.findViewById(R.id.cart_he);
        btn_buy = view.findViewById(R.id.cart_buy);
        quanxuan = view.findViewById(R.id.cart_quanxuan);
        tv_total_price = view.findViewById(R.id.cart_totalPrice);
    }

    @Override
    protected CartPresenter provide() {
        return new CartPresenter(this);
    }

    @Override
    protected int provId() {
        return R.layout.cartfragment;
    }

    @Override
    public void onCartSuccess(CartBean cartBean) {
        List<CartBean.DataBean> data = cartBean.getData();
        myAdapter = new MyAdapter(data);
        expand.setAdapter(myAdapter);

        //设置点击监听
            myAdapter.setOnCartListChangeListener(new MyAdapter.onCartListChangeListener() {
                @Override
                public void SellerCheckChange(int groupPosition) {
                    //关于商家按钮
                    boolean b=myAdapter.productStatus(groupPosition);
                    //子类按钮跟着改变
                    myAdapter.noProductStatus(groupPosition,!b);
                    //适配器刷新
                    myAdapter.notifyDataSetChanged();
                    reRefreshSelectedAndTotalAllNumber();
                }

                @Override
                public void onProductCheckedChange(int groupPosition, int childPosition) {
                   //改变当前商品状态
                    myAdapter.changeCurentProductStatus(groupPosition,childPosition);
                    //适配器刷新
                    myAdapter.notifyDataSetChanged();
                    reRefreshSelectedAndTotalAllNumber();
                }

                @Override
                public void onProductNumberChange(int groupPosition, int childPosition, int number) {

                }
            });
        //展开二级列表
        for (int i = 0; i <data.size() ; i++) {
            expand.expandGroup(i);
        }
    }

    @Override
    public void onCartFaild(String error) {
        Log.d("cart_error", "onCartFaild: "+error);
    }


   public void reRefreshSelectedAndTotalAllNumber(){
    //判断是否全部选中
       boolean allProductSelected=myAdapter.isAllProductSeleted();
       quanxuan.setChecked(allProductSelected);
       //设置总数量
       int totalNum=myAdapter.calcuteTotalNum();
       tv_he.setText("总数量为:"+totalNum+"");
       //设置总价格
       totalPrice=myAdapter.calcuteTotalPrice();
       tv_total_price.setText("总价格¥:"+totalPrice+"");
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值