RecyclerView 局部刷新优化使用

    private int mSelectedPos = 0;
    List<StartCheckBean> items;

    @Override
    public void onBindViewHolder(@NonNull StartCheckItmenListAdapter.ViewHolder holder, int position) {
        Log.d("TAG", "onBindViewHolder() called with: holder = [" + holder + "], position = [" + position + "]");
        holder.checkBox2.setChecked(items.get(position).getSelected());//“CheckBox”
        holder.textView102.setText(items.get(position).getName());//TextView
        holder.checkBox2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //方法一   : 当数量较大时 会卡顿
                //实现单选,第一种方法,十分简单, Lv Rv通用,因为它们都有notifyDataSetChanged()方法
                // 每次点击时,先将所有的selected设为false,并且将当前点击的item 设为true, 刷新整个视图
//                for (StartCheckBean data : items) {
//                    data.setSelected(false);
//                }
//                items.get(position).setSelected(true);
//                notifyDataSetChanged();
                //方法二
                if (mSelectedPos!=position){
                    //先取消上个item的勾选状态
                    items.get(mSelectedPos).setSelected(false);
                    notifyItemChanged(mSelectedPos);
                    //设置新Item的勾选状态
                    mSelectedPos = position;
                    items.get(mSelectedPos).setSelected(true);
                    notifyItemChanged(mSelectedPos);
                }
            }
        });

    }

StartCheckBean.class

package com.xy.youxianghaoche.emp;

/**
 * @author 杨景
 * @description:
 * @date : 2021/5/8 0008 10:27
 */
public class StartCheckBean {
    private Boolean isSelected;
    private String name;
    private String checkType;
    private int money;

    public Boolean getSelected() {
        return isSelected;
    }

    public String getName() {
        return name;
    }

    public String getCheckType() {
        return checkType;
    }

    public int getMoney() {
        return money;
    }

    public void setSelected(Boolean selected) {
        isSelected = selected;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setCheckType(String checkType) {
        this.checkType = checkType;
    }

    public void setMoney(int money) {
        this.money = money;
    }

    public StartCheckBean(Boolean isSelected, String name, String checkType, int money) {
        this.isSelected = isSelected;
        this.name = name;
        this.checkType = checkType;
        this.money = money;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值