购物车逻辑+自定义加减器

第一次做购物车的时候,对于单选,全选,没有弄得太明白,后来又做了几次,打算上传一下,有写的不好的,请广大读者提出宝贵意见,本人小贱了一下,将二级列表放到了recyclerview里面

自定义加减器
public class AmountView  extends LinearLayout implements View.OnClickListener, TextWatcher {

    private static final String TAG = "AmountView";
    private int amount = 1; //购买数量
    private int goods_storage = 1; //商品库存

    private OnAmountChangeListener mListener;

    private EditText etAmount;
    public Button btnDecrease;
    public Button btnIncrease;

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

    public AmountView(Context context, AttributeSet attrs) {
        super(context, attrs);

        LayoutInflater.from(context).inflate(R.layout.view_amount, this);
        etAmount = (EditText) findViewById(R.id.etAmount);
        btnDecrease = (Button) findViewById(R.id.btnDecrease);
        btnIncrease = (Button) findViewById(R.id.btnIncrease);
        btnDecrease.setOnClickListener(this);
        btnIncrease.setOnClickListener(this);
        etAmount.addTextChangedListener(this);

        TypedArray obtainStyledAttributes = getContext().obtainStyledAttributes(attrs, R.styleable.AmountView);
        int btnWidth = obtainStyledAttributes.getDimensionPixelSize(R.styleable.AmountView_btnWidth,80);
        int tvWidth = obtainStyledAttributes.getDimensionPixelSize(R.styleable.AmountView_tvWidth, 70);
        int tvTextSize = obtainStyledAttributes.getDimensionPixelSize(R.styleable.AmountView_tvTextSize, 0);
        int btnTextSize = obtainStyledAttributes.getDimensionPixelSize(R.styleable.AmountView_btnTextSize, 0);
        obtainStyledAttributes.recycle();
        LayoutParams btnParams = new LayoutParams(btnWidth, LayoutParams.MATCH_PARENT);
        btnDecrease.setLayoutParams(btnParams);
        btnIncrease.setLayoutParams(btnParams);
        if (btnTextSize != 0) {
            btnDecrease.setTextSize(TypedValue.COMPLEX_UNIT_PX, btnTextSize);
            btnIncrease.setTextSize(TypedValue.COMPLEX_UNIT_PX, btnTextSize);
        }

        LayoutParams textParams = new LayoutParams(tvWidth, LayoutParams.MATCH_PARENT);
        etAmount.setLayoutParams(textParams);
        if (tvTextSize != 0) {
            etAmount.setTextSize(tvTextSize);
        }
    }

    public void setOnAmountChangeListener(OnAmountChangeListener onAmountChangeListener) {
        this.mListener = onAmountChangeListener;
    }

    public void setGoods_storage(int goods_storage) {
        this.goods_storage = goods_storage;
    }

    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.btnDecrease) {
            if (amount > 1){
                amount--;
                etAmount.setText(amount + "");
            }
        } else if (i == R.id.btnIncrease) {
            if (amount < goods_storage) {
                amount++;
                etAmount.setText(amount + "");
            }
        }

        etAmount.clearFocus();

        if (mListener != null) {
            mListener.onAmountChange(this, amount);
        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        if (s.toString().isEmpty())
            return;
        amount = Integer.valueOf(s.toString());
        if (amount > goods_storage) {
            etAmount.setText(goods_storage + "");
            return;
        }

        if (mListener != null) {
            mListener.onAmountChange(this, amount);
        }
    }
    public int getAmount(){
        String string = etAmount.getText().toString();
        return Integer.parseInt(string);
    }
    public void setAmount(int amount){
        this.amount = amount;
        etAmount.setText(amount+"");
    }

    public interface OnAmountChangeListener {
        void onAmountChange(View view, int amount);
    }
}
自定义的attrs文件
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="AmountView">
        <!-- 左右2边+-按钮的宽度 -->
        <attr name="btnWidth" format="dimension" />
        <!-- 中间TextView的宽度 -->
        <attr name="tvWidth" format="dimension" />
        <!--<attr name="tvColor" format="color"/>-->
        <attr name="tvTextSize" format="dimension"/>
        <attr name="btnTextSize" format="dimension"/>
    </declare-styleable>
