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?
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With