逻辑:
全选或全不选(复选框);
商品控制商家(复选框);
商家控制商品复选框(复选框);
商家和商品控制全选(复选框);
复选框选中状态计算价格;
首先要在Bean层中添加布尔值,用来判断全选反选的状态
Activity界面
public class MainActivity extends AppCompatActivity implements IView {
@BindView(R.id.bigrecy)
RecyclerView bigrecy;
@BindView(R.id.iv_cricle)
CheckBox ivCricle;
@BindView(R.id.txt_all)
TextView txtAll;
@BindView(R.id.layout_all)
RelativeLayout layoutAll;
@BindView(R.id.all_price)
TextView allPrice;
@BindView(R.id.sum_price_txt)
TextView sumPriceTxt;
@BindView(R.id.sum_price)
RelativeLayout sumPrice;
@BindView(R.id.layout_buttom)
RelativeLayout layoutButtom;
private List<ShopCarBean.DataBean> list;
private ShopAdapter shopAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
Presenter presenter = new Presenter(this);
presenter.getModelShop();
presenter.attach(this);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setOrientation(OrientationHelper.VERTICAL);
bigrecy.setLayoutManager(linearLayoutManager);
//底部全选框
ivCricle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//调用内部类判断状态
checkSeller(ivCricle.isChecked());
//实时刷新适配器
shopAdapter.notifyDataSetChanged();
}
});
}
@Override
public void getPreData(ShopCarBean shopCarBean) {
list = shopCarBean.getData();
shopAdapter = new ShopAdapter(MainActivity.this, list);
bigrecy.setAdapter(shopAdapter);
shopAdapter.setListener(new ShopAdapter.ShopCallBackListener() {
@Override
public void callBack(List<ShopCarBean.DataBean> list) {
//在这里重新遍历已经改状态后的数据,
// 这里不能break跳出,因为还需要计算后面点击商品的价格和数目,所以必须跑完整个循环
double totalPrice = 0;
//勾选商品的数量,不是该商品购买的数量
int num = 0;
//所有商品总数,和上面的数量做比对,如果两者相等,则说明全选
int totalNum = 0;
for (int a = 0; a < list.size(); a++) {
//获取商家里商品
List<ShopCarBean.DataBean.ListBean> listAll = list.get(a).getList();
for (int i = 0; i < listAll.size(); i++) {
totalNum = totalNum + listAll.get(i).getNum();
//取选中的状态
if (listAll.get(i).isChecked()) {
totalPrice = totalPrice + (listAll.get(i).getPrice() * listAll.get(i).getNum());
num = num + listAll.get(i).getNum();
}
}
}
if (num < totalNum) {
//不是全部选中
ivCricle.setChecked(false);
} else {
//是全部选中
ivCricle.setChecked(true);
}
allPrice.setText("合计:" + totalPrice);
sumPriceTxt.setText("去结算(" + num + ")");
}
});
}
/**
* 底部全选框的处理
* 修改选中状态,获取价格和数量
*/
private void checkSeller(boolean bool) {
double totalPrice = 0;
int num = 0;
for (int a = 0; a < list.size(); a++) {
//遍历商家,改变状态
ShopCarBean.DataBean dataBean = list.get(a);
dataBean.setChecked(bool);
List<ShopCarBean.DataBean.ListBean> listAll = list.get(a).getList();
for (int i = 0; i < listAll.size(); i++) {
//遍历商品,改变状态
listAll.get(i).setChecked(bool);
totalPrice = totalPrice + (listAll.get(i).getPrice() * listAll.get(i).getNum());
num = num + listAll.get(i).getNum();
}
}
if (bool) {
allPrice.setText("合计:" + totalPrice);
sumPriceTxt.setText("去结算(" + num + ")");
} else {
allPrice.setText("合计:0.00");
sumPriceTxt.setText("去结算(0)");
}
}
}
商家的适配器
public class ShopAdapter extends RecyclerView.Adapter<ShopAdapter.MyHodel> {
Context context;
List<ShopCarBean.DataBean> list;
public ShopAdapter(Context context, List<ShopCarBean.DataBean> list) {
this.context = context;
this.list = list;
}
@NonNull
@Override
public MyHodel onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View inflate = View.inflate(context, R.layout.recyitem, null);
MyHodel myHodel = new MyHodel(inflate);
return myHodel;
}
@Override
public void onBindViewHolder(@NonNull final MyHodel myHodel, final int i) {
myHodel.biao.setText(list.get(i).getSellerName());
//商品的布局管理器
List<ShopCarBean.DataBean.ListBean> beanList = this.list.get(i).getList();
myHodel.tworecy.setLayoutManager(new LinearLayoutManager(context));
final ItemAdapter itemAdapter = new ItemAdapter(context, beanList);
myHodel.tworecy.setAdapter(itemAdapter);
//设置商家的选中状态
myHodel.xuan.setChecked(list.get(i).isChecked());
itemAdapter.setListener(new ItemAdapter.ShopCallBackListener() {
@Override
public void callBack() {
//从商品适配里回调回来,回给activity,activity计算价格和数量
if(mShopCallBackListener != null) {
mShopCallBackListener.callBack(list);
}
List<ShopCarBean.DataBean.ListBean> listBeans = list.get(i).getList();
//创建一个临时的标志位,用来记录现在点击的状态
boolean isAllChecked = true;
for (ShopCarBean.DataBean.ListBean bean : listBeans) {
if (!bean.isChecked()) {
//只要有一个商品未选中,标志位设置成false,并且跳出循环
isAllChecked = false;
break;
}
}
//刷新商家的状态
myHodel.xuan.setChecked(isAllChecked);
list.get(i).setChecked(isAllChecked);
}
});
//监听checkBox的点击事件
//商家改变商品的选中状态
myHodel.xuan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//首先改变自己的标志位
list.get(i).setChecked(myHodel.xuan.isChecked());
//调用产品adapter的方法,用来全选和反选
itemAdapter.selectOrRemoveAll(myHodel.xuan.isChecked());
//刷新适配器,改变选中商家之后的价格
itemAdapter.notifyDataSetChanged();
}
});
}
@Override
public int getItemCount() {
return list.size();
}
public class MyHodel extends RecyclerView.ViewHolder {
private final TextView biao;
private final CheckBox xuan;
private final RecyclerView tworecy;
public MyHodel(@NonNull View itemView) {
super(itemView);
xuan = itemView.findViewById(R.id.xuan);
biao = itemView.findViewById(R.id.biao);
tworecy = itemView.findViewById(R.id.errecy);
}
}
//商家回调商品的接口回调
private ShopCallBackListener mShopCallBackListener;
public void setListener(ShopCallBackListener listener) {
this.mShopCallBackListener = listener;
}
public interface ShopCallBackListener {
void callBack(List<ShopCarBean.DataBean> list);
}
}
商品适配器
public class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.MyHodel> {
Context context;
List<ShopCarBean.DataBean.ListBean> beanList;
public ItemAdapter(Context context, List<ShopCarBean.DataBean.ListBean> beanList) {
this.context = context;
this.beanList = beanList;
}
@NonNull
@Override
public MyHodel onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View inflate = View.inflate(context, R.layout.item, null);
MyHodel myHodel = new MyHodel(inflate);
return myHodel;
}
@Override
public void onBindViewHolder(@NonNull MyHodel myHodel, final int i) {
myHodel.imageView.setImageURI(beanList.get(i).getImages());
myHodel.nei.setText(beanList.get(i).getTitle());
myHodel.price.setText("$:"+beanList.get(i).getPrice()+"");
//根据我记录的状态,改变勾选
myHodel.xuan2.setChecked(beanList.get(i).isChecked());
//商品的跟商家的有所不同,商品添加了选中改变的监听
myHodel.xuan2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
beanList.get(i).isChecked();
if (mShopCallBackListener != null) {
mShopCallBackListener.callBack();
}
}
});
//将值传给自定义View
myHodel.custMyAdd.setData(this,beanList,i);
//设置自定义输入框内的数字改变
myHodel.custMyAdd.setOnCallBack(new CustomAddView.CallBackListener() {
@Override
public void callBack() {
if (mShopCallBackListener != null) {
mShopCallBackListener.callBack();
}
}
});
}
@Override
public int getItemCount() {
return beanList.size();
}
public class MyHodel extends RecyclerView.ViewHolder {
private final CheckBox xuan2;
private final SimpleDraweeView imageView;
private final TextView nei;
private final TextView price;
private final CustomAddView custMyAdd;
public MyHodel(@NonNull View itemView) {
super(itemView);
xuan2 = itemView.findViewById(R.id.xuan2);
imageView = itemView.findViewById(R.id.imageView);
nei = itemView.findViewById(R.id.nei);
price = itemView.findViewById(R.id.price);
custMyAdd = itemView.findViewById(R.id.custMyAdd);
}
}
/**
* 在我们子商品的adapter中,修改子商品的全选和反选
*
* @param isSelectAll
* List<ShopCarBean.DataBean.ListBean> beanList;
*/
public void selectOrRemoveAll(boolean isSelectAll) {
for (ShopCarBean.DataBean.ListBean listBean : beanList) {
listBean.setChecked(isSelectAll);
}
notifyDataSetChanged();
}
//自定义View接口回调
private ShopCallBackListener mShopCallBackListener;
public void setListener(ShopCallBackListener listener) {
this.mShopCallBackListener = listener;
}
public interface ShopCallBackListener {
void callBack();
}
}
自定义View界面
public class CustomAddView extends RelativeLayout implements View.OnClickListener {
Context mContext;
private EditText mEditCar;
private List<ShopCarBean.DataBean.ListBean> listBeans;
private ItemAdapter itemAdapter;
private int i;
public CustomAddView(Context context) {
super(context);
init(context);
}
public CustomAddView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public CustomAddView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
private void init(Context context) {
this.mContext = 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);
mEditCar = view.findViewById(R.id.edit_shop_car);
addIamge.setOnClickListener(this);
jianIamge.setOnClickListener(this);
addView(view);
}
private int num;
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.add_car:
//改变数量,设置数量,改变对象内容,回调,局部刷新
num++;
mEditCar.setText(num + "");
listBeans.get(i).setNum(num);
mCallBackListener.callBack();
itemAdapter.notifyItemChanged(i);
break;
case R.id.jian_car:
if (num > 1) {
num--;
} else {
Toast.makeText(mContext, "商品数量不能小于1", Toast.LENGTH_LONG).show();
}
mEditCar.setText(num + "");
listBeans.get(i).setNum(num);
mCallBackListener.callBack();
itemAdapter.notifyItemChanged(i);
break;
default:
break;
}
}
public void setData(ItemAdapter itemAdapter, List<ShopCarBean.DataBean.ListBean> list, int position) {
this.listBeans = list;
this.itemAdapter = itemAdapter;
i = position;
num = listBeans.get(i).getNum();
mEditCar.setText(this.num + "");
}
private CallBackListener mCallBackListener;
public void setOnCallBack(CallBackListener listener) {
this.mCallBackListener = listener;
}
public interface CallBackListener {
void callBack();
}
}
首页布局
<RelativeLayout 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.support.v7.widget.RecyclerView
android:id="@+id/bigrecy"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/layout_buttom" />
<!--下面的全选-->
<RelativeLayout
android:id="@+id/layout_buttom"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
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_toLeftOf="@+id/sum_price"
android:layout_toRightOf="@+id/layout_all"
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>
</RelativeLayout>
商家布局
<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="wrap_content"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="#9f0aea"
android:orientation="horizontal">
<CheckBox
android:id="@+id/xuan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp" />
<TextView
android:id="@+id/biao"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="商家" />
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/errecy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></android.support.v7.widget.RecyclerView>
</LinearLayout>
商品布局
<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="wrap_content"
android:orientation="horizontal"
tools:context=".MainActivity">
<CheckBox
android:id="@+id/xuan2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
/>
<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/imageView"
android:layout_width="100dp"
android:layout_height="100dp"
app:placeholderImage="@mipmap/ic_launcher"
/>
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<TextView
android:id="@+id/nei"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="内容"
android:layout_marginLeft="20dp"
/>
<TextView
android:id="@+id/price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:text="¥:价钱"
/>
//调用自定义View加减框
<com.example.mouthdemo4.MyView.CustomAddView
android:id="@+id/custMyAdd"
android:layout_marginTop="15dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></com.example.mouthdemo4.MyView.CustomAddView>
</LinearLayout>
</LinearLayout>