记得在bean类里添加check属性
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#ffffff"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/shop_recy"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginBottom="@dimen/dp_50"
android:layout_marginTop="@dimen/dp_20"
android:layout_marginLeft="@dimen/dp_14"
android:layout_marginRight="@dimen/dp_24"
>
</android.support.v7.widget.RecyclerView>
<View
android:id="@+id/shop_view_bottom"
android:layout_width="match_parent"
android:layout_height="@dimen/dp_1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toTopOf="@id/shop_text_bottom"
android:background="#cccccc"/>
<TextView
android:id="@+id/shop_text_bottom"
android:layout_width="match_parent"
android:layout_height="@dimen/dp_50"
android:background="#ffffff"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
/>
<CheckBox
android:id="@+id/shop_box_all"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="@id/shop_text_bottom"
app:layout_constraintBottom_toBottomOf="@id/shop_text_bottom"
android:text="全选"
android:textSize="@dimen/sp_12"
android:textColor="#cccccc"
android:layout_marginLeft="@dimen/dp_14"
/>
<TextView
android:id="@+id/shop_text_all"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="@id/shop_text_bottom"
app:layout_constraintBottom_toBottomOf="@id/shop_text_bottom"
app:layout_constraintLeft_toRightOf="@id/shop_box_all"
android:layout_marginLeft="@dimen/dp_20"
android:text="合计:"
android:textSize="@dimen/sp_12"
android:textColor="#cccccc"
/>
<TextView
android:id="@+id/shop_text_allpr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="@id/shop_text_bottom"
app:layout_constraintBottom_toBottomOf="@id/shop_text_bottom"
app:layout_constraintLeft_toRightOf="@id/shop_text_all"
android:text="¥"
android:textSize="@dimen/sp_14"
android:textColor="#ff0606"
/>
<TextView
android:id="@+id/shop_text_allprice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="@id/shop_text_bottom"
app:layout_constraintBottom_toBottomOf="@id/shop_text_bottom"
app:layout_constraintLeft_toRightOf="@id/shop_text_allpr"
android:text="0"
android:textSize="@dimen/sp_14"
android:textColor="#ff0606"
/>
<TextView
android:id="@+id/shop_text_go"
android:layout_width="@dimen/dp_118"
android:layout_height="@dimen/dp_50"
android:background="#ff5e70"
app:layout_constraintTop_toTopOf="@id/shop_text_bottom"
app:layout_constraintBottom_toBottomOf="@id/shop_text_bottom"
app:layout_constraintRight_toRightOf="@id/shop_text_bottom"
android:text="提交订单"
android:gravity="center"
android:textColor="#ffffff"
android:textSize="@dimen/sp_16"
/>
</android.support.constraint.ConstraintLayout>
public class Frag_car extends Fragment implements CarView {
@BindView(R.id.shop_recy)
RecyclerView shopRecy;
@BindView(R.id.shop_view_bottom)
View shopViewBottom;
@BindView(R.id.shop_text_bottom)
TextView shopTextBottom;
@BindView(R.id.shop_box_all)
CheckBox shopBoxAll;
@BindView(R.id.shop_text_all)
TextView shopTextAll;
@BindView(R.id.shop_text_allpr)
TextView shopTextAllpr;
@BindView(R.id.shop_text_allprice)
TextView shopTextAllprice;
@BindView(R.id.shop_text_go)
TextView shopTextGo;
Unbinder unbinder;
private Carpresenter carpresenter;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.frag_02, container, false);
unbinder = ButterKnife.bind(this, view);
LinearLayoutManager linearLayoutManager=new LinearLayoutManager(getActivity());
shopRecy.setLayoutManager(linearLayoutManager);
carpresenter = new Carpresenter(this);
carpresenter.onRetlade();
return view;
}
@Override
public void onDestroyView() {
super.onDestroyView();
unbinder.unbind();
}
@Override
public void getHttpData(final List<CarBean.DataBean> data) {
final CarOneAadapter carOneAadapter=new CarOneAadapter(getActivity(), (ArrayList<CarBean.DataBean>) data);
shopRecy.setAdapter(carOneAadapter);
carOneAadapter.setParentCheckListener(new CarOneAadapter.OnParentCheckListener() {
@Override
public void onCheck(ArrayList<CarBean.DataBean> data) {
double priceSum = 0.0;//累计总价
int num1 = 0;//累计商品数量
int num2 = 0;//累计商品商品选中数量
for (int i=0;i<data.size();i++){
List<CarBean.DataBean.ListBean> list = data.get(i).getList();
for (int j=0;j<list.size();j++){
num1++;
boolean ischeck = list.get(j).isIscheck();
if (ischeck){
num2++;
priceSum+=list.get(j).getPrice()*list.get(j).getNum();
}
}
}
if (num1==num2){
shopBoxAll.setChecked(true);
}else {
shopBoxAll.setChecked(false);
}
shopTextAllprice.setText("总价:"+priceSum);
}
});
shopBoxAll.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
double priceSum = 0.0;//累计总价
if (shopBoxAll.isChecked()){
for (int i=0;i<data.size();i++){
data.get(i).setIscheck(true);
List<CarBean.DataBean.ListBean> list = data.get(i).getList();
for (int j=0;j<list.size();j++){
list.get(j).setIscheck(true);
priceSum+=list.get(j).getPrice()*list.get(j).getNum();
}
}
carOneAadapter.notifyDataSetChanged();
shopTextAllprice.setText("总价:"+priceSum);
}else {
for (int i=0;i<data.size();i++){
data.get(i).setIscheck(false);
List<CarBean.DataBean.ListBean> list = data.get(i).getList();
for (int j = 0; j < list.size(); j++) {
list.get(j).setIscheck(false);
priceSum+=list.get(j).getPrice()*list.get(j).getNum();
}
}
carOneAadapter.notifyDataSetChanged();
shopTextAllprice.setText("总价:0.0");
}
}
});
}
}
一级适配器
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<CheckBox
android:id="@+id/parent_checkBox"
android:layout_width="40dp"
android:layout_height="50dp"
android:gravity="center" />
<TextView
android:id="@+id/shop_title"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="15dp"
android:textSize="25sp"
android:textColor="#000"
android:layout_gravity="center_vertical"
android:text="哈哈哈" />
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/one_ryc"
android:layout_width="match_parent"
android:layout_height="wrap_content"></android.support.v7.widget.RecyclerView>
</LinearLayout>
public class CarOneAadapter extends RecyclerView.Adapter<CarOneAadapter.MyViewHolder> {
private Context context;
private ArrayList<CarBean.DataBean> arrayList;
public CarOneAadapter(Context context, ArrayList<CarBean.DataBean> list) {
this.context = context;
this.arrayList = list;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view=LayoutInflater.from(context).inflate(R.layout.carone_item,null);
MyViewHolder myViewHolder=new MyViewHolder(view);
return myViewHolder;
}
@Override
public void onBindViewHolder(@NonNull final MyViewHolder myViewHolder, final int i) {
final CarBean.DataBean dataBean = this.arrayList.get(i);
myViewHolder.textView.setText(dataBean.getSellerName());
myViewHolder.recyclerView.setLayoutManager(new LinearLayoutManager(context));
final List<CarBean.DataBean.ListBean> list=dataBean.getList();
final CarTwoAadapter aadapter=new CarTwoAadapter(context, (ArrayList<CarBean.DataBean.ListBean>) list);
myViewHolder.recyclerView.setAdapter(aadapter);
//myViewHolder.recyclerView.addItemDecoration(new DividerItemDecoration(context,DividerItemDecoration.VERTICAL));
myViewHolder.checkBox.setChecked(arrayList.get(i).isIscheck());
aadapter.setChildCheckListener(new CarTwoAadapter.OnChildCheckListener() {
@Override
public void oncheck() {
if (parentCheckListener!=null){
parentCheckListener.onCheck(arrayList);
}
boolean value=true;
for (int j=0;j<list.size();j++){
boolean childCheck=list.get(j).isIscheck();
if (!childCheck){
value=false;
break;
}
}
dataBean.setIscheck(value);
myViewHolder.checkBox.setChecked(value);
}
});
myViewHolder.checkBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dataBean.setIscheck(myViewHolder.checkBox.isChecked());
aadapter.setCheck(myViewHolder.checkBox.isChecked());
}
});
}
@Override
public int getItemCount() {
return arrayList.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder{
private final TextView textView;
private final CheckBox checkBox;
private final RecyclerView recyclerView;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
checkBox = itemView.findViewById(R.id.parent_checkBox);
textView = itemView.findViewById(R.id.shop_title);
recyclerView = itemView.findViewById(R.id.one_ryc);
}
}
public interface OnParentCheckListener{
void onCheck(ArrayList<CarBean.DataBean> data);
}
private OnParentCheckListener parentCheckListener;
public void setParentCheckListener(OnParentCheckListener parentCheckListener) {
this.parentCheckListener = parentCheckListener;
}
}
二级适配器
<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="match_parent"
android:orientation="vertical"
android:background="#9999"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="@dimen/dp_120"
android:orientation="horizontal">
<CheckBox
android:layout_marginLeft="20dp"
android:id="@+id/check_two"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<com.facebook.drawee.view.SimpleDraweeView
android:id="@+id/img"
app:roundedCornerRadius="10dp"
android:layout_gravity="center"
android:layout_width="@dimen/dp_100"
android:layout_height="@dimen/dp_100" />
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/name"
android:text="哈哈哈"
android:layout_marginTop="@dimen/dp_20"
android:textSize="15sp"
android:singleLine="true"
android:layout_marginLeft="@dimen/dp_5"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:orientation="horizontal"
android:layout_marginLeft="@dimen/dp_10"
android:layout_marginTop="@dimen/dp_50"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/money"
android:textColor="#f00"
android:textSize="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<com.smq.lx.widget.Custom_num
android:id="@+id/car_cus"
android:layout_marginLeft="@dimen/dp_20"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></com.smq.lx.widget.Custom_num>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
public class CarTwoAadapter extends RecyclerView.Adapter<CarTwoAadapter.MyViewHolder> {
private Context context;
private ArrayList<CarBean.DataBean.ListBean> listBeans;
public CarTwoAadapter(Context context, ArrayList<CarBean.DataBean.ListBean> listBeans) {
this.context = context;
this.listBeans = listBeans;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view=LayoutInflater.from(context).inflate(R.layout.cartwo_item,null);
MyViewHolder myViewHolder=new MyViewHolder(view);
return myViewHolder;
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, final int i) {
final CarBean.DataBean.ListBean list = this.listBeans.get(i);
myViewHolder.name.setText(list.getTitle());
myViewHolder.money.setText("¥"+list.getPrice());
myViewHolder.imageView.setImageURI(list.getDetailUrl());
myViewHolder.checkBox.setChecked(list.isIscheck());
myViewHolder.custom_num.setData(this, listBeans,i);
myViewHolder.custom_num.setNumChangeListener(new Custom_num.onNumChangeListener() {
@Override
public void onResult() {
if (childCheckListener!=null){
childCheckListener.oncheck();
}
}
});
myViewHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (childCheckListener!=null){
list.setIscheck(isChecked);
childCheckListener.oncheck();
}
}
});
}
@Override
public int getItemCount() {
return listBeans.size();
}
public void setCheck(boolean checked){
for (int i=0;i<listBeans.size();i++){
listBeans.get(i).setIscheck(checked);
}
notifyDataSetChanged();
}
public class MyViewHolder extends RecyclerView.ViewHolder{
private final CheckBox checkBox;
private final SimpleDraweeView imageView;
private final TextView name;
private final TextView money;
private Custom_num custom_num;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
checkBox = itemView.findViewById(R.id.check_two);
imageView = itemView.findViewById(R.id.img);
name = itemView.findViewById(R.id.name);
money = itemView.findViewById(R.id.money);
custom_num=itemView.findViewById(R.id.car_cus);
}
}
public interface OnChildCheckListener {
void oncheck();
}
public OnChildCheckListener childCheckListener;
public void setChildCheckListener(OnChildCheckListener childCheckListener) {
this.childCheckListener = childCheckListener;
}
}
加减器
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_height="match_parent">
<ImageView
android:id="@+id/num_image_minus"
android:layout_width="@dimen/dp_20"
android:layout_height="@dimen/dp_20"
android:src="@mipmap/jian"
android:layout_marginTop="@dimen/dp_3"
/>
<EditText
android:id="@+id/num_edit_price"
android:layout_width="@dimen/dp_31"
android:layout_height="@dimen/dp_20"
android:layout_marginLeft="@dimen/dp_5"
android:background="@drawable/shop_price_background"
android:gravity="center"
android:inputType="number"
android:textSize="@dimen/sp_12"
android:textColor="#666666"
android:layout_marginTop="@dimen/dp_5"
/>
<ImageView
android:id="@+id/num_image_add"
android:layout_width="@dimen/dp_20"
android:layout_height="@dimen/dp_20"
android:src="@mipmap/add"
android:layout_marginLeft="@dimen/dp_5"
android:layout_marginTop="@dimen/dp_3"
/>
</LinearLayout>
public class Custom_num extends LinearLayout implements View.OnClickListener {
private int num;
private EditText num_price;
private CarBean.DataBean.ListBean getitem;
private CarTwoAadapter carTwoAadapter;
private List<CarBean.DataBean.ListBean> list;
private int i;
public Custom_num(Context context) {
super(context);
}
public Custom_num(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
View view = LayoutInflater.from(context).inflate(R.layout.custom_num, null);
ImageView num_add = (ImageView) view.findViewById(R.id.num_image_add);
ImageView num_minus = (ImageView) view.findViewById(R.id.num_image_minus);
num_price = (EditText) view.findViewById(R.id.num_edit_price);
num_add.setOnClickListener(this);
num_minus.setOnClickListener(this);
num_price.setText(num+"");
addView(view);
}
public Custom_num(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.num_image_minus:
num--;
if (num < 1) {
Toast.makeText(getContext(), "不能再减啦!", Toast.LENGTH_SHORT).show();
} else {
num_price.setText(num + "");
list.get(i).setNum(num);
numChangeListener.onResult();
carTwoAadapter.notifyDataSetChanged();
}
break;
case R.id.num_image_add:
num++;
num_price.setText(num + "");
list.get(i).setNum(num);
numChangeListener.onResult();
carTwoAadapter.notifyDataSetChanged();
break;
default:
break;
}
}
public void setData(CarTwoAadapter carTwoAadapter,List<CarBean.DataBean.ListBean> list,int i){
this.carTwoAadapter=carTwoAadapter;
this.list = list;
this.i = i;
num = list.get(i).getNum();
num_price.setText(list.get(i).getNum() + "");
}
public interface onNumChangeListener {
void onResult();
}
public onNumChangeListener numChangeListener;
public void setNumChangeListener(onNumChangeListener numChangeListener) {
this.numChangeListener = numChangeListener;
}
}