Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/218.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在Recyclerview中将所选项目位置更改为顶部?_Android_Android Recyclerview - Fatal编程技术网

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

Android 如何在Recyclerview中将所选项目位置更改为顶部?,android,android-recyclerview,Android,Android Recyclerview,我有recyclerview,在选择recyclerview的项目后,我想动态地将项目的位置更改为顶部 请建议解决上述问题的最佳方法?在单击项目时添加以下代码 String removedItem = actualList.remove(position); //actualList is list used to populate recylerview and position is selected //item position actualList.add(0,removedIte

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


请建议解决上述问题的最佳方法?

在单击项目时添加以下代码

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

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 

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

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

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

Collections.swap(orderItems.getItems(), position, 0);
                notifyItemMoved(position, 0);
希望有帮助

如果您有不同的,我们可以通过调用
notifyItemChanged(position)
实现此方法,它还将调用
onBindViewHolder
,然后相应地更新您的布局

*在这种情况下,如果0项与其他项具有不同的布局

您可能需要阅读以获得正确的项目位置


感谢@amit srivastava answer和@manohar reddy

点击项目,从数组列表中删除该位置的项目,并将其添加到0位置Yes!!成功了!!谢谢关于如何将这个很棒的算法与更改的布局项结合起来,有什么建议吗?零位/第一位上的每个项目@amit srivastava零项:带有图标。其余项目:没有图标。当交换发生时,发生。当前项目没有更改布局。我尝试了您的代码,但它没有滚动到顶部位置。