Check Mobile App
Read now

Drag and Drop Item on RecyclerView

Save

Hello everyone, today’s topic is about how to drag an item on recycler view and drop that item on another position.

 

Firstly, we have to create a new project and add the dependencies on the app build. gradle file.

and also enable data binding on the app build.gradle file.

Now we can start working on the coding to add drag and drop functionality on recycler view.

  1. Create the layout having the recycler view: activity_language.xml
  2. For now let’s  add the layout for an item let’s name it: item_language.xml
  3. The code for the adapter class let’s name it: LanguageRvAdapter.java
  4. We are almost complete and now on the activity file, we will start to implement the code for recycler view, drag and drop, let’s name the activity file as LanguageActivity.kt.

    On this, we have created an object of ItemTouchHelper. Callback through which we will be able to implement the drag and drop feature, inside this, we have overridden multiple methods:

    • getMovementFlags -> We have to pass the directions of drag(Like ItemTouchHelper.UP or ItemTouchHelper.DOWN) and for swipe, we have passed the flag 0.
    • onMove -> On this method the actual drag and drop functionality is provided.
    • onSwipe ->We have to add the code for swiping but we are leaving it empty because we have already worked on the onMove method for swapping.
    • onSelectedChanged -> When the particular selected item of recycler view is released the clearView method is invoked but for now when the item of recycler view is selected we are changing the background color of the item view.
    • clearView -> When the particular selected item of recycler view is released the clearView method is invoked and but for now we are changing the background color of the item view.
    • isLongPressDragEnabled ->  This is returning true to enable the long press on the recycler view item for the drag and drop.

 

If you want to understand more about ItemTouchHelper then check this link: https://developer.android.com/reference/android/support/v7/widget/helper/ItemTouchHelper.html

I hope this blog was helpful to you.

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

css.php

Big thanks to Webkul and his team for helping get Opencart 3.0.3.7 release ready!

Deniel Kerr

Owner and Founder. Opencart

Talk to Sales
Start a Project

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home