[RecyclerView & ItemTouchHelper]After setPadding to RecyclerView, it fail to drag item?

  android, android-recyclerview, itemtouchhelper

1.itemTouchHelper works well at first.
Kotlin code as followed

val itemTouchHelper = ItemTouchHelper(object: ItemTouchHelper.Callback(){
    
    override fun getMovementFlags(recyclerView: RecyclerView, viewHolder:RecyclerView.ViewHolder): Int{
        val dragFlag = ItemTouchHelper.UP | ItemTouchHelper.Down
        val swipeFlags = 0
        return makeMovementFlags(dragFlags, swipeFlags)
    }

    override fun onMove(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder, target: RecyclerView.ViewHolder){
        
        val fromPosition = viewHolder.adapterPosition
        val toPosition = target.adapterPosition
        Collections.swap(mList, fromPosition, toPosition)
        mAdapter.notifyItemMoved(fromPosition, toPosition)

    }

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

    }

})

itemTouchHelper.attachToRecyclerView(mRecyclerView)

enter image description here

2.if I setPadding to RecyclerView, there is something wrong when I try to drag item

mRecyclerView.setPadding(0,250,0,0) // (left, top, right ,bottom)

enter image description here

we can drag items easily when recyclerview is at its top(recyclerview has not been scrolled, it is at its top). However, if the recyclerview is not at its top(recyclerview has been scrolled downwards), when I drag the item it will go to the toppest position immediately instead of the position I drag to.

watch the gif for more details, somebody help me 🙁

Source: Android Questions

LEAVE A COMMENT