RecyclerView ambiguos setVisibility函数,单击一个视图会影响多个视图 [英] RecyclerView ambiguos setVisibility function, clicking on one view affects multiple views

查看:132
本文介绍了RecyclerView ambiguos setVisibility函数,单击一个视图会影响多个视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我要运行的项目.这是我的RecyclerView.Adapter类中的onBindViewHolder的代码

This is the project I am trying to run. Here is my code for the onBindViewHolder from RecyclerView.Adapter class

@Override
    public void onBindViewHolder(ViewHolder holder, final int position) {

        TextView title = (TextView) holder.view.findViewById(R.id.title);
        final TextView desc = (TextView) holder.view.findViewById(R.id.desc);
        final ImageView imageView = (ImageView) holder.view.findViewById(R.id.imageView);

        title.setText(pojos.get(position).getTitle());
        desc.setText(pojos.get(position).getDesc());

        imageView.setImageResource(pojos.get(position).getImage());

        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                desc.setText("clicked");
                desc.setBackgroundColor(Color.BLUE);
                imageView.setImageResource(R.drawable.heart_red);
            }
        });

    }

列表加载正常,调用imageView的onclicklistener时会发生问题.

The list loads fine, the problem happens when the imageView's onclicklistener is called.

desc.setText("clicked");

上面的行在单击它的列表项中进行更改.但是

The line above makes the change in the list item on which it was clicked on. but

 desc.setBackgroundColor(Color.BLUE);

执行此行时,更改将反映在列表中的多个项目中.怎么了?在下面显示的图片中,我单击了项目0,文本更改为已单击"并设置了颜色.但是,当我向下滚动时,单击项目0也会影响项目12.仅反映了背景颜色变化,而没有反映文本变化.我该如何停止?

when this line is executed, the change reflects in multiple items on the list. What is going wrong? In the pictures shown below, I clicked on item 0, the text changes to "clicked" and color is set. But when I scroll down, item 12 has also been affected from my click on item 0. Only the background color change has reflected, not the text change. How do I stop this?

很长一段时间以来,我一直在尝试解决此问题,请下载项目并尝试执行代码以了解我的确切意思(如果我的问题不清楚).

I have been trying to solve this for a long time, kindly download the project and try executing the code to understand what I exactly mean, if my question is not clear.

推荐答案

之所以会发生这种情况,是因为视图被回收再利用.

This happens because the views get recycled and reused.

因此,在回收视图时,如果您不再次对其进行更改,它将保留旧"视图的属性.因此,当您向下滚动到数字12时,用于保留数字1的视图将被回收(因为不再可以在屏幕上看到它),并用于创建数字12.这就是为什么蓝色在数字上的原因12.

So when the view gets recycled, it retains properties of the "old" view if you don't change them again. So when you scroll down to number 12, the view that used to hold number 1 gets recycled (as it can't be seen on the screen anymore), and is used to create number 12. This is why the blue color is on number 12.

例如,单击该项目时,您需要将"clicked"值保存到POJO对象中.然后,在绘制项目时,检查该值并根据该值设置正确的图像/背景色.

When the item is, for example, clicked, you'll need to save a "clicked" value into your POJO object. Then when the item is drawn, check that value and set the correct image / background color depending on that value.

我已经在下面的代码中完成了此操作,因此它应该使您大致了解要做什么:

I've done this in the below code, so it should give you a rough idea of what to do:

@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
    TextView title = (TextView) holder.view.findViewById(R.id.title);
    final TextView desc = (TextView) holder.view.findViewById(R.id.desc);
    final ImageView imageView = (ImageView) holder.view.findViewById(R.id.imageView);

    final MyPojo pojo = pojos.get(position);

    title.setText(pojo.getTitle());
    if(!pojo.clicked) {
        desc.setText(pojo.getDesc());
        imageView.setImageResource(pojo.getImage());
        desc.setBackgroundColor(Color.argb(0,0,0,0));
    } else {
        desc.setText("clicked");
        desc.setBackgroundColor(Color.BLUE);
        imageView.setImageResource(R.drawable.heart_red);
    }

    imageView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            pojo.clicked = true;
            desc.setText("clicked");
            desc.setBackgroundColor(Color.BLUE);
            imageView.setImageResource(R.drawable.heart_red);
        }
    });
}

并且我在MyPojo类中添加了一个单击的"布尔值.

And i've added a "clicked" boolean to the MyPojo class.

public class MyPojo {

    String title;
    String desc;
    int image;
    boolean clicked;
 }

这篇关于RecyclerView ambiguos setVisibility函数,单击一个视图会影响多个视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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