在 android 中滚动时,突出显示的项目在 recylerview 中丢失 [英] Highlighted items are lost in recylerview when scroll in android

查看:28
本文介绍了在 android 中滚动时,突出显示的项目在 recylerview 中丢失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 recylerview 中有一个项目列表.我在点击时突出显示项目,但问题是当我滚动时,这些项目的突出显示状态会丢失.我已经环顾了很多解决方案,但问题仍然存在.当您在滚动后单击这些项目时,列表的大小会减小,因为它应该只高亮状态消失.

I have a list of items in the recylerview. I am highlighting the items on click but the problem is that the highlighted state of those items are lost when I scroll. I have looked around a lot of solutions but the problems still remain. When you click back those items after scrolling, the size of list decreases as it should only the highlight state is gone.

public void onBindViewHolder (@NonNull MyAdapter.MyViewHolder holder, int position) {

    MyFiles item = myFiles.get(position);

    if (item.isHighlighted()) {

        // change to desired color
        item.setHighlighted(true);
        holder.itemView.setBackgroundColor(activity.getResources().getColor(R.color.grey));

    } else {
        // change to default color
        item.setHighlighted(false);
        holder.itemView.setBackgroundColor(activity.getResources().getColor(R.color.white));
    }

    holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {

        @Override
        public boolean onLongClick(View v) {
            if (!isModeEnabled) {
                
                // highlight item here
                item.setHighlighted(!item.isHighlighted());
                isActionModeEnabled = true;
                Highlight(holder);
            }
            
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (isActionModeEnabled) {
                        
                        // highlight item here 
                        item.setHighlighted(!item.isHighlighted());                      
                        Highlight(holder);
                    }
                }
            }
        }
        
        @Override
        public int getItemViewType(int position) {
            return position;
        }
        
        public void Highlight(MyViewHolder holder) {
        isSelectMode = true;
        
        MyFiles files = myFiles.get(holder.getAdapterPosition());
        if (!selectList.contains(files)){
            selectList.add(files);
        } else {
            selectList.remove(files);
        }
    }
}

public class MyFiles{
    
    public long id;
    public String path;
    private boolean isHighlighted = false;
    
    public boolean isHighlighted() {
        return isHighlighted;
    }
    
    public void setChecked(boolean isHighlighted) {
        this.isHighlighted = isHighlighted;
    }
    
    public MyFiles(Long id, String title, boolean isHighlighted) {
        this.id = id;
        this.title = title;
        this.isHighlighted = isHighlighted;
    }
}

有人可以帮忙吗?谢谢!

Could someone please help? Thanks!

推荐答案

添加boolean highlited"到您的我的文件"类和在您突出显示项目集highlighted = true"的侦听器中.这样,您将获得每个项目的信息,无论它是否突出显示,并做出相应的反应.如果您只是通过某些操作为某个项目着色,但不存储有关它正在完成的信息,则无法真正知道哪个项目被突出显示.

Add "boolean highlited" to your "MyFiles" class and in the listener where you highlight an item set "highlighted = true". That way you will have information for each item wheather it is highlighted or not and react accordingly. If you just color an item via some action, but don't store information about it being done there isn't really a way of knowing which one was highlighted.

进一步澄清:在您的持有人中,您应该做类似的事情

To further clarify: in your holder you should do something like

if(item.isHighlighted()){
   // change to desired color
}else{
   // change to default color
}

在您的 onItemClick 中:

And in your onItemClick:

item.setHighlighted(!item.isHighlighted());

通常在处理回收者视图时,您希望有一个活动,其中包含您要查看的所有项目的列表.一旦该列表存在,您就将其传递给适配器.

Generally when dealing with recycler view you want to have an activity containing a list of all the items you want to be viewed. Once that list exists you pass it to the adapter.

在适配器内部有一个支架,您可以使用它来格式化您的 recyclerView 的外观.在这里,您将根据天气项目已突出显示 = 真或假来更改特定项目的颜色.

Inside adapter there is a holder which you use to format the look of your recyclerView. Here you will change the color of the specific item depending on weather item has highlighted = true or false.

在适配器内的 onclickListener 上,您将更改突出显示的状态(如果为真则设为假,如果为假则设为真).

On the onclickListener inside your adapter you will change highlighted state (if it was true make it false if it was false make it true).

更改项目状态后,必须将该更改应用于传递给适配器的原始列表,否则您始终拥有原始列表.

After changing item state, that change has to be applied to the original list passed to the adapter, otherwise you always have the original list.

这篇关于在 android 中滚动时,突出显示的项目在 recylerview 中丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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