仿购物车

首先看看效果在这里插入图片描述
这个就是一个效果,然后来实现它吧
,首先还是看看布局吧.

<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"
    tools:context=".MainActivity"
    android:orientation="vertical">

    <ExpandableListView
        android:id="@+id/expnd"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="9.5"
        />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_alignParentBottom="true"
        android:background="#eeeeee"
        android:gravity="center_vertical">

        <CheckBox
            android:id="@+id/check"
            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/tv_suoy_price"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:paddingLeft="20dp"
            android:text="合计:¥0.00" />

        <Button
            android:id="@+id/btn_jisuan"
            android:layout_width="100dp"
            android:layout_height="match_parent"
            android:text="去结算(0)" />
    </LinearLayout>
</LinearLayout>


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:padding="2dp"
    android:layout_marginLeft="10dp"
    android:layout_width="60dp"
    android:layout_height="30dp"
    android:layout_gravity="center_vertical"
    android:background="#99000000"
    android:gravity="center_vertical">

    <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:layout_marginLeft="2dp"
        android:background="#ffffff"
        android:layout_weight="1"
        android:id="@+id/add_tv"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        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"
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:gravity="center_vertical"
    android:descendantFocusability="blocksDescendants"
    >


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

    <TextView
        android:id="@+id/it_t1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#000000"
        android:layout_marginLeft="28dp" />
</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:gravity="center_vertical">

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

    <ImageView
        android:id="@+id/itz_iamge"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_marginLeft="20dp"
        android:scaleType="centerCrop"
        android:src="@color/colorPrimary" />

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

        <TextView
            android:id="@+id/itz_t1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ellipsize="end"
            android:maxLines="2"
            android:text="商品标题" />

        <TextView
            android:id="@+id/itz_t2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:text="¥0.0" />
    </LinearLayout>

    <bwie.com.gouwuche.JiaJian
        android:id="@+id/add_remove_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="10dp" />
</LinearLayout>

