回收的recyclerview项目保留了旧的背景色 [英] recycled recyclerview item keeps the old background color

查看:179
本文介绍了回收的recyclerview项目保留了旧的背景色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个回收站视图,并且在onClick(View view)内部,我正在将背景色更改为几乎透明的红色view.setBackgroundColor(Color.argb(64, 183, 28, 28)); 但是发生了一些奇怪的事情,当我向下滚动时,我看到尚未单击的项目的颜色已更改,我的猜测是,当回收该项目时,它仍保留该颜色.我想删除该颜色,但是在持有人的构造函数中删除它却不起作用,所以我的问题是我该如何处理?

I have a recycler view and inside the onClick(View view) i'm changing the background color to almost transparent red view.setBackgroundColor(Color.argb(64, 183, 28, 28)); but something weird is happening which is when I scroll down I see the color has changed for items that have not been clicked yet, my guess is when the item is recycled it is retaining the color. I want to remove that color but removing it inside the constructor for the holder is not working so my question is how do I go about that?

评论后,这是更详细的代码

after the comment this is more detailed code

public class GridHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    public TextView Name;
    public ImageView Photo;
    public GridHolder(View itemView) {
        super(itemView);
        itemView.setOnClickListener(this);
               Name = (TextView) itemView.findViewById(R.id.name);
        Photo = (ImageView) itemView.findViewById(R.id.photo);
        itemView.setClickable(true);
    }

    @Override
    public void onClick(View view) {

            view.setBackgroundColor(Color.argb(64, 183, 28, 28));
        }
    }

推荐答案

在我看来,实现此目的的最简单方法是为RecyclerView中的每个项目赋予一个状态(布尔值)并根据该状态设置背景.这样,当您回收视图时,将绘制正确的背景.

The simplest way to achieve this in my opinion is to give each item in your RecyclerView a state (boolean) and set the background depending on the state. This way when you recycle the view, the correct background is drawn.

if(isClicked){
    itemView.setBackgroundColor(Color.RED);
}
else{
    itemView.setBackgroudnColor(Color.WHITE);
}

这篇关于回收的recyclerview项目保留了旧的背景色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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