详见
https://github.com/YangJun1208/liandong/tree/master/gouwuche

加减器的布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

<ImageView
    android:id="@+id/jian_car"
    android:layout_width="20dp"
    android:layout_height="20dp"
    android:layout_centerVertical="true"
    android:background="@drawable/jian"
    />

<ImageView
    android:id="@+id/add_car"
    android:layout_width="20dp"
    android:layout_height="20dp"
    android:layout_centerVertical="true"
    android:layout_toRightOf="@+id/edit_shop_car1"
    android:background="@drawable/add"
    />

    <EditText
        android:id="@+id/edit_shop_car1"
        android:layout_width="50dp"
        android:layout_height="30dp"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@+id/jian_car"
        android:background="@drawable/home_shop_bg"
        android:gravity="center_horizontal"
        android:inputType="number"
        android:text="1"
        android:textSize="14sp" />
</RelativeLayout>

自定义View的加减器

public class CustomCounterView extends RelativeLayout implements View.OnClickListener {

private Context context;
private EditText editCar;

public CustomCounterView(Context context) {
    super(context);
    init(context);
}

public CustomCounterView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init(context);
}

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

private void init(Context context) {
    this.context=context;
    View view = View.inflate(context, R.layout.shop_car_price_layout, null);
    ImageView addIamge = (ImageView) view.findViewById(R.id.add_car);
    ImageView jianIamge = (ImageView) view.findViewById(R.id.jian_car);
    editCar = (EditText) view.findViewById(R.id.edit_shop_car1);
    addIamge.setOnClickListener(this);
    jianIamge.setOnClickListener(this);
    addView(view);

    editCar.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            //TODO:改变数量

            try {
                num= Integer.valueOf(String.valueOf(s));
                list.get(position).setNum(num);
            }catch (Exception e){
                list.get(position).setNum(0);
            }

            if(listener!=null){
                listener.callBack();
            }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });
}

private int num;
@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.add_car:
            //改变数量,设置数量,改变对象内容,回调,局部刷新
            num++;
            editCar.setText(num + "");
            list.get(position).setNum(num);
            listener.callBack();
            productsAdapter.notifyItemChanged(position);
            break;
        case R.id.jian_car:
            if (num > 1) {
                num--;
            } else {
                Toast.makeText(context, "我是有底线的", Toast.LENGTH_LONG).show();
            }
            editCar.setText(num + "");
            list.get(position).setNum(num);
            listener.callBack();
            productsAdapter.notifyItemChanged(position);
            break;
        default:
            break;
    }
}

//传递的数据
private List<ShopBean.DataBean.ListBean> list = new ArrayList<>();
private int position;
private ProductsAdapter productsAdapter;

public void setData(ProductsAdapter productsAdapter, List<ShopBean.DataBean.ListBean> list, int i) {
    this.list = list;
    this.productsAdapter = productsAdapter;
    position = i;
    num = list.get(i).getNum();
    editCar.setText(num + "");
}


private CallBackListener listener;

public void setOnCallBack(CallBackListener listener) {
    this.listener = listener;
}

public interface CallBackListener {
    void callBack();
}

MainActivity的java代码

public class MainActivity extends AppCompatActivity implements IView,View.OnClickListener {

private RecyclerView recyclerView;
private CheckBox iv_cricle;
private TextView all_price, sum_price_txt;
private ShopAdapter shopAdapter;
private IPersenterImpl iPersenter;
private List<ShopBean.DataBean> mList;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    recyclerView = findViewById(R.id.recycle);
    iv_cricle = findViewById(R.id.iv_cricle);
    iPersenter = new IPersenterImpl(this);
    all_price = findViewById(R.id.all_price);
    sum_price_txt = findViewById(R.id.sum_price_txt);

    iv_cricle.setOnClickListener(this);

    final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
    linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
    recyclerView.setLayoutManager(linearLayoutManager);

    shopAdapter = new ShopAdapter(this);
    recyclerView.setAdapter(shopAdapter);

