Firstly, the function of dragging and sliding is added on the basis of recyclerview. It is written directly according to the official document as follows
ItemTouchHelper(object : ItemTouchHelper.Callback() {
override fun getMovementFlags(
recyclerView: RecyclerView,
viewHolder: RecyclerView.ViewHolder,
): Int {
//item dragging direction
var dragflag =
ItemTouchHelper.UP or ItemTouchHelper.DOWN or ItemTouchHelper.LEFT or ItemTouchHelper.RIGHT
return makeMovementFlags(dragflag, 0)
}
override fun onMove(
recyclerView: RecyclerView,
viewHolder: RecyclerView.ViewHolder,
target: RecyclerView.ViewHolder,
): Boolean {
myAdapter.notifyItemMoved(viewHolder.layoutPosition, target.layoutPosition)
return true
}
override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {}
override fun canDropOver(
recyclerView: RecyclerView,
current: RecyclerView.ViewHolder,
target: RecyclerView.ViewHolder,
): Boolean {
//The current ViewHolder can be placed on the target ViewHolder
return true
}
override fun isLongPressDragEnabled(): Boolean {
//Turn on long press drag
return true
}
}).attachToRecyclerView(binding.myRV)//add to RecyclerView
After running, it is found that the UI is normal, but when obtaining adapter.items, it is found that the data has not changed, so perform data exchange and sorting in onmove:
ItemTouchHelper(object : ItemTouchHelper.Callback() {
override fun getMovementFlags(
recyclerView: RecyclerView,
viewHolder: RecyclerView.ViewHolder,
): Int {
var dragflag =
ItemTouchHelper.UP or ItemTouchHelper.DOWN or ItemTouchHelper.LEFT or ItemTouchHelper.RIGHT
return makeMovementFlags(dragflag, 0)
}
override fun onMove(
recyclerView: RecyclerView,
viewHolder: RecyclerView.ViewHolder,
target: RecyclerView.ViewHolder,
): Boolean {
if (viewHolder.layoutPosition < target.layoutPosition) {
for (i in viewHolder.layoutPosition until target.layoutPosition) {
Collections.swap(myAdapter.items, i, i + 1)
}
} else {
for (i in viewHolder.layoutPosition downTo target.layoutPosition + 1) {
Collections.swap(myAdapter.items, i, i - 1)
}
}
myAdapter.notifyItemMoved(viewHolder.layoutPosition, target.layoutPosition)
myAdapter.notifyItemChanged(target.layoutPosition)
return true
}
override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {}
override fun canDropOver(
recyclerView: RecyclerView,
current: RecyclerView.ViewHolder,
target: RecyclerView.ViewHolder,
): Boolean {
return true
}
override fun isLongPressDragEnabled(): Boolean {
return true
}
}).attachToRecyclerView(binding.myRV)
If you want to listen to the end of drag, you can override the Clearview method to listen
Read More:
- [MySQL] [serialize] [error record] after modifying data, no data will be returned (in fact, MySQL does not support it)
- Django + jQuery get data in the form + Ajax send data
- Failed to load response data:No data found for resource with given identifie
- circuit_breaking_exception,“reason“:“[parent] Data too large, data for [<http_request>]
- Data analysis to obtain Yahoo stock data: some problems are encountered when using panda datareader (cannot import name ‘is_ list_ Like ‘problem)
- No data: data: get host by name failed in TCP_ Connect() error resolution
- How to Fix Sklearn ValueError: This solver needs samples of at least 2 classes in the data, but the data
- This (code, message, data: null) still exists after importing spring cloud project into Lombok; the data in the project is unrecognized
- ERROR 1406 (22001): Data Too Long, field len 30, data len 48
- Mybatis openSession.commit () manually submit data and openSession.commit (true) automatically submit data
- dfs.namenode.name . dir and dfs.datanode.data .dir dfs.name.dir And dfs.data.dir What do you mean
- Pit encountered by entity class data type BigDecimal
- The problem that the content extra data in the notification cannot be updated
- Python export data (CSV format)
- [leetcode] 295. Find Median from Data Stream Python
- Lua — using remove to delete table data
- Error converting data type nvarchar to datetime
- Additional data: error value: 2147750687
- Error reporting when Lombok @ data and @ builder are used together
- [Oracle] when inserting data, “ora-00001: unique constraint” appears