这里呢我布局就全放里面了。然后看看组合式控件:

    private TextView sub_tv;
    private TextView product_number_tv;
    private TextView add_tv;
    private int number=1;
    public JiaJian(Context context) {
        this(context, null);
    }

    public JiaJian(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public JiaJian(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        //获取一个view的对象,切记最后为this
        View view = View.inflate(getContext(), R.layout.add_and_remoeve, this);
        //利用View对象获取控件的id
        add_tv= view.findViewById(R.id.add_tv);
        sub_tv= view.findViewById(R.id.sub_tv);
        product_number_tv= view.findViewById(R.id.product_number_tv);
        add_tv.setOnClickListener(this);
        sub_tv.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            //对商品数量进行判断,在进行接口回调
            case R.id.add_tv:
                if (number<=100){
                    number++;
                    product_number_tv.setText(number+"");
                    if (mLinJianTin!=null){
                        mLinJianTin.kanYiKan(number);
                    }
                }else {
                    Toast.makeText(getContext(),"最大为100件",Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.sub_tv:
                if (number>1){
                    number--;
                    product_number_tv.setText(number+"");
                    if (mLinJianTin!=null){
                        mLinJianTin.kanYiKan(number);
                    }
                }else {
                    Toast.makeText(getContext(),"最少为一",Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
        product_number_tv.setText(number+"");
    }
    //自定义接口回调
    public interface LinJianTin{
        void kanYiKan(int num);
    }
    private LinJianTin mLinJianTin;

    public void setLinJianTin(LinJianTin linJianTin) {
        mLinJianTin = linJianTin;
    }
}

这就是自定义控件里面的代码,然后我们在看看适配去里面的代码


import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.List;

import bwie.com.gouwuche.JiaJian;
import bwie.com.gouwuche.R;
import bwie.com.gouwuche.bean.GoBean;
import bwie.com.gouwuche.bean.Http2http;

/**
 * date:2018/11/20
 * author:李晓亮:Shinelon
 * function:
 */
public class MAdapter extends BaseExpandableListAdapter {
    private List<GoBean.DataBean> mList;
    private Context mContext;

    public MAdapter(List<GoBean.DataBean> list, Context context) {
        mList = list;
        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) {
        ViewHolderParent vhp;
        if (view == null) {
            vhp=new ViewHolderParent();
            view = View.inflate(mContext, R.layout.item, null);
            vhp.mch= view.findViewById(R.id.it_cb);
            vhp.t1= view.findViewById(R.id.it_t1);
            view.setTag(vhp);
        }else {
            vhp= (ViewHolderParent) view.getTag();
        }
        //商家名称
        vhp.t1.setText(mList.get(i).getSellerName());
        boolean b1 = ziTiaoMuSelected(i);
        vhp.mch.setChecked(b1);
        vhp.mch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mOnCartList!=null){
                    mOnCartList.onSeller(i);
                }
            }
        });
        return view;
    }

    @Override
    public View getChildView(final int i, final int i1, boolean b, View view, ViewGroup viewGroup) {
        final GoBean.DataBean.ListBean listBean = mList.get(i).getList().get(i1);
        ViewHolder vh;
        if (view == null) {
            vh=new ViewHolder();
            view = View.inflate(mContext, R.layout.itemzi, null);
            vh.mc1= view.findViewById(R.id.itz_cb);
            vh.t1= view.findViewById(R.id.itz_t1);
            vh.t2= view.findViewById(R.id.itz_t2);
            vh.mJian= view.findViewById(R.id.add_remove_view);
            vh.imageView=view.findViewById(R.id.itz_iamge);
            view.setTag(vh);
        }else {
            vh= (ViewHolder) view.getTag();
        }
        vh.t1.setText(listBean.getTitle());
        vh.t2.setText(listBean.getPrice()+"");
        vh.mc1.setChecked(listBean.getSelected()==1);
        String images = listBean.getImages();
        Glide.with(mContext).load(images.split("\\|")[0]).into(vh.imageView);
        vh.mJian.setNumber(listBean.getNum());
        vh.mJian.setLinJianTin(new JiaJian.LinJianTin() {
            @Override
            public void kanYiKan(int num) {
                if (mOnCartList!=null){
                    mOnCartList.onNumber(i,i1,num);
                    notifyDataSetChanged();
                }
            }
        });
        vh.mc1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mOnCartList!=null){
                    mOnCartList.onProduct(i,i1);
                }
            }
        });
        return view;
    }
    //这个方法就是判断子条目是否全部选中
    public boolean ziTiaoMuSelected(int i){
        GoBean.DataBean dataBean = mList.get(i);
        List<GoBean.DataBean.ListBean> list = dataBean.getList();
        for (GoBean.DataBean.ListBean data:list) {
            if (data.getSelected()==0){
                return false;
            }

        }
        return true;
    }
    //全选判断所有单选框是否为选中,不是则返回false
    public boolean isAllProductsSelected(){
        for (int i = 0; i <mList.size() ; i++) {
            GoBean.DataBean dataBean = mList.get(i);
            List<GoBean.DataBean.ListBean> list = dataBean.getList();
            for (int j = 0; j <list.size() ; j++) {
                GoBean.DataBean.ListBean listBean = list.get(j);
                if (listBean.getSelected()==0){
                    return false;
                }
            }
        }
        return true;
    }
    //计算选中的总量
    public int TotalNumber(){
        int number=0;
        for (int i = 0; i <mList.size() ; i++) {
            GoBean.DataBean dataBean = mList.get(i);
            List<GoBean.DataBean.ListBean> list = dataBean.getList();
            for (int j = 0; j <list.size() ; j++) {
                GoBean.DataBean.ListBean listBean = list.get(j);
                if (listBean.getSelected()==1){
                    number+=listBean.getNum();
                }
            }
        }
        return number;
    }
    //计算选中的总价
    public float TotalPrice(){
        int price=0;
        for (int i = 0; i <mList.size() ; i++) {
            GoBean.DataBean dataBean = mList.get(i);
            List<GoBean.DataBean.ListBean> list = dataBean.getList();
            for (int j = 0; j <list.size() ; j++) {
                GoBean.DataBean.ListBean listBean = list.get(j);
                if (listBean.getSelected()==1){
                    price+=listBean.getPrice()*listBean.getNum();
                }
            }
        }
        return price;
    }
    //点击父条目时,把子条目设为选中
    public void ziXuanZong(int i,boolean xuan){
        GoBean.DataBean dataBean = mList.get(i);
        List<GoBean.DataBean.ListBean> list = dataBean.getList();
        for (int j = 0; j <list.size() ; j++) {
            GoBean.DataBean.ListBean listBean = list.get(j);
            listBean.setSelected(xuan ? 1:0);
        }
    }
    //子条目选中时,父条目设为true
    public void fuXuanZhong(int i,int po){
        GoBean.DataBean dataBean = mList.get(i);
        List<GoBean.DataBean.ListBean> list = dataBean.getList();
        GoBean.DataBean.ListBean listBean = list.get(po);
        listBean.setSelected(listBean.getSelected()==0?1:0);
    }
    //应该是让所有商品为false
    public void changeStatus(boolean bole){
        for (int i = 0; i <mList.size() ; i++) {
            GoBean.DataBean dataBean = mList.get(i);
            List<GoBean.DataBean.ListBean> list = dataBean.getList();
            for (int j = 0; j <list.size() ; j++) {
                GoBean.DataBean.ListBean listBean = list.get(j);
                //这看着像是反选
                listBean.setSelected(bole?1:0);
            }
        }
    }
    //C.当加减器被点击时,调用,改变里面当前商品的数量  参数1定位那个商家   参数2定位哪个商品  参数3定位改变具体的数量是多少
    public void changeNumber(int poist,int po,int number){
        GoBean.DataBean dataBean = mList.get(poist);
        List<GoBean.DataBean.ListBean> list = dataBean.getList();
        GoBean.DataBean.ListBean listBean = list.get(po);
        listBean.setNum(number);
    }
    public interface onCartList{
        /**
         * 当商家的checkBox点击时回调
         */
        void onSeller(int groupPosition);

        /**
         * 当点击子条目商品的CheckBox回调
         */
        void onProduct(int groupPosition ,int childPosition);

        /**
         * 当点击加减按钮的回调
         */
        void onNumber(int groupPosition , int childPosition , int number);

    }
    onCartList mOnCartList;

    public void setOnCartListChangeListener(onCartList onCartListChangeListener) {
        mOnCartList = onCartListChangeListener;
    }

    class ViewHolderParent {
        TextView t1;
        CheckBox mch;
    }

    class ViewHolder {
        JiaJian mJian;
        TextView t1, t2;
        CheckBox mc1;
        ImageView imageView;
    }

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


    @Override
    public boolean isChildSelectable(int i, int i1) {
        return false;
    }
}