    shopAdapter.setListener(new ShopAdapter.ShopCallBackListener() {
        @Override
        public void callBack(List<ShopBean.DataBean> list) {
            int num = 0;
            double price = 0;
            int totlaNum = 0;
            for (int a = 0; a < list.size(); a++) {
                List<ShopBean.DataBean.ListBean> beans = list.get(a).getList();
                for (int i = 0; i < beans.size(); i++) {
                    totlaNum= totlaNum + beans.get(i).getNum();
                    if (beans.get(i).isCheck()) {
                        price=price+(beans.get(i).getPrice()*beans.get(i).getNum());
                        num=num+beans.get(i).getNum();
                    }
                }
            }
            if (num < totlaNum) {
                //不是全部选中
                iv_cricle.setChecked(false);
            } else {
                iv_cricle.setChecked(true);
            }
            sum_price_txt.setText("合计:" + price);
            all_price.setText("去结算:(" + num + ")");
        }
    });

    getData();
}

private void getData() {
    Map<String, String> map = new HashMap<>();
    map.put("uid", "71");
    iPersenter.getRequest(Apis.TYPE_TITLE, map, ShopBean.class);
}

@Override
public void onSuccess(Object data) {
    if (data instanceof ShopBean) {
        ShopBean shopBean = (ShopBean) data;
        mList = shopBean.getData();
        if (mList != null) {
            mList.remove(0);
            shopAdapter.setList(mList);
        }
    }
}

@Override
public void onClick(View v) {
    switch (v.getId()) {
        case R.id.iv_cricle:
            checkSeller(iv_cricle.isChecked());
            shopAdapter.notifyDataSetChanged();
            break;
        default:
            break;
    }
}

/**
 * 修改选中状态,获取价格和数量
 */
private void checkSeller(boolean bool) {
    double totalPrice = 0;
    int num = 0;
    for (int a = 0; a < mList.size(); a++) {
        //遍历商家,改变状态
        ShopBean.DataBean dataBean = mList.get(a);
        dataBean.setCheck(bool);

        List<ShopBean.DataBean.ListBean> listAll = mList.get(a).getList();
        for (int i = 0; i < listAll.size(); i++) {
            //遍历商品,改变状态
            listAll.get(i).setCheck(bool);
            totalPrice = totalPrice + (listAll.get(i).getPrice() * listAll.get(i).getNum());
            num = num + listAll.get(i).getNum();
        }
    }
    if (bool) {
        sum_price_txt.setText("合计:" + totalPrice);
        all_price.setText("去结算(" + num + ")");
    } else {
        sum_price_txt.setText("合计:0.00");
        all_price.setText("去结算(0)");
    }

}

MainActivity的布局

<android.support.constraint.ConstraintLayout 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">

<RelativeLayout
    android:id="@+id/layout_top"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:background="@color/colorPrimary">

    <Button
        android:id="@+id/back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="返回"
        android:layout_centerVertical="true"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerInParent="true"
        android:text="购物车"
        android:textColor="#ffffff"
        android:textSize="16sp" />

</RelativeLayout>
<android.support.v7.widget.RecyclerView
    android:id="@+id/recycle"
    app:layout_constraintTop_toBottomOf="@id/layout_top"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

<RelativeLayout
    android:id="@+id/layout_buttom"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    app:layout_constraintBottom_toBottomOf="parent"
    android:background="#ffffff"
    android:paddingLeft="10dp">

    <RelativeLayout
        android:id="@+id/layout_all"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true">

        <CheckBox
            android:id="@+id/iv_cricle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true" />

        <TextView
            android:id="@+id/txt_all"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="5dp"
            android:layout_toRightOf="@+id/iv_cricle"
            android:text="全选/全不选" />
    </RelativeLayout>

    <TextView
        android:id="@+id/all_price"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="5dp"
        android:layout_toRightOf="@+id/layout_all"
        android:layout_toLeftOf="@+id/sum_price"
        android:text="合计:0.00"
        android:textColor="#222222"
        android:textSize="16sp" />

    <RelativeLayout
        android:id="@+id/sum_price"
        android:layout_width="100dp"
        android:layout_height="match_parent"
        android:layout_alignParentRight="true"
        android:background="@color/colorPrimary">

        <TextView
            android:id="@+id/sum_price_txt"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:text="去结算(0)"
            android:textColor="#ffffff" />
    </RelativeLayout>
    </RelativeLayout>

