50

What I want to achieve: Have a RecyclerView with GridLayoutManager that supports drag'n'drop and that rearranges the items while dragging.

Side note: First time developing anything with drag and drop.

There are a lot of topics on how to achieve this feature using a ListView, for example: https://raw.githubusercontent.com/btownrippleman/FurthestProgress/master/FurthestProgress/src/com/anappforthat/android/languagelineup/DynamicListView.java

However the examples are usually a lot of code with, creating bitmaps of the dragged view and it feels like it should be possible to achieve the same result using View.startDrag(...) and RecyclerView with notifyItemAdded(), notifyItemMoved() and notifyItemRemoved() since they provide rearrange animations.

So I played around some and came up with this:

final CardAdapter adapter = new CardAdapter(list);
adapter.setHasStableIds(true);
adapter.setListener(new CardAdapter.OnLongClickListener() {
    @Override
    public void onLongClick(View view) {
        ClipData data = ClipData.newPlainText("","");
        View.DragShadowBuilder builder = new View.DragShadowBuilder(view);
        final int pos = mRecyclerView.getChildAdapterPosition(view);
        final Goal item = list.remove(pos);

        mRecyclerView.setOnDragListener(new View.OnDragListener() {
            int prevPos = pos;

            @Override
            public boolean onDrag(View view, DragEvent dragEvent) {
                final int action = dragEvent.getAction();
                switch(action) {
                    case DragEvent.ACTION_DRAG_LOCATION:
                        View onTopOf = mRecyclerView.findChildViewUnder(dragEvent.getX(), dragEvent.getY());
                        int i = mRecyclerView.getChildAdapterPosition(onTopOf);

                        list.add(i, list.remove(prevPos));
                        adapter.notifyItemMoved(prevPos, i);
                        prevPos = i;
                        break;

                    case DragEvent.ACTION_DROP:
                        View underView = mRecyclerView.findChildViewUnder(dragEvent.getX(), dragEvent.getY());
                        int underPos = mRecyclerView.getChildAdapterPosition(underView);

                        list.add(underPos, item);
                        adapter.notifyItemInserted(underPos);
                        adapter.notifyDataSetChanged();
                        break;
                }

                return true;
            }
        });

        view.startDrag(data, builder, view, 0);
    }
});
mRecyclerView.setAdapter(adapter);

This piece of code sort of work, I get the swapping, but very unstable/shaky and sometimes when it's refreshing the whole grid is rearranged back to original order or to something random. Anyway the code above is just my first quick attempt, what I'm really more interested in knowing is if there's some standard/best practice way of doing the drag and drop with ReyclerView's or if the correct way of solving it is still the same that's been used for ListViews for years?

patrick.elmquist
  • 2,113
  • 2
  • 21
  • 35

4 Answers4

124

There is actually a better way to achieve this. You can use some of the RecyclerView's "companion" classes:

ItemTouchHelper, which is

a utility class to add swipe to dismiss and drag & drop support to RecyclerView.

and its ItemTouchHelper.Callback, which is

the contract between ItemTouchHelper and your application

// Create an `ItemTouchHelper` and attach it to the `RecyclerView`
ItemTouchHelper ith = new ItemTouchHelper(_ithCallback);
ith.attachToRecyclerView(rv);

// Extend the Callback class
ItemTouchHelper.Callback _ithCallback = new ItemTouchHelper.Callback() {
    //and in your imlpementaion of
    public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
        // get the viewHolder's and target's positions in your adapter data, swap them
        Collections.swap(/*RecyclerView.Adapter's data collection*/, viewHolder.getAdapterPosition(), target.getAdapterPosition());
        // and notify the adapter that its dataset has changed
        _adapter.notifyItemMoved(viewHolder.getAdapterPosition(), target.getAdapterPosition());
        return true;
    }

    @Override
    public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
        //TODO    
    }

    //defines the enabled move directions in each state (idle, swiping, dragging). 
    @Override
    public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        return makeFlag(ItemTouchHelper.ACTION_STATE_DRAG,
                ItemTouchHelper.DOWN | ItemTouchHelper.UP | ItemTouchHelper.START | ItemTouchHelper.END);
    }
};

