How to change selected item position to top in Recyclerview?

百般思念 提交于 2019-12-08 14:03:28

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!

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 

add below code in onclick of item,

String removedItem = actualList.remove(position); 
//actualList is list used to populate recylerview and position is selected 
//item position
actualList.add(0,removedItem);
notifyDataSetChanged();

If you have different cases like this, we can achieve this approach from @manohar-reddy and call notifyItemChanged(position) it will also call the onBindViewHolder, then update your layout accordingly.

*In this case, if the 0 item has a different layout from the other items

You might wanna read this reference for getting the item position correctly

thanks to @amit-srivastava answer and @manohar-reddy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!