Bean 集合手动加两条数据 一个用来控制选中状态 一个用来做数量
private boolean checktwo;
private int aaa=1;
在fragment 或者activity中的布局
<android.support.v7.widget.RecyclerView
android:layout_weight="1"
android:id="@+id/recytwo"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
<LinearLayout
android:gravity="center_vertical"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<CheckBox
android:id="@+id/qf"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="全选/反选"
android:textSize="20sp"
/>
<TextView
android:id="@+id/money"
android:layout_weight="1"
android:layout_marginLeft="30dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="总价"
android:textColor="#f00"
android:textSize="25sp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="去结算"
android:textColor="#fff"
android:background="#f00"
android:textSize="20sp"
/>
</LinearLayout>
网络请求到数据后进入adapter中写一级的布局及赋值
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:gravity="center_vertical"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/shoppcheck"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="名字"
android:textColor="#f00"
android:id="@+id/shoppname"
android:textSize="28sp"
/>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/recythree"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
在一级适配器中执行二级适配器数据及布局
//二级数据
final List<ShoppBean.DataBean.ListBean> listt = this.list.get(i).getList();
//布局管理
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
myviewholder.recy.setLayoutManager(linearLayoutManager);
//适配器
final Shopp2Adapter shopp2Adapter = new Shopp2Adapter(context,listt);
myviewholder.recy.setAdapter(shopp2Adapter);
二级的布局
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/spcheck"
/>
<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/spimg"
android:layout_width="100dp"
android:layout_height="100dp"
/>
<LinearLayout
android:layout_weight="1"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:textSize="20sp"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="商品名"
android:id="@+id/spname"
/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_weight="1"
android:textSize="25sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/spprice"
android:text="价格"
android:textColor="#f00"
/>
<com.bw.wangyao.myview.AddView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/add"
>
</com.bw.wangyao.myview.AddView>
</LinearLayout>
</LinearLayout>
</LinearLayout>
主页面或fragment
//点击全选及全不选及价格
qf.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
for (int i = 0; i<data.size(); i++) {
//将最外层复选框的值赋值给商家
data.get(i).setCheckone(qf.isChecked());
for (int j = 0; j <data.get(i).getList().size(); j++) {
//将最外层复选框的值赋值给商品
data.get(i).getList().get(j).setChecktwo(qf.isChecked());
}
}
shopp1Adapter.notifyDataSetChanged();
//价格
zongmoney(data);
}
});
// 反选 商家的接口回调方法中
shopp1Adapter.setBool(new Shopp1Adapter.Bool() {
@Override
public void getdate() {
boolean da=true;
for (int i = 0; i<data.size(); i++) {
//将商家的状态给变量
da= da&data.get(i).isCheckone();
for (int j = 0; j <data.get(i).getList().size(); j++) {
//将商品的状态给变量
da= da&data.get(i).getList().get(j).isChecktwo();
}
}
qf.setChecked(da);
//价格
zongmoney(data);
}
});
}
//总价
private void zongmoney(List<ShoppBean.DataBean> data) {
double zong=0;
for (int i = 0; i<data.size(); i++) {
for (int j = 0; j <data.get(i).getList().size(); j++) {
if (data.get(i).getList().get(j).isChecktwo()){
//价格乘以数量 数量的值为集合中自己添的字段
int i1 = data.get(i).getList().get(j).getPrice() * data.get(i).getList().get(j).getAaa();
zong+=i1;
}
}
}
money.setText(zong+"");
}
商家的适配器
商家控制商品
myviewholder.check.setOnClickListener(null);
myviewholder.check.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
for (int j = 0; j<list.get(i).getList().size(); j++) {
list.get(i).getList().get(j).setChecktwo(myviewholder.check.isChecked());
}
list.get(i).setCheckone(myviewholder.check.isChecked());
notifyDataSetChanged();
//商家接口回调状态
bool.getdate();
}
});
反选 商品的接口回调方法中
//反选
shopp2Adapter.setBooll(new Shopp2Adapter.Booll() {
@Override
public void getdate() {
boolean da=true;
for (int j = 0; j <list.get(i).getList().size(); j++) {
da=da&list.get(i).getList().get(j).isChecktwo();
}
list.get(i).setCheckone(da);
shopp2Adapter.notifyDataSetChanged();
notifyDataSetChanged();
//商家接口回调状态
bool.getdate();
}
});
商品的适配器
myviewholder.check.setOnCheckedChangeListener(null);
myviewholder.check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
list.get(i).setChecktwo(myviewholder.check.isChecked());
//商品的接口回调
booll.getdate();
}
});
//自定义加减器中的方法构造传参
myviewholder.add.setnumbre(list.get(i).getAaa());
自定义加减器的值
//接收到值
myviewholder.add.setDate(new AddView.Date() {
@Override
public void getdate(int num) {
list.get(i).setAaa(num);
//商品的接口回调
booll.getdate();
}
});
自定义加减器类
public AddView(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.add_item,this);
jian = findViewById(R.id.jian);
jia = findViewById(R.id.jia);
ed = findViewById(R.id.zhi);
jian.setOnClickListener(this);
jia.setOnClickListener(this);
}
//设置ed的值
public void setnumbre(int numbre) {
ed.setText(numbre+"");
}
@Override
public void onClick(View v) {
String s = ed.getText().toString();
int i = Integer.parseInt(s);
switch (v.getId()){
case R.id.jian:
if (i>1){
i--;
ed.setText(i+"");
date.getdate(i);
return;
}
break;
case R.id.jia:
i++;
ed.setText(i+"");
date.getdate(i);
break;
}
}
public interface Date{
void getdate(int i);
}
private Date date;
public void setDate(Date date) {
this.date = date;
}