如何在回收站视图中更改项目的位置

Ash*_*Jha 2 android android-recyclerview

我正在开发具有回收站视图的 Android 应用程序。我在回收商视图中随机订购了物品。我想将列表的第五项移动到第一个位置。并将之前在第一个位置的项目移动到第二个位置。有人可以帮我吗?

Nil*_*hod 5

您可以使用 Collections.swap()

  • Swaps指定列表中指定位置的元素。(如果指定的位置相等,则调用此方法会使列表保持不变。)

方法

 public static void swap(List<?> list,
        int i,
        int j)
Run Code Online (Sandbox Code Playgroud)

参数:

list - The list in which to swap elements.
i - the index of one element to be swapped.
j - the index of the other element to be swapped.
Run Code Online (Sandbox Code Playgroud)

示例代码

// first swap the item using  Collections.swap() method
Collections.swap(yourList, firstPosition, positionToSwap);

// than notify your adapter about change in list using  notifyItemMoved() method
YourAdapter.notifyItemMoved(firstPosition, positionToSwap);
Run Code Online (Sandbox Code Playgroud)