在recyclerview中错误的项目更改 [英] wrong item changes in recyclerview

查看:72
本文介绍了在recyclerview中错误的项目更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我受困于此,需要帮助:

Hi everyone I'm stuck in this and need help :

每个项目都有一个CheckBox,我在RecyclerView中为我的项目的根元素设置setOnLongClickListener,如下所示:

Each item has a CheckBox and I set setOnLongClickListener for root element of my items in RecyclerView like this :

    holder.faviorateVideoItemRelative.setOnLongClickListener(new View.OnLongClickListener() {
        public boolean onLongClick(View arg0) {


            if (chk_visible)
            {
                return  true ;
            }
            holder.chk_faviorateVideo.setChecked(!holder.chk_faviorateVideo.isChecked());
            chk_visible = true ;
            checkedItemsCNT = 1 ;
            deleteListVideoCourses.add(data.get(holder.getAdapterPosition())) ;
            notifyDataSetChanged() ;
            return  true ;
        }
    });

如果我向下滚动,当我长时间单击其中一项时,将检查错误项的CheckBox

If I scroll down , when I make a long click on one of items , the CheckBox of wrong item get checked !

推荐答案

它是因为用户RecycleView会在每次滚动时重用视图. RecycleView这样重复使用您的资源

It because as you user RecycleView it reuse your view every time when you scroll. RecycleView reuse your resource like this

因此,当您滚动时,它显示的视图状态错误

So when you scroll it's showing wrong state of your view

如果您编写了用于在onBindViewHolder中进行检查的任何逻辑,则必须同时使用true和false

If you write any logic for check in onBindViewHolder then you have to user both part for true and false

if(yourCondition){
   //code if condition is true
}else {
   //code if condition is false
}

其他解决方案

只需使用一条语句来停止RecycleView这样的重用视图状态,就可以解决该问题

Other Solution

Simply you can solve it just using one statement to stop your RecycleView to reuse your view state like this

@Override
public void onBindViewHolder(ReqNotificationAdapter.MyViewHolder holder, int position) {

    holder.setIsRecyclable(false);
    //.................your other code
}

我用它来解决我的问题..希望它可以解决您的问题,如果您没有问题,请停止按照目标进行回收.

I use it to solve my problem.. hope it will solve yours if you don't have problem stop Recycling with your purpose.

这篇关于在recyclerview中错误的项目更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