recyclerview实现拖拽功能可以借助原生提供的ItemTouchHelpter类来实现,基础拖拽和删除功能都封装的有,细节需要自己处理下。
先定义一个接口,定义一下方法,方便和adapter联动处理数据,相对ItemTouchHelpter.call 增加一个判断当前所选item是否能够拖拽的判断。
interface RecyclerItemDragTouchHelper { /** * 交换 * * @param source * @param target */ fun onItemMove(source: RecyclerView.ViewHolder?, target: RecyclerView.ViewHolder?) /** * 选中 * * @param source */ fun onItemSelect(source: RecyclerView.ViewHolder?, actionState: Int) /** * 状态清除 * * @param source */ fun onItemClear(source: RecyclerView.ViewHolder?) /** * item是否可以拖拽 * * @param source */ fun isCanMove(position: Int): Boolean }
需要目标adapter 继承RecyclerItemDragTouchHelper接口实现接口中方法
定义两个变量记录数据交换后要刷新的位置,避免出现混乱问题。
var startPosition = -1 var endPosition = -1
override fun onItemMove(source: RecyclerView.ViewHolder?, target: RecyclerView.ViewHolder?) { if (!isCanMove(source?.bindingAdapterPosition ?: 0)) return if (!isCanMove(target?.bindingAdapterPosition ?: 0)) return val fromPosition = source?.bindingAdapterPosition ?: 0 val toPosition = target?.bindingAdapterPosition ?: 0 if (fromPosition < items.size && toPosition < items.size) { Collections.swap(items, fromPosition, toPosition) notifyItemMoved(fromPosition, toPosition) } onItemClear(source) endPosition = toPosition } override fun onItemSelect(source: RecyclerView.ViewHolder?, actionState: Int) { if (!isCanMove(source?.layoutPosition ?: 0)) return if (actionState != ACTION_STATE_IDLE) { source?.itemView?.scaleX = 1.2f source?.itemView?.scaleY = 1.2f } if (source != null && actionState != ACTION_STATE_IDLE) { // 非闲置状态下,记录下起始 position startPosition = source.layoutPosition } if (actionState == ACTION_STATE_IDLE) { // 只刷新交换两个以及中间数据 看自己需求 // notifyItemRangeChanged( // startPosition.coerceAtMost(endPosition), // abs(startPosition - endPosition) + 1 // ) //全部刷新 notifyItemRangeChanged(0,items.size-1) } } override fun onItemClear(source: RecyclerView.ViewHolder?) { source?.itemView?.scaleX = 1.0f source?.itemView?.scaleY = 1.0f } //此方法判断某个viewholder是否指出拖拽 override fun isCanMove(position: Int): Boolean = !items[position].isAdd()
主要还是得靠ItemTouchHelper实现,自定义一个类继承ItemTouchHelper.Callback,实现其方法,通过RecyclerItemDragTouchHelper与adapter 关联,这样方便处理。
class RecyclerItemDragTouchHelperCallback(private val helper: RecyclerItemDragTouchHelper ) : ItemTouchHelper.Callback() { override fun getMovementFlags( recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder ): Int { val dragFlags = ItemTouchHelper.UP or ItemTouchHelper.DOWN or ItemTouchHelper.LEFT or ItemTouchHelper.RIGHT return makeMovementFlags( if (helper.isCanMove(viewHolder.bindingAdapterPosition)) dragFlags else 0, 0 ) } override fun onMove( recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder, target: RecyclerView.ViewHolder ): Boolean { helper.onItemMove(viewHolder, target) return true } override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) { } override fun isLongPressDragEnabled(): Boolean { return true } override fun isItemViewSwipeEnabled(): Boolean { return false } override fun clearView(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder) { super.clearView(recyclerView, viewHolder) if (!recyclerView.isComputingLayout) { helper.onItemClear(viewHolder) } } override fun onSelectedChanged(viewHolder: RecyclerView.ViewHolder?, actionState: Int) { super.onSelectedChanged(viewHolder, actionState) helper.onItemSelect(viewHolder,actionState) } }
然后再绑定RecyclerView即可
// 适配器添加拖拽回调 val callback = RecyclerItemDragTouchHelperCallback(adapter) val itemTouchHelper = ItemTouchHelper(callback) // 为recyclerView添加拖拽功能 itemTouchHelper.attachToRecyclerView(binding.rvList)