Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android RecyclerView Duplicate Item When Scrolling

I have a problem in RecyclerView. When I move item in RV and then scroll, saw some items has duplicated.

like image 570
RayaCoder Avatar asked May 29 '16 06:05

RayaCoder


3 Answers

I know its late but hope it will help someone. Override these two methods in your adapter.

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

@Override
public int getItemViewType(int position) {
   return position;
}
like image 192
Zubair Rehman Avatar answered Nov 14 '22 03:11

Zubair Rehman


I think I'm late in here but anyway I'll suggest a way that worked good for me, maybe someone still facing problems with this..
So, I added my recyclerview inside a nestedScrollView then disabled nested scrolling for my recyclerview.

Using this method the scrolling will be detected by the nestedScrollView, and the recyclerview stopped duplicating items while scrolling.

That's my xml code:

<androidx.core.widget.NestedScrollView
     android:layout_width="match_parent"
     android:layout_height="match_parent">

     <androidx.recyclerview.widget.RecyclerView
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:nestedScrollingEnabled="false"/>

</androidx.core.widget.NestedScrollView>
like image 6
Hussein Nasereddine Avatar answered Nov 14 '22 02:11

Hussein Nasereddine


RecyclerView will recycle the view.When you delete data,call notifyItemChanged(pos)or notifyDataSetChanged() method.

like image 1
moonChen Avatar answered Nov 14 '22 04:11

moonChen