一级购物车

布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >
    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycle_shop"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="8"
        >
    </android.support.v7.widget.RecyclerView>
<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="#000"
    />
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        >
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            >
            <CheckBox
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:id="@+id/Qcheck"
                android:text="全选"
                />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="合计:¥0"
                android:textColor="#000"
                android:textSize="14dp"
                android:id="@+id/zong"
                />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="总数量:0"
                android:textColor="#000"
                android:textSize="14dp"
                android:id="@+id/shu"
                />
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="去结算"
                android:background="#f00"
                android:layout_marginLeft="250dp"
                />
        </LinearLayout>

    </LinearLayout>

</LinearLayout>

activity

public class ShopCarFragment extends Fragment implements IBaseView {
    private Presenter presenter;
    private RecyclerView recycle_shop;
    private CheckBox Qcheck;
    private List<ShopCarBean.ResultBean> list;
    private ShopCarAdapter shopCarAdapter;
    private TextView zong;
    private TextView shu;
    private SharedPreferences sp;
    private boolean isGetNet = false;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = View.inflate(container.getContext(), R.layout.shop_layout, null);
        Qcheck = view.findViewById(R.id.Qcheck);
        shu = view.findViewById(R.id.shu);
        zong = view.findViewById(R.id.zong);

        recycle_shop = view.findViewById(R.id.recycle_shop);
        recycle_shop.setLayoutManager(new LinearLayoutManager(getActivity()));
        presenter = new Presenter();
        presenter.setView(this);
        sp = getActivity().getSharedPreferences("id", Context.MODE_PRIVATE);
        String uid = sp.getString("uid", "");
        String sid = sp.getString("sid", "");
        HashMap<String, String> xiangMap = new HashMap<>();
        xiangMap.put("userId", uid + "");
        xiangMap.put("sessionId", sid);
        presenter.getShopCar(xiangMap);
        EventBus.getDefault().register(this);
        Qcheck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                checkAll(isChecked);
            }
        });
        return view;
    }
    @Override
    public Void onSuccess(Object o, int isWhat) {
        if (isWhat == 8) {
            ShopCarBean shopCarBean = (ShopCarBean) o;
            list = shopCarBean.getResult();
            shopCarAdapter = new ShopCarAdapter(getActivity(), list);
            recycle_shop.setAdapter(shopCarAdapter);
            }
        return null;
    }
    @Override
    public Void onErroe(String mess) {
        return null;
    }
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        presenter.dettch();
    }
    //全选反选
    private void checkAll(boolean isChecked) {
            for (int i = 0; i <list.size() ; i++) {
                ShopCarBean.ResultBean resultBean = list.get(i);
                resultBean.setCheck(isChecked);
        }
        shopCarAdapter.notifyDataSetChanged();
            Price();
    }
//总价
    private void Price() {
        int total=0;
        int totalNum=0;
        List<ShopCarBean.ResultBean> result = shopCarAdapter.getResult();
        for (int i = 0; i < result.size(); i++) {
            ShopCarBean.ResultBean resultBean = result.get(i);
            if (resultBean.isCheck()){
                int count = resultBean.getCount();
                totalNum+=count;
                int price = resultBean.getPrice();
                total+=price*count;
            }
        }
        zong.setText("总价"+total+"¥");
        shu.setText("总数"+totalNum+"");
    }
    //数量相加
    @Subscribe
    public void CountSubAdd(CountEvent event){
        int adapterPosition = event.getAdapterPosition();
        List<ShopCarBean.ResultBean> result = shopCarAdapter.getResult();
         ShopCarBean.ResultBean resultBean = result.get(adapterPosition);
         if (resultBean.isCheck()){
              resultBean.setCount(event.getNewNum());
         }
         shopCarAdapter.notifyDataSetChanged();
            Price();
    }
    //单个选中计算价格
    @Subscribe
    public void GoosEvent(GoosEvent event){
        int adapterPosition = event.getAdapterPosition();
        List<ShopCarBean.ResultBean> result = shopCarAdapter.getResult();
        ShopCarBean.ResultBean resultBean = result.get(adapterPosition);
        resultBean.setCheck(event.isGoodsCheck());
        if (!resultBean.isCheck()){
            Qcheck.setChecked(false);
        }
        shopCarAdapter.notifyDataSetChanged();
        Price();
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
        presenter.dettch();
    }
}

```java
adapter

```java
public class ShopCarAdapter extends RecyclerView.Adapter<ShopCarAdapter.ViewHolder> {
    Context context;
    List<ShopCarBean.ResultBean>list;

