当前位置:网站首页>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 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
边栏推荐
- POI operation word template replaces data and exports word
- Autumn recruitment in 2021, salary ranking No
- 查询2013年到2021年的数据,只查询到2020的数据,遇到了这个问题所进行的解决办法
- Jacob print word
- CentOS mysql多实例部署
- PySide2
- leetcode--977. Squares of a Sorted Array
- 微信小程序 input隐藏和不可操作的设置
- Restful WebService和gSoap WebService的本质区别
- mysql通过binlog文件恢复数据
猜你喜欢
连接公司跳板机取别名
浅谈基于openssl的多级证书,Multi-level CA的签发和管理,以及双向认证
POI operation word template replaces data and exports word
Three point positioning based on ibeacons (wechat applet)
Wechat applet initializes Bluetooth, searches nearby Bluetooth devices and connects designated Bluetooth (I)
快速安装mongodb
RecyclerView高级使用(一)-侧滑删除的简单实现
基于ibeacons签到系统
PATH环境变量
Indoor and outdoor map switching (indoor three-point positioning based on ibeacons)
随机推荐
RecyclerView高级使用(一)-侧滑删除的简单实现
Cdh6 based on CM management 3.2 cluster integration atlas 2 one
smart-doc + torna生成接口文档
Mock测试
HyperMotion云迁移助力中国联通,青云完成某央企上云项目,加速该集团核心业务系统上云进程
关于云容灾,你需要知道这些
Postman的安装使用及填坑心得
Check in system based on ibeacons
微信小程序调用客服接口
PyMySQL
JSP学习2
PySide2
Understand the concepts of virtual base class, virtual function and pure virtual function (turn)
Wechat applet communicates with low-power Bluetooth - sending data to hardware (III)
VMware 15pro mounts the hard disk of the real computer in the deepin system
Jmeter安装教程以及我遇到的问题的解决办法
基于ibeacons签到系统
CDH cluster integration Phoenix based on CM management
Three point positioning based on ibeacons (wechat applet)
leetcode--357. Count the number of different figures