What\'s Happening:
The list (RecyclerView)
is mixing up the data when I scroll.
I.E when I scr
done .
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getItemViewType(int position) {
return position;
}
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!
If anybody using kotlin, use like inside RecyclerView.Adapter
The setHasStableId(true) is to be applied to the adapter of RecylerView
init {
setHasStableIds(true);
}
Override the position with product id
override fun getItemId(position: Int): Long {
return myList?.get(position).id
}
Just add
override fun getItemViewType(position: Int): Int {
return position
}
I had the same problem with a big grid recyclerview with hundreds of numbers that should have appeared sequentially but their order was getting mixed up and repeated.
// More concise code with Kotlin expressions
override fun getItemId(position: Int) = position.toLong()
override fun getItemViewType(position: Int) = position
Added above lines to the adapter.
In kotlin with clearing the previous items from the LinearLayout list item from the 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
}