</resources>
xml文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
   <android.support.v7.widget.RecyclerView
       android:id="@+id/rcv"
       android:layout_width="match_parent"
       android:layout_height="match_parent">
   </android.support.v7.widget.RecyclerView>
   <LinearLayout
       android:layout_alignParentBottom="true"
       android:orientation="horizontal"
       android:layout_width="match_parent"
       android:layout_height="50dp">
       <CheckBox
           android:id="@+id/cb"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content" />
       <Button
           android:text="全选"
           android:id="@+id/quanxuan"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content" />
       <TextView
           android:layout_marginLeft="30dp"
           android:text="合计"
           android:textSize="20sp"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content" />
       <TextView
           android:layout_marginLeft="20dp"
           android:id="@+id/hj"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="text"
           />
       <Button
           android:layout_marginLeft="80dp"
           android:id="@+id/sum"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="去结算"
           />
   </LinearLayout>
</RelativeLayout>
一级item
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <CheckBox
        android:focusable="false"
        android:id="@+id/cb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
   <TextView
       android:layout_marginTop="3dp"
       android:layout_marginLeft="5dp"
       android:textSize="20sp"
       android:text="商家"
       android:id="@+id/shangjia"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content" />
</LinearLayout>
二级item
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <CheckBox
        android:focusable="false"
        android:id="@+id/cb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <ImageView
        android:layout_marginLeft="10dp"
        android:id="@+id/iv"
        android:layout_width="60dp"
        android:layout_height="60dp" />
    <LinearLayout
        android:layout_marginLeft="20dp"
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <TextView
            android:layout_marginTop="10dp"
            android:id="@+id/title"
            android:text="标题"
            android:textSize="15sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <TextView
                android:id="@+id/price"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="price"
                />
            <bwie.com.cartnumlibrary.AmountView
                android:id="@+id/av"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
            </bwie.com.cartnumlibrary.AmountView>
        </LinearLayout>
    </LinearLayout>
</LinearLayout>
二级列表
 <ExpandableListView
      android:id="@+id/elv"
      android:layout_width="match_parent"
      android:layout_height="wrap_content">

  </ExpandableListView>
