当前位置:网站首页>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
边栏推荐
猜你喜欢
Idea控制台乱码解决
浅谈基于openssl的多级证书,Multi-level CA的签发和管理,以及双向认证
RobotFramework 之 文件上传和下载
There is a mining virus in the server
Kettle -- control parsing
Jmeter安装教程以及我遇到的问题的解决办法
PySide2
基于ibeacons签到系统
Cdh6 based on CM management 3.2 cluster integration atlas 2 one
Wechat applet initializes Bluetooth, searches nearby Bluetooth devices and connects designated Bluetooth (I)
随机推荐
微信小程序进行蓝牙初始化、搜索附近蓝牙设备及连接指定蓝牙(一)
Logging module
Request module
org.apache.parquet.schema.InvalidSchemaException: A group type can not be empty. Parquet does not su
多重继承虚基类习题
RobotFramework 之 用例执行
星界边境文本自动翻译机使用说明
Ptorch classical convolutional neural network lenet
Node接入支付宝开放平台的沙箱实现支付功能
全局变量能否放在头文件中定义
RobotFramework 之 用例标签机制
Kettle -- control parsing
As a junior college student, I studied hard in closed doors for 56 days, won Ali offer with tears, five rounds of interviews and six hours of soul torture
Lin Lin, product manager of Lenovo: network failure of local network operator in Tianjin. The background server of Zui system can't work normally for the time being
RobotFramework 之 项目框架
Restful WebService和gSoap WebService的本质区别
帆软中单元格中隔行变色以及数量大于100字体变大变红设置
DDT+Excel进行接口测试
pthread_self()为何重复了
linux安装mysql后修改密码