iPaulPro / Android-ItemTouchHelper-Demo

Basic example of using ItemTouchHelper to add drag & drop and swipe-to-dismiss to RecyclerView.
Apache License 2.0
2.51k stars 499 forks source link

How to do drag and drop with different view types? #54

Open kokilaw opened 6 years ago

kokilaw commented 6 years ago

I need to implement the drag and drop for items with different view types. I tried with following code

override fun onMove(recyclerView: RecyclerView, source: RecyclerView.ViewHolder, target: RecyclerView.ViewHolder): Boolean {
        // Notify the adapter of the move
        mAdapter.onItemMove(source.adapterPosition, target.adapterPosition)
        return true
}

without

if (source.itemViewType != target.itemViewType) {
            return false
}

but it doesn't work as I expected. I'm using a grid layout by the way.

safakesberk commented 6 years ago

Did you change getItemViewType, onCreateViewHolder and onBindViewHolder methods in your adapter class?

kokilaw commented 6 years ago

Yes. RecylerView is working fine with different view types. Drag and drop between different view types is the place that that I'm stuck at.

AndroidDeveloperLB commented 5 years ago

To decide if a viewHolder should be drag-able and/or swipe-able, I think you need to do it in the getMovementFlags function. There you have a change to decide if you want to allow drag and/or swipe for the given ViewHolder.

To decide if a viewHolder can be switched with another one, you have onMove . Here, made a sample for this : https://github.com/AndroidDeveloperLB/RecyclerViewDragAndDropTest

Weird thing is that the animation might be weird in some cases. Reported it here: https://issuetracker.google.com/issues/138465468