下面是Java代码,因为我用了recycleview,所以重要的代码放到了
recycleview适配器的onbindviewholder方法里面
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        if(holder instanceof OneViewholder){
            ((OneViewholder) holder).elv.setGroupIndicator(null);

            ((OneViewholder) holder).elv.setAdapter(new BaseExpandableListAdapter() {
                @Override
                public int getGroupCount(){
                    return car.getData().size();
                }
                @Override
                public int getChildrenCount(int i) {
                    return car.getData().get(i).getList().size();
                }
                @Override
                public Object getGroup(int i) {
                    return car.getData().get(i);
                }
                @Override
                public Object getChild(int i, int i1) {
                    return car.getData().get(i).getList().get(i1);
                }
                @Override
                public long getGroupId(int i) {
                    return i;
                }
                @Override
                public long getChildId(int i, int i1) {
                    return i1;
                }
                @Override
                public boolean hasStableIds() {
                    return true;
                }
                @Override
                public View getGroupView(final int i, boolean b, View view, ViewGroup viewGroup) {
                    final ViewHolder vh;
                    if(view==null){
                        vh=new ViewHolder();
                        view=View.inflate(context,R.layout.yijiitem,null);
                        vh.cb=view.findViewById(R.id.cb);
                        vh.tv=view.findViewById(R.id.shangjia);
                        view.setTag(vh);
                    }else{
                        vh= (ViewHolder) view.getTag();
                    }
                    vh.tv.setText(car.getData().get(i).getSellerName());
                    Log.i("qqw",car.getData().get(i).getIstrue()+"");


                    //给复选框赋值,这时候复选框是false
                    vh.cb.setChecked(car.getData().get(i).getIstrue());
                    vh.cb.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            //点击事件触发时,复选框状态值改变,把这个时候值放到bean里面
                            car.getData().get(i).setIstrue(vh.cb.isChecked());
                            int price=0;
                            //遍历一级
                            for (int z = 0; z <car.getData().size();z++) {
                                if (car.getData().get(z).getIstrue()) {
                                    //如果一级被选中,二级下都要被选中,遍历二级列表,都赋成选中状态
                                    for (int j = 0; j < car.getData().get(z).getList().size(); j++) {
                                        car.getData().get(z).getList().get(j).setIstrue(true);
                                        //如果二级被选中算价钱
                                        if (car.getData().get(z).getList().get(j).istrue()) {
                                            int num = (int) (car.getData().get(z).getList().get(j).getNum() * car.getData().get(z).getList().get(j).getPrice());
                                            price = price + num;
                                        }
                                    }
                                } else {
                                    for (int j = 0; j < car.getData().get(z).getList().size(); j++) {
                                        car.getData().get(z).getList().get(j).setIstrue(false);
                                    }
                                }
                            }
                            //将总价格发送出去
                            EventBus.getDefault().post(new Price(price));
                            notifyDataSetChanged();
                        }
                    });

                    return view;
                }
                @Override
                public View getChildView(final int i, final int i1, boolean b, View view, ViewGroup viewGroup) {
                    final ViewHolder1 vh1;
                    if(view==null){
                        vh1=new ViewHolder1();
                        view=View.inflate(context,R.layout.erjiitem,null);
                        vh1.cb=view.findViewById(R.id.cb);
                        vh1.iv=view.findViewById(iv);
                        vh1.tv=view.findViewById(R.id.title);
                        vh1.tv1=view.findViewById(price);
                        vh1.av=view.findViewById(av);
                        view.setTag(vh1);
                    }else{
                        vh1= (ViewHolder1) view.getTag();
                    }
                    //由于本人找的接口,里面的图片地址需要拆分,所以拆分一下
                    String images = car.getData().get(i).getList().get(i1).getImages();
                    String[] split = images.split("\\|");
                    Picasso.with(context).load(split[0]).placeholder(R.mipmap.ic_launcher).into(vh1.iv);
                    vh1.tv.setText(car.getData().get(i).getList().get(i1).getTitle());
                    vh1.tv1.setText(car.getData().get(i).getList().get(i1).getPrice()+"");
                    //给自定义加减器设置最大值
                    vh1.av.setGoods_storage(100);
                    //将值付给自定义加减器
                    vh1.av.setAmount(car.getData().get(i).getList().get(i1).getNum());
                    //点击减号的监听事件
                    vh1.av.btnDecrease.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            //取得加减器里面的值
                            int amount = vh1.av.getAmount();
                            if(amount>1){
                                amount--;
                            }
                            //值变化之后要赋给加减器
                            vh1.av.setAmount(amount);
                            //也要把值赋给bean类
                            car.getData().get(i).getList().get(i1).setNum(vh1.av.getAmount());
                            //刷新适配器
                            notifyDataSetChanged();
                        }
                    });
                    vh1.av.btnIncrease.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            int amount = vh1.av.getAmount();
                            if(amount<100){
                                amount++;
                            }
                            vh1.av.setAmount(amount);
                            car.getData().get(i).getList().get(i1).setNum(vh1.av.getAmount());
                            notifyDataSetChanged();
                            Log.i("111",vh1.av.getAmount()+"");

                        }
                    });

                    vh1.cb.setChecked(car.getData().get(i).getList().get(i1).istrue());
                    //二级控制一级
                    vh1.cb.setOnClickListener(new View.OnClickListener(){
                        @Override
                        public void onClick(View view) {
                            //把此时二级列表的状态赋值到bean里面
                            car.getData().get(i).getList().get(i1).setIstrue(vh1.cb.isChecked());
                            int z=0;
                            for (int j = 0; j <car.getData().get(i).getList().size() ; j++) {
                               if(car.getData().get(i).getList().get(j).istrue()){
                                    z++;
                                }
                            }
                            if(z==car.getData().get(i).getList().size()){
                                car.getData().get(i).setIstrue(true);
                                Log.i("123",123+"");
                            }else{
                                car.getData().get(i).setIstrue(false);
                                Log.i("1234",1235+"");
                            }
                            //算价钱
                            int num=0;
                            int price=0;
                            for (int j = 0; j <car.getData().size();j++) {
                                for (int k = 0; k <car.getData().get(j).getList().size() ; k++) {
                                    if(car.getData().get(j).getList().get(k).istrue()){
                                        num= (int) (car.getData().get(j).getList().get(k).getNum()*car.getData().get(j).getList().get(k).getPrice());
                                        price=price+num;
                                    }
                                }
                            }
                            //发送到需要的页面
                            EventBus.getDefault().post(new Price(price));
                            Log.i("qqq9090",car.getData().get(i).getIstrue()+"");
                            notifyDataSetChanged();
                        }
                    });

                    return view;
                }
                @Override
                public boolean isChildSelectable(int i, int i1) {
                    return true;
                }
                class ViewHolder{
                    CheckBox cb;
                    TextView tv;
                }
                class ViewHolder1{
                    CheckBox cb;
                    ImageView iv;
                    TextView tv,tv1;
                    AmountView av;
                }
            });
            //二级列表默认展开
            int count = (((OneViewholder) holder).elv).getCount();
            for (int j = 0; j<count; j++) {
                ((OneViewholder) holder).elv.expandGroup(j);
            }
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值