For more details check their documentation.

Gowtham
  • 11,853
  • 12
  • 43
  • 64
stan0
  • 11,549
  • 6
  • 42
  • 59
  • Interesting, will explore this solution on Monday but it seems promising. – patrick.elmquist Jun 05 '15 at 14:40
  • @PaulBurke: I updated the code but the original idea was to give an overview of the method and leave the details to the documentation – stan0 Jul 20 '15 at 13:05
  • @stan0 Up-voted. Hopefully OP will accept the answer now (that it's complete). – Paul Burke Jul 20 '15 at 13:06
  • In addition: you can disable drag for specific item based on the viewHolder in `getMovementFlag` - simply call `makeFlag` wih '0' instead of the direction flags. – stan0 Jul 23 '15 at 13:19
  • Marking this as the answer, didn't use the included code but the post lead me to a solution that works. Thanks! – patrick.elmquist Sep 03 '15 at 18:55
  • Can I get Drop event with this ? I required to validate position on Drop event – Vishal Khakhkhar Sep 23 '15 at 09:11
  • 1
    which solution @VishalKhakhkhar ? I added mandatory onSwiped() method – Shine Oct 06 '15 at 17:12
  • 19
    For right order change: instead just Collection.swap() you should do: if (fromPosition < toPosition) { for (int i = fromPosition; i < toPosition; i++) { Collections.swap(gridItems, i, i + 1); } } else { for (int i = fromPosition; i > toPosition; i--) { Collections.swap(gridItems, i, i - 1); } } – Denis Nek Jan 21 '16 at 11:06
  • @PaulBurke having a problem with scroll, when user reaches the end and is also dragging, scroll and drag gets mixed up. Any suggestions – abhishesh Apr 26 '16 at 04:03
  • so f ing easy) no headache – user25 Mar 18 '17 at 21:39
  • 3
    Is there a way to initiate drag/move on short press instead of long press? – Murcielago Dec 11 '17 at 22:53
  • Is there any way to halt drag and drop for specific position? – Rahul Khurana Jan 16 '19 at 10:12
  • 8
    @DenisNek What you wrote isn't swapping between 2 items. Instead it's moving an item from one place to another. Also, there is no need for a loop. You could just call `val item = items.removeAt(fromPosition) items.add(toPosition, item) recyclerView.adapter!!.notifyItemMoved(fromPosition, toPosition)` – android developer Jul 31 '19 at 12:41
  • @RahulKhurana I don't think there is a way, at least not official one. Asked about this here: https://stackoverflow.com/q/57286355/878126 – android developer Jul 31 '19 at 12:43
  • @abhishesh Try my sample here: https://github.com/AndroidDeveloperLB/RecyclerViewDragAndDropTest – android developer Jul 31 '19 at 12:44
  • @androiddeveloper Thanks I have resolved it here https://stackoverflow.com/a/54216384/4079010 using `getMovementFlags` method – Rahul Khurana Aug 01 '19 at 03:23
  • This worker for me `Collections.swap(rvAdapter.items, startPos, target) rvAdapter.notifyItemMoved(startPos, target)` – Vishak A Kamath Feb 04 '20 at 12:57
  • @androiddeveloper but if I set my RecyclerView Adapter with `setHasStableIds(true)`, your solution won't work then right? Since the unique ids of the items are related to the item position, your solution does not modify the id of the item? – marticztn Feb 22 '22 at 21:58
  • @marticztn You actually have to set it as you wrote. Otherwise it won't work well. The changes to the list aren't enough, because the adapter needs to know about the changes. Each time you change the list in any way that the adapter needs to know, you need to tell it. In this case, it's `notifyItemMoved` . – android developer Feb 23 '22 at 08:26
  • @androiddeveloper Thanks for your reply! I added the code that modifies the unique item ID inside my `ItemTouchHelper` callback, I also overrode `getItemId()` in my adapter class, but now when I drag and drop the item, it only drags to 1 position and it just stopped right there, not sure if it's related to the item id modification inside the callbacks function, I tried 4 hours looking for a solution but I got no luck so far, any idea how to solve this issue? – marticztn Feb 24 '22 at 05:09
  • @marticztn Create a new question on the website with a small code to demonstrate the issue. I'm sure people will help. – android developer Feb 25 '22 at 08:01
