AndroidKotlinreorder recyclerview,在项目被持有时使用ItemTouchHelper调用函数

hc8w905p  于 2023-08-07  发布在  Kotlin
关注(0)|答案(2)|浏览(89)

我有一个recycler视图,它的列表存储在firestore中。我想让用户使用拖放重新排序的项目。当用户删除hold(放下项目)时,我想调用函数来更新firestore,但每当位置改变时,即使拖动到所需的位置,函数也会被调用。如何仅在放置物品时调用函数。

val itemTouchHelper = ItemTouchHelper(
        object: ItemTouchHelper.SimpleCallback(ItemTouchHelper.UP or ItemTouchHelper.DOWN, 0){
            override fun onMove(
                recyclerView: RecyclerView,
                source: RecyclerView.ViewHolder,
                target: RecyclerView.ViewHolder
            ): Boolean {
                val sourcePosition = source.adapterPosition
                val targetPosition = target.adapterPosition

                Collections.swap(itemList, sourcePosition, targetPosition)
                adapter.notifyItemMoved(sourcePosition, targetPosition)

// Firestore update
                updateFirestoreList()

                return true
            }

            override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
                TODO("Not yet implemented")
            }
        })

字符串

atmip9wb

atmip9wb1#

当您选择拖动时,存储此位置

var sourcePosition = -1
override fun onSelectedChanged(viewHolder: RecyclerView.ViewHolder?, actionState: Int) {
     super.onSelectedChanged(viewHolder, actionState)
     if(actionState != ItemTouchHelper.ACTION_STATE_IDLE){
           sourcePosition = viewHolder!!.adapterPosition
     }
}

字符串
当你放下clearView将被调用,获取ViewHolder的位置并交换Collection

override fun clearView(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder) {
    super.clearView(recyclerView, viewHolder)
    val targetPosition = viewHolder.adapterPosition
    if(sourcePosition != -1 && sourcePosition != targetPosition){
        Collections.swap(itemList, sourcePosition, targetPosition)
    }
    sourcePosition = -1
}


希望对你有帮助:))

w51jfk4q

w51jfk4q2#

我实现它的方法是重写ItemTouchHelperclearView()方法,并从该方法调用回调。其内容如下:

class MyTouchHelper(val onActionEnded: () -> Unit) : ItemTouchHelper.SimpleCallback(UP or DOWN, 0) {

    override fun onMove(
        recyclerView: RecyclerView,
        viewHolder: RecyclerView.ViewHolder,
        target: RecyclerView.ViewHolder,
    ): Boolean {
        recyclerView.adapter?.let { adapter ->
            adapter as MyListAdapter
            val from = viewHolder.absoluteAdapterPosition
            val to = target.absoluteAdapterPosition
            adapter.onItemMoved(from, to)
            return true
        }
        return false
    }

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

    override fun clearView(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder) {
        super.clearView(recyclerView, viewHolder)
        onActionEnded()
    }
}

字符串
onItemMovied(from, to)MyListAdapter类中的一个函数,它的实现如下所示:

fun onItemMoved(from: Int, to: Int) {
    val list = currentList.toMutableList()
    list.add(to, list.removeAt(from))
    submitList(list)
}


它只是用新项目的位置更新列表(您可能有不同的方法,可以根据需要随意使用它,也可以根本不使用它)。@ vietohoang implementation for updating the list itself seems more efficient.我的天啊!我建议你试一下。
最后,在附加MyTouchHelper的地方,可以使用它的回调来调用特定的函数,如下所示:

ItemTouchHelper(MyTouchHelper {
    updateFirestoreList()// Or any other functions that you want.
}).attachToRecyclerView(recyclerView)


我希望能帮上忙。

相关问题