当前位置:网站首页>Recyclerview advanced use (II) - simple implementation of vertical drag and drop sorting
Recyclerview advanced use (II) - simple implementation of vertical drag and drop sorting
2022-04-23 14:09:00 【Senzhiqianshou】
Let's take a look at the renderings to be realized :
![ Simple vertical drag and drop sorting ](https://img-blog.csdnimg.cn/20210329142737702.gif#pic_center
The effect is simple , It's a vertical list , You can then drag its children to sort .
Therefore, the method adopted is still RecyclerView+ItemTouchHelper, About ItemTouchHelper You can also refer to RecyclerView Advanced use ( One )- Simple implementation of sideslip deletion and RecyclerView A detailed study -RecyclerView Discussion and repair of click dislocation . Here we need to write a class by ourselves VerticalDragSortHelperCallBack To inherit ItemTouchHelper.Callback, We need to implement the following methods :
@Override
public int getMovementFlags(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
int dragFlag = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
return makeMovementFlags(dragFlag, ItemTouchHelper.ACTION_STATE_IDLE);
}
@Override
public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
int fromPos = viewHolder.getAdapterPosition();
int toPos = target.getAdapterPosition();
Log.d("onMove", fromPos + "--->" + toPos);
//1、 Where to exchange entries from the data source
Collections.swap(recyclerItemList, fromPos, toPos);
//2、 Notify the refresh view of changes from the view
recyclerView.getAdapter().notifyItemMoved(fromPos, toPos);
//3、 Correct the actually clicked position, Prevent when clicking ,position Disorder
int startPos = Math.min(fromPos, toPos);
int itemCount = Math.abs(fromPos - toPos) + 1;
recyclerView.getAdapter().notifyItemRangeChanged(startPos, itemCount);
//4、PS:2 and 3 The fitting step can be used 4 replace , But there is no replacement animation , And the refresh efficiency is lower than the above , Is a global refresh
// recyclerView.getAdapter().notifyDataSetChanged();
// Notice the return value : Only in return true When , To leave onMoved Method
return true;
}
@Override
public void onMoved(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, int fromPos, @NonNull RecyclerView.ViewHolder target, int toPos, int x, int y) {
super.onMoved(recyclerView, viewHolder, fromPos, target, toPos, x, y);
Log.d("onMoved", " first :" + recyclerItemList.get(0).toString() + " " + fromPos + "--->" + toPos);
Log.d("onMoved", " the second :" + recyclerItemList.get(1).toString() + " " + fromPos + "--->" + toPos);
if (onDragListener != null) {
onDragListener.onItemMoved(viewHolder, target, fromPos, toPos);
}
}
@Override
public boolean isLongPressDragEnabled() {
return true;
}
It should be noted that :
1、 Because drag vertically , therefore getMovemonetFlags The return is DOWN and UP
2、 stay onMove in ,pos Because it is calculated from zero , So the actual number of refresh entries should be +1
3、 stay onMove in , About data exchange , The system is adopted Collections Of swap In exchange for , More efficient
4、isLongPressDragEnabled Indicates whether dragging is enabled after long pressing , We need to go back to true, Indicate need
Source code download address :github
版权声明
本文为[Senzhiqianshou]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231405037238.html
边栏推荐
- org.apache.parquet.schema.InvalidSchemaException: A group type can not be empty. Parquet does not su
- 关于NodeJS中JSON5的相关配置和使用
- 报表FCRA考试题集及答案(错了11题)
- Three point positioning based on ibeacons (wechat applet)
- Can I compile the header file and source file of the template separately
- 微信小程序调用客服接口
- request模块
- smart-doc + torna生成接口文档
- 微信小程序 input隐藏和不可操作的设置
- Subscription number development of wechat applet (message push)
猜你喜欢
HyperMotion云迁移助力中国联通,青云完成某央企上云项目,加速该集团核心业务系统上云进程
DDT+Excel进行接口测试
mysql新表,自增id长达20位,原因竟是......
Autumn recruitment in 2021, salary ranking No
室内外地图切换(室内基于ibeacons三点定位)
Wechat applet positioning and ranging through low-power Bluetooth device (2)
Cdh6 based on CM management 3.2 cluster integration atlas 2 one
Detailed tutorial on the use of smoke sensor (mq-2) (based on raspberry pie 3B +)
Call wechat customer service applet
smart-doc + torna生成接口文档
随机推荐
Visio installation error 1:1935 2: {XXXXXXXX
HyperBDR云容灾V3.3.0版本发布|容灾功能升级,资源组管理功能优化
Chapter I review of e-commerce spike products
findstr不是内部或外部命令解决方法
帆软中单元格中隔行变色以及数量大于100字体变大变红设置
微信小程序 input隐藏和不可操作的设置
Essential difference between restful WebService and gSOAP webservice
如何快速批量创建文本文档?
帆软之单元格部分字体变颜色
Switch usage (wechat applet)
PATH环境变量
VMWare安装64位XP中文教程
封装logging模块
理解虚基类、虚函数与纯虚函数的概念(转)
sql中出现一个变态问题
关于训练过程中损失函数出现断崖式增长的问题
微信小程序进行蓝牙初始化、搜索附近蓝牙设备及连接指定蓝牙(一)
SQL数据库
Use of WiFi module based on wechat applet
VMware15Pro在Deepin系统里面挂载真机电脑硬盘