Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RecyclerView messed up data when scrolling

Tags:

Having a problem when scrolling RecyclerView after scrolling down and up. The idea is to change elements color, but when I scroll down everything is great and when the scroll goes up - the elements, which are shouldn't be colored are changing color.

Here's my adapter:

public class NotificationsAdapter extends RecyclerView.Adapter<NotificationsAdapter.ViewHolder> {  private NotificationData notificationData; private Context mContext; private ArrayList<NotificationData> infromationList = new ArrayList<>();   public NotificationsAdapter(Context context, ArrayList<NotificationData> infromationList) {     this.infromationList = infromationList;     this.mContext = context; }   @Override public NotificationsAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {      View itemLayoutView;     ViewHolder viewHolder;      itemLayoutView = LayoutInflater.from(parent.getContext())             .inflate(R.layout.notification_single_item, parent, false);     viewHolder = new ViewHolder(itemLayoutView, viewType);      return viewHolder; }  @Override public void onBindViewHolder(NotificationsAdapter.ViewHolder holder, int position) {      notificationData = infromationList.get(position);     holder.notificationDate.setText(convertDate(notificationData.getDate()));     holder.notificationStatus.setText(notificationData.getNotificationStatus());     holder.orderDescription.setText(notificationData.getNotificationLabel());      if ("true".equals(notificationData.getReadStatus())) {         holder.root.setBackgroundColor(mContext.getResources().getColor(R.color.white));         holder.notificationStatus.setTypeface(Typeface.create("sans-serif-light", Typeface.NORMAL));     }  }  @Override public int getItemCount() {     return (null != infromationList ? infromationList.size() : 0); }  public static class ViewHolder extends RecyclerView.ViewHolder {      public TextView notificationDate;     public TextView notificationStatus;     public TextView orderDescription;     public LinearLayout root;      public ViewHolder(View itemView, int position) {         super(itemView);          notificationDate = (TextView) itemView.findViewById(R.id.notificationDate);         notificationStatus = (TextView) itemView.findViewById(R.id.notificationStatus);         orderDescription = (TextView) itemView.findViewById(R.id.orderDescription);         root = (LinearLayout) itemView.findViewById(R.id.root);     }  }  private String convertDate(String date) {     String convertedDate;      String[] parts = new String[2];     parts = date.split("T");     date = parts[0];      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");     Date testDate = null;     try {         testDate = sdf.parse(date);     }catch(Exception ex){         ex.printStackTrace();     }     SimpleDateFormat formatter = new SimpleDateFormat("dd.mm.yyyy");     convertedDate = formatter.format(testDate);      return convertedDate; } } 
like image 719
k.nadonenko Avatar asked May 06 '15 16:05

k.nadonenko


2 Answers

I had the same problem and the only solution I found for this is:

holder.setIsRecyclable(false); 

Your recycler will not recycle anymore so the items will be the same when you scroll, and if you want to delete some item do not use notifyitemRemoved(position), use notifyDataSetChanged() instead.

like image 148
Jhonatan Sabadi Avatar answered Sep 21 '22 01:09

Jhonatan Sabadi


Add setHasStableIds(true); in your adapter constructor and Override these two methodes in adapter.

@Override public long getItemId(int position) {             return position; }  @Override public int getItemViewType(int position) {        return position; } 
like image 26
Nathan Teyou Avatar answered Sep 19 '22 01:09

Nathan Teyou