20

This is my solution with database reordering:

    ItemTouchHelper.SimpleCallback simpleItemTouchCallback = new ItemTouchHelper.SimpleCallback(ItemTouchHelper.UP | ItemTouchHelper.DOWN, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {
        @Override
        public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
            final int fromPosition = viewHolder.getAdapterPosition();
            final int toPosition = target.getAdapterPosition();
            if (fromPosition < toPosition) {
                for (int i = fromPosition; i < toPosition; i++) {
                    Collections.swap(mAdapter.getCapitolos(), i, i + 1);
                }
            } else {
                for (int i = fromPosition; i > toPosition; i--) {
                    Collections.swap(mAdapter.getCapitolos(), i, i - 1);
                }
            }
            mAdapter.notifyItemMoved(fromPosition, toPosition);
            return true;
        }

        @Override
        public void onSwiped(RecyclerView.ViewHolder viewHolder, int swipeDir) {
            MyViewHolder svH = (MyViewHolder ) viewHolder;
            int index = mAdapter.getCapitolos().indexOf(svH.currentItem);
            mAdapter.getCapitolos().remove(svH.currentItem);
            mAdapter.notifyItemRemoved(index);
            if (emptyView != null) {
                if (mAdapter.getCapitolos().size() > 0) {
                emptyView.setVisibility(TextView.GONE);
                } else {
                emptyView.setVisibility(TextView.VISIBLE);
                }
            }
        }

        @Override
        public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
            super.clearView(recyclerView, viewHolder);
            reorderData();
        }
    };

    ItemTouchHelper itemTouchHelper = new ItemTouchHelper(simpleItemTouchCallback);
    itemTouchHelper.attachToRecyclerView(recList);

There is a support functions tahat make use of AsyncTask:

private void reorderData() {
    AsyncTask<String, Void, Spanned> task = new AsyncTask<String, Void, Spanned>() {
        @Override
        protected Spanned doInBackground(String... strings) {
            dbService.deleteAllData();
            for (int i = mAdapter.getCapitolos().size() - 1; i >= 0; i--) {
                Segnalibro s = mAdapter.getCapitolos().get(i);
                dbService.saveData(s.getIdCapitolo(), s.getVersetto());
            }
            return null;
        }

        @Override
        protected void onPostExecute(Spanned spanned) {
        }
    };
    task.execute();
}
Flavio Barisi
  • 472
  • 6
  • 9
9

Here, I've made a full sample in Kotlin (here), and, if you wish, you can enable swipe-to-dismiss on it . Here's the entire code of it:

build.gradle

implementation 'androidx.appcompat:appcompat:1.1.0-rc01'
implementation 'androidx.core:core-ktx:1.2.0-alpha02'
implementation 'androidx.constraintlayout:constraintlayout:2.0.0-beta2'
implementation 'com.google.android.material:material:1.1.0-alpha08'
implementation 'androidx.recyclerview:recyclerview:1.1.0-beta01'

grid_item.xml

<TextView
    android:id="@+id/textView" xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="100dp" android:gravity="center"/>

activity_main.xml

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recyclerView" tools:listitem="@layout/grid_item"  xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:orientation="vertical" app:spanCount="3" app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"/>

manifest

<manifest package="com.sample.recyclerviewdraganddroptest" xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools">

    <application
        android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true"
        android:theme="@style/AppTheme.NoActionBar" tools:ignore="AllowBackup,GoogleAppIndexingWarning">
        <activity
            android:name=".MainActivity" android:label="@string/app_name" android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>

                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>

