RecyclerView elements are duplicated and constantly changing

What's happening:

The list (RecyclerView) mixes the data when scrolling.

IE , when I scroll through the backup after scrolling down, some list items are repeated without displaying the proper content.

 package jamesnguyen.newzyv2.UI_update; import android.content.Context; import android.support.v7.widget.CardView; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import java.util.List; import jamesnguyen.newzyv2.R; import jamesnguyen.newzyv2.RSS_Processcors.RssItem; public class RVAdapter extends RecyclerView.Adapter<RVAdapter.FeedViewHolder> { private static List<RssItem> items = null; private static Context context; public RVAdapter(Context context, List<RssItem> items) { this.items = items; this.context = context; } public static List<RssItem> getItems() { return items; } @Override public RVAdapter.FeedViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) { View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item, viewGroup, false); return new FeedViewHolder(v); } @Override public void onBindViewHolder(RVAdapter.FeedViewHolder holder, int position) { FeedViewHolder.getTitle().setText(items.get(position).getTitle()); FeedViewHolder.getPubDate().setText(items.get(position).getPubDate()); //FeedViewHolder.getDescription().setText(items.get(position).getDescription()); } @Override public long getItemId(int id) { return id; } @Override public int getItemCount() { return items.size(); } @Override public void onAttachedToRecyclerView(RecyclerView recyclerView) { super.onAttachedToRecyclerView(recyclerView); } public static class FeedViewHolder extends RecyclerView.ViewHolder { private static CardView cv; private static TextView title; private static TextView pubDate; private static TextView description; FeedViewHolder(View itemView) { super(itemView); cv = (CardView) itemView.findViewById(R.id.cv); title = (TextView) itemView.findViewById(R.id.title); pubDate = (TextView) itemView.findViewById(R.id.pubDate); //description = (TextView) itemView.findViewById(R.id.description); } public static TextView getTitle() { return title; } public static TextView getPubDate() { return pubDate; } public static TextView getDescription() { return description; } } } 

I believe that maybe some work should be done with the RecyclerView recycling process, but I have not tried anything to work.

+13
source share
7 answers

done.

 @Override public long getItemId(int position) { return position; } @Override public int getItemViewType(int position) { return position; } 
+41
source

Only two things in your RecyclerView adapter

  @Override public long getItemId(int position) { return position; } 

and in the adapter constructor

  setHasStableIds(true); 

Hope this helps!

+33
source

Override these methods in your adapter class as follows.

 @Override public long getItemId(int position) { return position; } @Override public int getItemViewType(int position) { return position; } 
+17
source

The problem is that CardView and TextView objects are declared static inside the FeedViewHolder . This means that all calls trying to set the header in the onBindViewHolder method are in the last bloated view.

Fix this to remove static from cv , title , pubDate , description , and then implement some non-static setters, for example:

 public void setTitle(String s) { title.setText(s); } 

Called in the onBindViewHolder method:

 @Override public void onBindViewHolder(RVAdapter.FeedViewHolder holder, int position) { holder.setTitle(items.get(position).getTitle()); //... } 
+6
source

I had the same problem with a large view of grids with hundreds of numbers that were supposed to appear in sequence, but their order was mixed up and repeated.

 // More concise code with Kotlin expressions override fun getItemId(position: Int) = position.toLong() override fun getItemViewType(position: Int) = position 

Added the above lines to the adapter.

+4
source

In kotlin with clearing previous items from a LinearLayout list item from RecyclerView

 override fun onBindViewHolder(view: MyViewHolder, index: Int) { view.guideLinearLayout.removeAllViews() /* do binding here*/ } override fun getItemId(position: Int): Long { return position.toLong() } override fun getItemViewType(position: Int): Int { return position } 
+2
source

If someone uses kotlin, use as inside RecyclerView.Adapter

SetHasStableId (true) should apply to the RecylerView adapter

  init { setHasStableIds(true); } 

Override line item with product id

 override fun getItemId(position: Int): Long { return myList?.get(position).id } 
0
source

All Articles