我一直在尝试使用“ItemTouchHelper.Callback”为 RecyclerView 中的项目实现“滑动菜单”。我首先要做的是使项目停止在某个位置,而不是一直滑回(X)位置 0。
对于这个解决方案,我只是按照 Medium 上的教程进行操作,它应该如上所述: https ://codeburst.io/android-swipe-menu-with-recyclerview-8f28a235ff28
下面的代码与教程中的代码不是 100% 相同,因为我还想实现一个项目拖动功能(有效)。
但是,我确实尝试一步一步地遵循教程,但没有任何运气。对于此特定功能,我找不到任何其他有用的教程等
到目前为止,这是我的 ItemTouchHelper.Callback 实现代码:
enum class ButtonsState {
GONE,
VISIBLE
}
class SwipeItemHelper : ItemTouchHelper.Callback {
private var mAdapter: ItemTouchHelperAdapter
private var swipeBack: Boolean = true
private var buttonShowedState: ButtonsState = ButtonsState.GONE
private val buttonsWidth: Float = 300f
constructor(adapter: ItemTouchHelperAdapter) {
mAdapter = adapter
}
override fun onChildDraw(c: Canvas, recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder, dX: Float, dY: Float, actionState: Int, isCurrentlyActive: Boolean) {
if (actionState == ACTION_STATE_SWIPE) {
setTouchListener(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive)
}
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive)
}
private fun setTouchListener(c: Canvas, recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder, dX: Float, dY: Float, actionState: Int, isCurrentlyActive: Boolean) {
recyclerView.setOnTouchListener { v, event ->
swipeBack = event.action == MotionEvent.ACTION_CANCEL || event.action == MotionEvent.ACTION_UP
if (swipeBack) {
if (dX < -buttonsWidth)
buttonShowedState = ButtonsState.VISIBLE
if (buttonShowedState !== ButtonsState.GONE) {
setTouchDownListener(c, recyclerView, viewHolder, -buttonsWidth, dY, actionState, isCurrentlyActive)
setItemsClickable(recyclerView, false)
}
}
false
}
}
// SwipeController.java
private fun setTouchDownListener(c: Canvas,
recyclerView: RecyclerView,
viewHolder: RecyclerView.ViewHolder,
dX: Float, dY: Float,
actionState: Int, isCurrentlyActive: Boolean) {
recyclerView.setOnTouchListener { v, event ->
if (event.action == MotionEvent.ACTION_DOWN) {
setTouchUpListener(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive)
}
false
}
}
private fun setTouchUpListener(c: Canvas,
recyclerView: RecyclerView,
viewHolder: RecyclerView.ViewHolder,
dX: Float, dY: Float,
actionState: Int, isCurrentlyActive: Boolean) {
recyclerView.setOnTouchListener { v, event ->
if (event.action == MotionEvent.ACTION_UP) {
SwipItemHelper@super.onChildDraw(c, recyclerView, viewHolder, 0f, dY, actionState, isCurrentlyActive)
recyclerView.setOnTouchListener { v, event -> false }
setItemsClickable(recyclerView, true)
swipeBack = false
buttonShowedState = ButtonsState.GONE
}
false
}
}
private fun setItemsClickable(recyclerView: RecyclerView,
isClickable: Boolean) {
for (i in 0 until recyclerView.childCount) {
recyclerView.getChildAt(i).isClickable = isClickable
}
}
override fun convertToAbsoluteDirection(flags: Int, layoutDirection: Int): Int {
if (swipeBack) {
swipeBack = false
return 0
}
return super.convertToAbsoluteDirection(flags, layoutDirection)
}
override fun getMovementFlags(recyclerView: RecyclerView?, viewHolder: RecyclerView.ViewHolder?): Int {
val dragFlags = ItemTouchHelper.UP or ItemTouchHelper.DOWN
val swipeFlags = ItemTouchHelper.START
return makeMovementFlags(dragFlags, swipeFlags)
}
override fun onMove(recyclerView: RecyclerView?, viewHolder: RecyclerView.ViewHolder?, target: RecyclerView.ViewHolder?): Boolean {
if (viewHolder == null || target == null)
return false
mAdapter.onItemMove(viewHolder.getAdapterPosition(), target.getAdapterPosition())
return true
}
override fun isLongPressDragEnabled(): Boolean {
return true
}
override fun onSwiped(viewHolder: RecyclerView.ViewHolder?, direction: Int) {
}
}