</manifest>

MainActivity.kt

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val items = ArrayList<Int>(100)
        for (i in 0 until 100)
            items.add(i)
        recyclerView.adapter = object : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
            override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
                return object : RecyclerView.ViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.grid_item, parent, false)) {}
            }

            override fun getItemCount() = items.size

            override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
                val data = items[position]
                holder.itemView.setBackgroundColor(if (data % 2 == 0) 0xffff0000.toInt() else 0xff00ff00.toInt())
                holder.itemView.textView.text = "item $data"
            }
        }
        val itemTouchHelper = ItemTouchHelper(object : ItemTouchHelper.Callback() {
            override fun isLongPressDragEnabled() = true
            override fun isItemViewSwipeEnabled() = false

            override fun getMovementFlags(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder): Int {
                val dragFlags = ItemTouchHelper.UP or ItemTouchHelper.DOWN or ItemTouchHelper.LEFT or ItemTouchHelper.RIGHT
                val swipeFlags = if (isItemViewSwipeEnabled) ItemTouchHelper.START or ItemTouchHelper.END else 0
                return makeMovementFlags(dragFlags, swipeFlags)
            }

            override fun onMove(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder, target: RecyclerView.ViewHolder): Boolean {
                if (viewHolder.itemViewType != target.itemViewType)
                    return false
                val fromPosition = viewHolder.adapterPosition
                val toPosition = target.adapterPosition
                val item = items.removeAt(fromPosition)
                items.add(toPosition, item)
                recyclerView.adapter!!.notifyItemMoved(fromPosition, toPosition)
                return true
            }

            override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
                val position = viewHolder.adapterPosition
                items.remove(position)
                recyclerView.adapter!!.notifyItemRemoved(position)
            }

        })
        itemTouchHelper.attachToRecyclerView(recyclerView)
    }

}
android developer
  • 114,585
  • 152
  • 739
  • 1,270
  • Hi, Can you tell me if it is possible to add Drag and Drop this implementation to RecyclerView which Uses ListAdapter with DiffUtils? Your help will be really appreciated. – Muhammad Farhan Jul 22 '20 at 13:09
  • 1
    @MuhammadFarhan I never used it. Thanks. Anyway, I think it might be possible. Just be careful about what you do with the operations. :) – android developer Jul 22 '20 at 13:52
  • 1
    Thanks for your Fast Reply really appreciated. It Works Like a charm with just little of changes like need to use `adapter.submitList(newList)`. To be honest your Drag and Drop onMove Code Works only for me Perfectly. Thanks a lot, mate. – Muhammad Farhan Jul 23 '20 at 05:33
  • @MuhammadFarhan Nice. What do you mean by "Works only for me Perfectly" ? It worked for me too :) – android developer Jul 23 '20 at 08:45
  • hahaha It will work for everyone. I didn't found any solution with ListApadater only your's solution works for me to Remove and Add item in List. everyone Swap in List and calls NotifyItemChange which is not working in ListAdapter it show janky animation and set different item on a different position, I have also some Room Operations change Order Like. again Thanks a lot :) – Muhammad Farhan Jul 23 '20 at 08:53
  • @MuhammadFarhan I think that you should have ID for each item correctly for anything advanced to work properly. Maybe by having a new list, you've missed it on the way, creating new IDs ? I think using a new list is ok. I used it a lot (never used ListAdapter though). – android developer Jul 23 '20 at 10:48
  • That code of the ItemTouchHelper is so beautiful, works like a charm! – SlowDeepCoder Apr 01 '21 at 12:18
  • @SlowDeep I wish I had time to think how to add it to my own apps :) – android developer Apr 01 '21 at 21:42
-4

I found Advancedrecyclerview quite useful. Have a look!!!

Ash
  • 1,391
  • 15
  • 20