Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change selected item position to top in Recyclerview?

I have recyclerview in that I want to change item's position dynamically to top after selecting the item of recyclerview.

Please suggest best way to achieve the above problem?

like image 655
Anamika Chavan Avatar asked Nov 30 '22 14:11

Anamika Chavan


2 Answers

You need to swap selected item with top item in list, then notify your adapter about position change. A sample code would look like:

Collections.swap(orderItems.getItems(), position, 0);
                notifyItemMoved(position, 0);

Hope it helps!

like image 87
amit srivastava Avatar answered Dec 03 '22 05:12

amit srivastava


Try this

String Item = List.get(position); 
List.remove(position);  // remove item from the list
List.add(0,removedItem); // add at 0 index of your list
notifyDataSetChanged();
linearLayoutManager.scrollToPositionWithOffset(0, 20); // scroll recyclerview to top 

OR

recyclerview.getLayoutManager().scrollToPosition(0)  // scroll recyclerview to top 

OR

recyclerview.smoothScrollToPosition(0);  // scroll recyclerview to top 
like image 20
AskNilesh Avatar answered Dec 03 '22 03:12

AskNilesh