gpt4 book ai didi

android - 如何更改回收站 View 中项目的位置

转载 作者:行者123 更新时间:2023-11-29 16:34:35 24 4
gpt4 key购买 nike

我正在开发一个具有回收器 View 的 Android 应用程序。我在回收者 View 中随机订购了一些元素。我想将列表的第五项移动到第一个位置。并将之前位于第一个位置的项目移动到第二个位置。有人可以帮我解决这个问题吗?

最佳答案

您可以使用Collections.swap()

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

方法

 public static void swap(List<?> list,
int i,
int j)

参数:

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.

示例代码

// 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);

关于android - 如何更改回收站 View 中项目的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52833836/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com