Drag And Drop RecyclerView Items

Before continuing this post, I recommend you read my previous one on Swipe To Dismiss RecyclerView Items as this will build upon the ItemTouchHelper class discussed there. Once you’ve done that, come back to this short post and learn how to drag and drop RecyclerView items like this:


Drag And Drop RecyclerView Items

Swap Adapter Items

Before we can properly use the drag and drop feature, we need to add another method to our Adapter that swaps two items. You can use Collections.swap() to do this easily on a List object:

Implement onMove

In our previous lesson you’ll recall we left our implementation of onMove blank. To make use of it, all we have to do is make sure it swaps our Adapter items:

Once you’ve done those two things in addition to all the necessary steps for swipe-to-dismiss, you’re done. If you chose to only use the drag and drop feature, just remove your remove() function from the adapter and the implementation of onSwap() inside the ItemTouchHelper class.

I hope this simple tutorial helps you advance your RecyclerView skills. I have updated the MovieList project on Github to include the code from this example.


Share this post...
Share on Reddit0Share on Google+0Tweet about this on TwitterShare on LinkedIn0Share on Facebook0

Leave a Reply