Skip to content Skip to sidebar Skip to footer

Move Recyclerview Clicked Item To The Top

I have a recyclerView with n items. Each item is expandable onCLick of it. I want my item to expand as well as move to the top onCLick. Suppose if I click third item then it should

Solution 1:

You can do like this way: Create one method in adaptor as below.

publicvoidswapeItem(int fromPosition,int toPosition){
     Collections.swap(arrayList, fromPosition, toPosition);
     notifyItemMoved(fromPosition, toPosition);
 }

now on item click you can use like this way.

swapeItem(9,0); // here 9 is a clicked item position and 0 means at top of the list.

Solution 2:

Moving to the top? Sure! 🙋‍♀️

This creates a nice animation with Kotlin, rendering the scrolling invisible to the eye:

In the Adapter:

Collections.swap(list, fromPosition, 0)
notifyItemMoved(fromPosition, 0)
// TODO: Callback to the fragment through listener or ViewModel

In the Fragment:recyclerView.layoutManager?.scrollToPosition(0)

Cheers!

Post a Comment for "Move Recyclerview Clicked Item To The Top"