使用ItemTouchHelper.Callback在RecyclerView中创建向左滑动菜单

时间:2018-09-19 21:45:58

标签: android android-recyclerview itemtouchhelper

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

    }

}

1 个答案:

答案 0 :(得分:0)

在同一教程中,我也遇到了同样的问题。

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

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

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