</android.support.constraint.ConstraintLayout>

商家的适配器

public class ShopAdapter extends RecyclerView.Adapter<ShopAdapter.ViewHolder> {

private Context context;
private List<ShopBean.DataBean> mDatas;

public ShopAdapter(Context context) {
    this.context = context;
    mDatas=new ArrayList<>();
}

public void setList(List<ShopBean.DataBean> list) {
    this.mDatas = list;
    notifyDataSetChanged();
}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    View view = View.inflate(context, R.layout.shop_seller_car_adapter, null);
    ViewHolder viewHolder = new ViewHolder(view);
    return viewHolder;
}

@Override
public void onBindViewHolder(@NonNull final ViewHolder viewHolder, final int i) {
    //商家的名字
    viewHolder.textView.setText(mDatas.get(i).getSellerName());

    final ProductsAdapter productsAdapter = new ProductsAdapter(context, mDatas.get(i).getList());
    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
    viewHolder.recyclerView.setLayoutManager(linearLayoutManager);

    viewHolder.recyclerView.setAdapter(productsAdapter);
    //改变商家的选中状态
    viewHolder.checkBox.setChecked(mDatas.get(i).isCheck());

    productsAdapter.setListener(new ProductsAdapter.ShopCallBackListener() {
        @Override
        public void callBack() {
            if(mShopCallBackListener!=null){
                mShopCallBackListener.callBack(mDatas);
            }
            List<ShopBean.DataBean.ListBean> listBeans = mDatas.get(i).getList();
            //创建一个临时的标志位,用来记录现在点击的状态
            boolean isAllCheck=true;

            for (ShopBean.DataBean.ListBean bean:listBeans){
                if(!bean.isCheck()){
                    isAllCheck=false;
                    break;
                }
            }
            //刷新商家的状态
            viewHolder.checkBox.setChecked(isAllCheck);
            mDatas.get(i).setCheck(isAllCheck);
        }
    });

    //监听checkBox的点击事件
    //目的是改变旗下所有商品的选中状态
    viewHolder.checkBox.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //改变自己的一个状态
            mDatas.get(i).setCheck(viewHolder.checkBox.isChecked());
            productsAdapter.selectOrRemoveAll(viewHolder.checkBox.isChecked());
        }
    });
}
@Override
public int getItemCount() {
    return mDatas.size();
}

public class ViewHolder extends RecyclerView.ViewHolder {

    private CheckBox checkBox;
    private TextView textView;
    private RecyclerView recyclerView;

    public ViewHolder(@NonNull View itemView) {

        super(itemView);
        checkBox=itemView.findViewById(R.id.check_shop);
        textView=itemView.findViewById(R.id.tv_shop);
        recyclerView=itemView.findViewById(R.id.recycler_shop);
    }
}

private ShopCallBackListener mShopCallBackListener;

public void setListener(ShopCallBackListener listener) {
    this.mShopCallBackListener = listener;
}

public interface ShopCallBackListener {
    void callBack(List<ShopBean.DataBean> list);
}

商家的布局

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <LinearLayout
    android:id="@+id/item_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <CheckBox
        android:id="@+id/check_shop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="10dp" />

    <TextView
        android:id="@+id/tv_shop"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp" />
</LinearLayout>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_shop"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toBottomOf="@id/item_layout" />

</android.support.constraint.ConstraintLayout>

商品的适配器

