1

我一直在尝试使用“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) {

    }

}
4

1 回答 1

0

在同一个教程之后,我也在努力解决同样的问题。

您可以在onChildDraw中以这种方式限制滑动:

val newDX:Float = max(dX, -buttonWidth)
super.onChildDraw(c, recyclerView, viewHolder, newDX, dY, actionState, isCurrentlyActive)

如果您滑动超出按钮宽度,这会将newDX设置为buttonWidth的值,因此您的列表项将被正确绘制。

于 2019-06-27T12:18:56.490 回答