有没有办法使用LinearLayoutManager
以编程方式将特定项目移动到RecyclerView
中的特定位置?如何以编程方式更改RecyclerView中项目的位置?
3
A
回答
6
你可以这样做:
一些活动/片段/不管:
List<String> dataset = new ArrayList<>();
RecyclerView recyclervSomething;
LinearLayoutManager lManager;
MyAdapter adapter;
//populate dataset, instantiate recyclerview, adapter and layoutmanager
recyclervSomething.setAdapter(adapter);
recyclervSomething.setLayoutManager(lManager);
adapter.setDataset(dataset);
MyAdapter:
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private List<String> dataset;
public MyAdapter() {}
//implement required methods, extend viewholder class...
public void setDataset(List<String> dataset) {
this.dataset = dataset;
notifyDataSetChanged();
}
// Swap itemA with itemB
public void swapItems(int itemAIndex, int itemBIndex) {
//make sure to check if dataset is null and if itemA and itemB are valid indexes.
String itemA = dataset.get(itemAIndex);
String itemB = dataset.get(itemBIndex);
dataset.set(itemAIndex, itemB);
dataset.set(itemBIndex, ItemA);
notifyDataSetChanged(); //This will trigger onBindViewHolder method from the adapter.
}
}
+0
我怎么能用动画做到这一点?例如,将位置n上的项目移动到列表顶部? – vtproduction
+5
如果你想动画只是移动元素,你可以使用\t \t'Collections.swap(this.mListItems,oldIndex,index); notifyItemMoved(oldIndex,newIndex)' – carvaq
相关问题
- 1. 如何以编程方式更改一个RecyclerView项目样式?
- 2. 以编程方式设置布局参数recyclerview的项目
- 3. 如何以编程方式更改LinearLayout的位置?
- 4. 如何以编程方式更改列表项目的背景?
- 5. 如何以编程方式更改项目的产品版本?
- 6. 在Android中以编程方式编辑RecyclerView项目
- 7. 以编程方式更改项目设置
- 8. 如何在android中以编程方式更改编辑文本的位置?
- 9. 如何以编程方式更改选项菜单中的项目?
- 10. 如何以编程方式更改Mac终端程序中光标的位置?
- 11. 如何以编程方式设置选项卡的位置?
- 12. 以编程方式更改Xamarin中UIBarButton项目的图标
- 13. 以编程方式更改ControlTemplate中项目的值
- 14. 以编程方式修改UIToolBar项目
- 15. 如何以编程方式更改Access中的选项?
- 16. 如何以编程方式更改Highcharts中的颜色选项?
- 17. 如何以编程方式更改MvxTabsFragmentActivity中的选项卡?
- 18. 更改RecyclerView中的项目的视图位置滚动Android
- 19. 如何以编程方式在ActionBar中更改微调器中的项目
- 20. 在Crystal Reports中以编程方式更改列宽和位置
- 21. 如何设置usercontrol的位置并以wpf C#编程方式更改它?
- 22. 如何以编程方式更改XCode项目的设备目标?
- 23. 更改IE设置以编程方式
- 24. 如何以编程方式更改card_view:cardCornerRadius
- 25. 如何以编程方式更改android:configChanges?
- 26. 如何以编程方式更改UIView?
- 27. 如何以编程方式更改此下拉菜单中的选定项目?
- 28. 如何以编程方式更改datagridview中的组合框选定项目?
- 29. 以编程方式更改视图的位置?
- 30. 以编程方式更改UltraGrid行的可见位置
正在更新数据集的选项? –