至于网络请求和bean类就不发了。
最后就是activity

    private String request="http://www.zhaoapi.cn/product/getCarts";
    private ExpandableListView expnd;
    private CheckBox check;
    private TextView tv_suoy_price;
    private Button btn_jisuan;
    private MAdapter mMAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        //获取控件
        expnd =  findViewById(R.id.expnd);
        check =  findViewById(R.id.check);
        tv_suoy_price =  findViewById(R.id.tv_suoy_price);
        btn_jisuan =  findViewById(R.id.btn_jisuan);
        //网络请求数据
        GouChu gouChu = new GouChu();
        //定义一个map集合,进行post请求
        HashMap<String ,String> map= new HashMap<>();
        map.put("uid","71");
        gouChu.Diao(request,map);
        gouChu.setKuai(new GouChu.Kuai() {
            @Override
            public void success(List<GoBean.DataBean> data) {
                mMAdapter = new MAdapter(data, getApplicationContext());
                //设置一个监听
                mMAdapter.setOnCartListChangeListener(new MAdapter.onCartList() {
                    //当商家的CheckBox点击
                    @Override
                    public void onSeller(int groupPosition) {
                        //判断子条目是否为选中
                        boolean b = mMAdapter.ziTiaoMuSelected(groupPosition);
                        //让你的子条目为选中
                        mMAdapter.ziXuanZong(groupPosition,!b);
                        //刷新
                        mMAdapter.notifyDataSetChanged();
                        numZong();
                    }
                    //当点击子条目商品的CheckBox回调
                    @Override
                    public void onProduct(int groupPosition, int childPosition) {
                        //子条目全部选中时,父条目为true
                        mMAdapter.fuXuanZhong(groupPosition,childPosition);
                        //刷新
                        mMAdapter.notifyDataSetChanged();
                        numZong();
                    }
                    //当点击加减按钮的回调
                    @Override
                    public void onNumber(int groupPosition, int childPosition, int number) {
                        //加减器的方法
                        mMAdapter.changeNumber(groupPosition,childPosition,number);
                        //刷新
                        mMAdapter.notifyDataSetChanged();
                        numZong();
                    }
                });
                //添加适配器
                expnd.setAdapter(mMAdapter);
                //展开子条目
                for(int x=0; x<data.size(); x++){
                    expnd.expandGroup(x);
                }
                //刷新
                numZong();
            }
        });
        check.setOnClickListener(this);
    }
    private void numZong(){
        //判断商品是否为选中
        boolean b = mMAdapter.isAllProductsSelected();
        //给全选设置值,这里一般为false毕竟一般人的思维没那么坑爹
        check.setChecked(b);
        //获取总价
        float price= mMAdapter.TotalPrice();
        tv_suoy_price.setText(price+"");
        //获取总数量
        int number = mMAdapter.TotalNumber();
        btn_jisuan.setText("去结算("+number+")");
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //全选方法或者说是反选方法
            case R.id.check:
                //如果有未选中的就全选,如果全部选中则反选
                boolean b = mMAdapter.isAllProductsSelected();
                mMAdapter.changeStatus(!b);
                mMAdapter.notifyDataSetChanged();
                numZong();
                break;
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值