public class ProductsAdapter extends RecyclerView.Adapter<ProductsAdapter.ViewHolder> {
private Context context;
private List<ShopBean.DataBean.ListBean> mDatas;

public ProductsAdapter(Context context, List<ShopBean.DataBean.ListBean> mDatas) {
    this.context = context;
    this.mDatas = mDatas;
}

@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    View view = View.inflate(context, R.layout.shop_car_adapter, null);
    ViewHolder holder = new ViewHolder(view);
    return holder;
}

@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, final int i) {

    String url = mDatas.get(i).getImages().split("\\|")[0].replace("https", "http");
    Glide.with(context).load(url).into(viewHolder.iv_product);

    viewHolder.tv_product_title.setText(mDatas.get(i).getTitle());
    viewHolder.tv_product_price.setText(mDatas.get(i).getPrice()+"");

    //根据我记录的状态,改变勾选
    viewHolder.check_product.setChecked(mDatas.get(i).isCheck());
    //商品的跟商家的有所不同,商品添加了选中改变的监听
    viewHolder.check_product.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            //改变自己的状态
            mDatas.get(i).setCheck(isChecked);
            //回调给Acivity,选中的状态被改变了
            if(mShopCallBackListener!=null){
                mShopCallBackListener.callBack();
            }
        }
    });

    //自定义view里的edit
    viewHolder.custom_product_counter.setData(this,mDatas,i);
    viewHolder.custom_product_counter.setOnCallBack(new CustomCounterView.CallBackListener() {
        @Override
        public void callBack() {
            if(mShopCallBackListener!=null){
                mShopCallBackListener.callBack();
            }
        }
    });

}

@Override
public int getItemCount() {
    return mDatas.size();
}

public class ViewHolder extends RecyclerView.ViewHolder {

    private CheckBox check_product;
    private ImageView iv_product;
    private TextView tv_product_title;
    private TextView tv_product_price;
    private CustomCounterView custom_product_counter;

    public ViewHolder(@NonNull View itemView) {
        super(itemView);
        check_product=itemView.findViewById(R.id.check_product);
        iv_product=itemView.findViewById(R.id.iv_product);
        tv_product_price=itemView.findViewById(R.id.tv_product_price);
        tv_product_title=itemView.findViewById(R.id.tv_product_title);
        custom_product_counter=itemView.findViewById(R.id.custom_product_counter);
    }
}

/**
 * 在我们子商品的adapter中,修改子商品的全选和反选
 *
 * @param isSelectAll
 */
public void selectOrRemoveAll(boolean isSelectAll) {
    for (ShopBean.DataBean.ListBean listBean : mDatas) {
        listBean.setCheck(isSelectAll);
    }
    notifyDataSetChanged();
}

private ShopCallBackListener mShopCallBackListener;

public void setListener(ShopCallBackListener listener) {
    this.mShopCallBackListener = listener;
}

public interface ShopCallBackListener {
    void callBack();
}

商品的布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:background="#ffffff">


<CheckBox
    android:id="@+id/check_product"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_marginLeft="10dp" />

<ImageView
    android:id="@+id/iv_product"
    android:layout_width="100dp"
    android:layout_height="80dp"
    android:layout_centerVertical="true"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="10dp"
    android:layout_toRightOf="@+id/check_product" />

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="80dp"
    android:layout_centerVertical="true"
    android:layout_toRightOf="@+id/iv_product">

    <TextView
        android:id="@+id/tv_product_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:lines="2"
        android:textColor="#222222"
        android:textSize="14sp" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_alignParentBottom="true">

        <TextView
            android:id="@+id/tv_product_price"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:textColor="@color/colorPrimary"
            android:textSize="12sp" />

        <com.bwei.gouwuche.CustomCounterView
            android:id="@+id/custom_product_counter"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/tv_product_price" />
    </RelativeLayout>
</RelativeLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="5dp"
        android:layout_alignParentBottom="true"
        android:background="#cccccc" />
</RelativeLayout>
Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