    public ShopCarAdapter(Context context, List<ShopCarBean.ResultBean> list) {
        this.context = context;
        this.list = list;
    }
    //加减器数据
    public List<ShopCarBean.ResultBean> getResult() {
        return list;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.shopcar_item, viewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
        viewHolder.name.setText(list.get(i).getCommodityName());
        viewHolder.price.setText(list.get(i).getPrice()+"");
        viewHolder.img.setImageURI(list.get(i).getPic());
        viewHolder.quan.setChecked(list.get(i).isCheck());
        final int adapterPosition = viewHolder.getAdapterPosition();
        viewHolder.quan.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                EventBus.getDefault().post(new GoosEvent(adapterPosition,isChecked));
            }
        });

        viewHolder.zdy.setOnclickAdd(new Zdy.OnclickAdd() {
            @Override
            public void onResout(int old, int newnum) {
                EventBus.getDefault().post(new CountEvent(adapterPosition,newnum));
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }
    public class ViewHolder extends RecyclerView.ViewHolder {

        private TextView name;
        private TextView price;
        private CheckBox quan;
        private SimpleDraweeView img;
        private Zdy zdy;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            name = itemView.findViewById(R.id.name);
            price = itemView.findViewById(R.id.price);
            quan = itemView.findViewById(R.id.quan);
            img = itemView.findViewById(R.id.img);
            zdy = itemView.findViewById(R.id.zdy);
        }
    }
}

自定义加减器

public class Zdy extends LinearLayout {
    private TextView jia;
    private TextView jian;
    private TextView num;

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

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

    public Zdy(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(final Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.zdy, this, true);
        jia = view.findViewById(R.id.jia);
        jian = view.findViewById(R.id.jian);
        num = view.findViewById(R.id.num);
        num.setText("1");
        jia.setOnClickListener(new OnClickListener() {
            private int newnum;
            @Override
            public void onClick(View v) {
                String old = num.getText().toString();
                newnum = Integer.parseInt(old);
                newnum++;
                num.setText(newnum+"");
                if (onclickAdd!=null){
                    onclickAdd.onResout(Integer.parseInt(old),newnum);
                } onclickAdd.onResout(Integer.parseInt(old),newnum);
            }
        });
        jian.setOnClickListener(new OnClickListener() {
            String old = num.getText().toString();
            private int newnum;
            @Override
            public void onClick(View v) {
                newnum = Integer.parseInt(num.getText().toString());
                newnum--;
                if (newnum>=1){
                    num.setText(newnum+"");
                }
                if (onclickAdd!=null){
                    onclickAdd.onResout(Integer.parseInt(old),newnum);
                } onclickAdd.onResout(Integer.parseInt(old),newnum);
            }
        });
    }
    public OnclickAdd onclickAdd;
    public interface  OnclickAdd{
        void onResout(int old,int newnum);
    }
    public void setOnclickAdd(OnclickAdd onclickAdd) {
        this.onclickAdd = onclickAdd;
    }
}

子条目布局

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="horizontal"
    app:cardCornerRadius="20dp"
    app:cardElevation="10dp"
    android:layout_margin="10dp">

    <CheckBox
        android:id="@+id/quan"
        android:layout_margin="5dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
    <com.facebook.drawee.view.SimpleDraweeView
        android:id="@+id/img"
        android:layout_margin="50dp"
        android:layout_width="110dp"
        android:layout_height="103dp" />
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_marginLeft="120dp"
        >

        <TextView
            android:id="@+id/name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="40dp"
            android:text="name" />

        <TextView
            android:id="@+id/price"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="40dp"
            android:text="price" />

    </LinearLayout>
    <LinearLayout
        android:layout_marginTop="80dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_marginLeft="200dp"
        >
        <bwie.com.wdsc.net.Zdy
            android:id="@+id/zdy"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
        </bwie.com.wdsc.net.Zdy>

    </LinearLayout>


</android.support.v7.widget.CardView>

event

public class CountEvent {
    private int adapterPosition;
    private int newNum;

    public CountEvent(int adapterPosition, int newNum) {
        this.adapterPosition = adapterPosition;
        this.newNum = newNum;
    }

    public int getAdapterPosition() {
        return adapterPosition;
    }

    public void setAdapterPosition(int adapterPosition) {
        this.adapterPosition = adapterPosition;
    }

    public int getNewNum() {
        return newNum;
    }

    public void setNewNum(int newNum) {
        this.newNum = newNum;
    }
}
public class GoosEvent {
    private int adapterPosition;
    private boolean isGoodsCheck;

    public GoosEvent(int adapterPosition, boolean isGoodsCheck) {
        this.adapterPosition = adapterPosition;
        this.isGoodsCheck = isGoodsCheck;
    }

    public int getAdapterPosition() {
        return adapterPosition;
    }

    public void setAdapterPosition(int adapterPosition) {
        this.adapterPosition = adapterPosition;
    }

    public boolean isGoodsCheck() {
        return isGoodsCheck;
    }

    public void setGoodsCheck(boolean goodsCheck) {
        isGoodsCheck = goodsCheck;
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_43916826/article/details/89847994