如何在Recyclerview中将所选项目位置更改为顶部?

Ana*_*van 1 android recycler-adapter android-recyclerview

我有recyclerview,我想在选择项目之后动态地将项目的位置更改为顶部recyclerview.

请建议实现上述问题的最佳方法?

ami*_*ava 8

您需要将所选项目与列表中的顶部项目交换,然后通知适配器有关位置更改的信息.示例代码如下所示:

Collections.swap(orderItems.getItems(), position, 0);
                notifyItemMoved(position, 0);
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你!


Nil*_*hod 5

尝试这个

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 
Run Code Online (Sandbox Code Playgroud)

要么

recyclerview.getLayoutManager().scrollToPosition(0)  // scroll recyclerview to top 
Run Code Online (Sandbox Code Playgroud)

要么

recyclerview.smoothScrollToPosition(0);  // scroll recyclerview to top 
Run Code Online (Sandbox Code Playgroud)