全选总价新
作者:互联网
package com.bw.view.adapter;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import com.bw.R;
import com.bw.model.bean.GwcBean;
import com.bw.model.zidingyi.JiaJianQi;
import com.facebook.drawee.view.SimpleDraweeView;
import java.util.List;
public class ShowAdapter extends RecyclerView.Adapter<ShowAdapter.ViewHolder> {
Context context;
GwcBean gwcBean;
CheckBox quanXuanCheck;
TextView zongjia;
private List<GwcBean.ResultBean> list;
public ShowAdapter(Context context, GwcBean gwcBean) {
this.context = context;
this.gwcBean = gwcBean;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(context).inflate(R.layout.gwc_buju, null);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, final int i) {
list = gwcBean.getResult();
viewHolder.gwc_name.setText(gwcBean.getResult().get(i).getCommodityName());
viewHolder.gwc_price.setText(gwcBean.getResult().get(i).getPrice()+"");
viewHolder.gwc_sim.setImageURI(gwcBean.getResult().get(i).getPic());
viewHolder.gwc_checkBox.setChecked(gwcBean.getResult().get(i).isCheck());//获取checkBox
viewHolder.gwc_jiajianqi.setShuLiang(gwcBean.getResult().get(i).getCount()+"");//先获取数量
viewHolder.gwc_jiajianqi.getJieKouJJQ(new JiaJianQi.JieKouJJQ() {
@Override
public void shuju(int value) {
gwcBean.getResult().get(i).setCount(value);
ZongJiaFF();
notifyDataSetChanged();
}
});
//每个商品的选中状态
viewHolder.gwc_checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override //商品的check
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
list.get(i).setCheck(isChecked);//先设置
boolean neicheckFF = NeiCheckFF();
quanXuanCheck.setChecked(neicheckFF);//改变
ZongJiaFF();
}
});
}
//总价方法
private void ZongJiaFF() {
int num = 0;
for (int i = 0; i < list.size(); i++) {
int pic = list.get(i).getPrice();
int count = list.get(i).getCount();
if (list.get(i).isCheck() == true){
num += pic*count;
}
}
zongjia.setText(num+"");
}
private boolean NeiCheckFF() {
for (int i = 0; i < list.size(); i++) {
boolean b = list.get(i).isCheck();//获取循环每个商品的状态
if (!b){//如果不为真
return false;
}
}
return true;
}
@Override
public int getItemCount() {
return gwcBean.getResult().size();
}
//fragment传过来的全选
public void setCheck(CheckBox gwcQuanxuan) {
this.quanXuanCheck = gwcQuanxuan;
quanXuanCheck.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean checked = quanXuanCheck.isChecked();//获取全选状态
WaiCheckFF(checked);//把状态给到全选的方法
ZongJiaFF();
}
});
notifyDataSetChanged();//记得刷新
}
//全选的方法
private void WaiCheckFF(boolean checked) {
for (int i = 0; i < gwcBean.getResult().size(); i++) {
list.get(i).setCheck(checked);//循环获取所有check状态,并用全选的按钮牛赋值
}
notifyDataSetChanged();//记得刷新
}
//fragment传过来的总价
public void setPrice(TextView gwcZongjia) {
this.zongjia = gwcZongjia;
notifyDataSetChanged();
}
public class ViewHolder extends RecyclerView.ViewHolder {
private final CheckBox gwc_checkBox;
private final SimpleDraweeView gwc_sim;
private final TextView gwc_name;
private final TextView gwc_price;
private final JiaJianQi gwc_jiajianqi;
public ViewHolder(@NonNull View itemView) {
super(itemView);
gwc_checkBox = itemView.findViewById(R.id.gwc_checkBox);
gwc_sim = itemView.findViewById(R.id.gwc_sim);
gwc_name = itemView.findViewById(R.id.gwc_name);
gwc_price = itemView.findViewById(R.id.gwc_price);
gwc_jiajianqi = itemView.findViewById(R.id.gwc_jiajianqi);
}
}
}
标签:总价,gwc,get,list,全选,import,gwcBean,public 来源: https://blog.csdn.net/weixin_44660509/article/details/89878871