public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{
private List<GoosBean.DataBean.DatasBean> list;
private Context context;
//创建自己的构造方法
public MyAdapter(List<GoosBean.DataBean.DatasBean> list, Context context) {
this.list = list;
this.context = context;
}
//加载布局
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.child,parent,false);
return new MyViewHolder(view);
}
//绑定数据
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
final GoosBean.DataBean.DatasBean datasBean = list.get(position);
final MyViewHolder myViewHolder = (MyViewHolder) holder;
myViewHolder.cbChild.setChecked(datasBean.isChecked());
myViewHolder.tv_tel.setText(datasBean.getType_name());
myViewHolder.tv_content.setText(datasBean.getMsg());
myViewHolder.tv_time.setText(datasBean.getAdd_time());
myViewHolder.tv_price.setText(datasBean.getPrice() + "");
myViewHolder.myView.setNum(datasBean.getNum() + "");
//给checkbox设置点击事件
myViewHolder.cbChild.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//首先改变当前checkbox的状态值
datasBean.setChecked(myViewHolder.cbChild.isChecked());
PriceAndCountEvent compute = compute();
EventBus.getDefault().post(compute);
//如果改变
if(myViewHolder.cbChild.isChecked()){
//查看是否全选
if(isAllCbSelect()){
//改变全选按钮的状态
changeAllCbState(true);
}
}else{
changeAllCbState(false);
}
notifyDataSetChanged();
}
});
//点击增加按钮
myViewHolder.myView.setAddClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//获取数量和价钱
int num = myViewHolder.myView.getNum();
num++;
datasBean.setNum(num);
//如果勾选了,
if(myViewHolder.cbChild.isChecked()){
EventBus.getDefault().post(compute());
}
notifyDataSetChanged();
}
});
//减少按钮的点击
myViewHolder.myView.setDelClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//获取数量和价钱
int num = myViewHolder.myView.getNum();
if(num==1){
return;
}
num--;
datasBean.setNum(num);
//如果勾选了,
if(myViewHolder.cbChild.isChecked()){
EventBus.getDefault().post(compute());
}
notifyDataSetChanged();
}
});
//删除按钮的
myViewHolder.tv_del.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
list.remove(position);
//发送价钱和数量
EventBus.getDefault().post(compute());
//刷新
notifyDataSetChanged();
}
});
}
@Override
public int getItemCount() {
return list.size();
}
//创建自定义viewholder,继承viewholder
class MyViewHolder extends RecyclerView.ViewHolder{
private final CheckBox cbChild;
private final TextView tv_tel;
private final TextView tv_content;
private final TextView tv_time;
private final TextView tv_price;
private MyView myView;
private final TextView tv_del;
public MyViewHolder(View itemView) {
super(itemView);
cbChild = (CheckBox) itemView.findViewById(R.id.cb_child);
tv_tel = (TextView) itemView.findViewById(R.id.tv_tel);
tv_content = (TextView) itemView.findViewById(R.id.tv_content);
tv_time = (TextView) itemView.findViewById(R.id.tv_time);
tv_price = (TextView) itemView.findViewById(R.id.tv_pri);
tv_del = (TextView) itemView.findViewById(R.id.tv_del);
myView = (MyView) itemView.findViewById(R.id.mv);
}
}
//查看checkbox是否全选
public boolean isAllCbSelect(){
for (int i = 0; i <list.size() ; i++) {
GoosBean.DataBean.DatasBean datasBean = list.get(i);
if(!datasBean.isChecked()){
return false;
}
}
return true;
}
//改变全选按钮的状态
public void changeAllCbState(boolean flag){
MessageEvent messageEvent = new MessageEvent();
messageEvent.setChecked(flag);
EventBus.getDefault().post(messageEvent);
}
//创建一个方法传递价钱和数量
public PriceAndCountEvent compute(){
int price = 0;
int count = 0;
for (int i = 0; i <list.size() ; i++) {
GoosBean.DataBean.DatasBean datasBean = list.get(i);
//如果勾选了
if(datasBean.isChecked()){
//进行价钱和数量的++
count += datasBean.getNum();
price += datasBean.getNum()*datasBean.getPrice();
}
}
PriceAndCountEvent priceAndCountEvent = new PriceAndCountEvent();
priceAndCountEvent.setCount(count);
priceAndCountEvent.setPrice(price);
return priceAndCountEvent;
}
//设置全选,反选的方法(点击全选,改变列表的状态值)
public void allSelect(boolean flag){
for (int i = 0; i <list.size() ; i++) {
GoosBean.DataBean.DatasBean datasBean = list.get(i);
datasBean.setChecked(flag);
}
EventBus.getDefault().post(compute());
notifyDataSetChanged();
}
}
android一级列表购物车
最新推荐文章于 2021-06-01 02:40:40 发布