Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RecyclerView ambiguos setVisibility function, clicking on one view affects multiple views

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);
            }
        });

    }

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);

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?

enter image description here

enter image description here

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.

like image 908
55597 Avatar asked Jun 01 '15 22:06

55597


People also ask

Why is RecyclerView used select one?

Recycler View you could say is an efficient way to create list of views. If you have 1000 items like ur contact list , and If ur visible screen can show only 10 items at once, it will Create only 10+1 (or +2) Views and as u scroll , items/views that left will be reused (not create) to show new data.

What is nested recycler view?

A nested RecyclerView is an implementation of a RecyclerView within a RecyclerView. An example of such a layout can be seen in a variety of apps such as the Play store where the outer (parent) RecyclerView is of Vertical orientation whereas the inner (child) RecyclerViews are of horizontal orientations.

In what situation should one use RecyclerView over ListView?

Simple answer: You should use RecyclerView in a situation where you want to show a lot of items, and the number of them is dynamic. ListView should only be used when the number of items is always the same and is limited to the screen size.


1 Answers

Just add a method in your adapter class after the getItemCount method

@Override
    public int getItemViewType(int position) {
        return position;
    }

it will solve the problem

like image 126
DalveerSinghDaiya Avatar answered Oct 17 '22 08:10

DalveerSinghDaiya