Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RecyclerView items duplicate and constantly changing

What's Happening:

The list (RecyclerView) is mixing up the data when I scroll.

I.E when I scroll back up after scrolling down, some of the list items are repeated, not displaying 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 maybe some works have to be done with the recycling process of RecyclerView but nothing i tried seesm to work.

like image 970
James Nguyen Avatar asked Sep 24 '15 21:09

James Nguyen


People also ask

Why does RecyclerView repeat?

RecyclerView asks the adapter to create a new list item view for the first data item in your list. Once it has the view, it asks the adapter to provide the data to draw the item. This process repeats until the RecyclerView doesn't need any more views to fill the screen.

What is LayoutManager in RecyclerView?

A LayoutManager is responsible for measuring and positioning item views within a RecyclerView as well as determining the policy for when to recycle item views that are no longer visible to the user.

How many times is called onCreateViewHolder?

By default it have 5. you can increase as per your need. Save this answer.

What is nested RecyclerView?

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.


2 Answers

done .

@Override public long getItemId(int position) {     return position; }  @Override public int getItemViewType(int position) {    return position; } 
like image 80
Sujit Yadav Avatar answered Oct 02 '22 18:10

Sujit Yadav


Only two things in your RecyclerView adapter

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

and in constructor of adapter

 setHasStableIds(true); 

Hope it helps!

like image 32
Dhrupal Avatar answered Oct 02 '22 19:10

Dhrupal