在RecyclerView中针对特定位置的拖放停止 [英] Drag Drop halt for Specific Position in RecyclerView

查看:109
本文介绍了在RecyclerView中针对特定位置的拖放停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何方法可以使用RecyclerView暂停项目的拖放到特定位置. 我已经实现了拖放使用以下代码删除.

Is there any way to Halt the Drag Drop of item for specific position using RecyclerView. I have achieved the Drag & Drop using below code.

    val _ithCallback = object : ItemTouchHelper.Callback() {

    //and in your implementation of
    override fun onMove(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder, target: RecyclerView.ViewHolder): Boolean {
        val fromPosition = viewHolder.adapterPosition
        val toPosition = target.adapterPosition

        // Video position
        if (fromPosition == list.size - 1 || toPosition == list.size - 1) {
            return false
        }

        if (list[fromPosition].trim().isEmpty()){
            return false
        }

        if (fromPosition < toPosition) {
            for (i in fromPosition until toPosition) {
                Collections.swap(list, i, i + 1)
            }
        } else {
            for (i in fromPosition downTo toPosition + 1) {
                Collections.swap(list, i, i - 1)
            }
        }
        adapter?.notifyItemMoved(fromPosition, toPosition)

        return true
    }

    override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {

    }

    //defines the enabled move directions in each state (idle, swiping, dragging).
    override fun getMovementFlags(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder): Int {
        return makeFlag(ItemTouchHelper.ACTION_STATE_DRAG,
                ItemTouchHelper.DOWN or ItemTouchHelper.UP or ItemTouchHelper.START or ItemTouchHelper.END)
    }
}

并像这样使用它

val ith = ItemTouchHelper(_ithCallback)
ith.attachToRecyclerView(imagesRecyclerView)

推荐答案

最后,在对 ItemTouchHelper 类进行了大量研究之后,我像这样解决了它.

Finally, after lots of research in ItemTouchHelper class i resolved it like this.

我所做的是将标志设置为ACTION_STATE_IDLE,我要在其中停止拖放.希望它能帮助像我这样的人.

What i have done is set the flag as ACTION_STATE_IDLE where i want to halt the Drag and Drop. Hope it helps someone like me.

以下是Kotlin版本.如果有人在JAVA中需要相同的内容,我可以将其转换.

Below is the Kotlin version. If anyone needs the same in JAVA i can convert it.

//defines the enabled move directions in each state (idle, swiping, dragging).
    override fun getMovementFlags(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder): Int {
        // The position i want to lock/halt
        if (list[viewHolder.adapterPosition].trim().isEmpty()){
            return makeFlag(ItemTouchHelper.ACTION_STATE_IDLE, ItemTouchHelper.DOWN or ItemTouchHelper.UP)
        }
        // The position i want to lock/halt
        if (viewHolder.adapterPosition == list.size - 1){
            return makeFlag(ItemTouchHelper.ACTION_STATE_IDLE, ItemTouchHelper.DOWN or ItemTouchHelper.UP)
        }
        // else enabling ACTION_STATE_DRAG
        return makeFlag(ItemTouchHelper.ACTION_STATE_DRAG,
                ItemTouchHelper.DOWN or ItemTouchHelper.UP or ItemTouchHelper.START or ItemTouchHelper.END)
    }

这篇关于在RecyclerView中针对特定位置的拖放停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