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

//布局页面



    


        
        
    

    
    

    
    

        

        

        

        
//加减器布局




    

    

    

//购物车父类布局





    

    

//购物车子类布局









    

        
    

        
    


        

            
            
        
    

 

//加减按钮自定义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 seller;

    public MyAdapter(List 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 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 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 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 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 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 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 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 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 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  
 

你可能感兴趣的:(仿京东购物车适配器(二级列表))