Android Studio - Drag and Drop Items RecyclerView (2018)

Описание к видео Android Studio - Drag and Drop Items RecyclerView (2018)

In this video you will learn how to drag and drop items in your RecyclerView. We are going to use class called ItemTouchHelper for implementing dragging 'n' dropping in our program.

First step is to create object of ItemTouchHelper and to pass in constructor object of ItemTouchHelper.SimpleCallBack.

In ItemTouchHelper.SimpleCallBack constructor we need to pass flags for swiping, that is where are we allow to swipe. SimpleCallBack has 2 parameters, first one is for dragging and dropping and second is for swiping. For this video we set first parameter as 0(since we don't use dragging 'n' dropping) and LEFT or RIGHT as second parameter.

We need to override 2 methods inside SimpleCallBack class: onMove() and onSwiped(), onSwiped() is for swiping and we don't need that, we will use onMove() because onMove() is for draggin and dropping.

Inside onMove() we get positions of moved item and position of item that we moved our initial moved item to. Next step is to swap those two items in our list and notify adapter about that.

Last step is attaching our RecyclerView to our ItemTouchHelper object.

Комментарии

Информация по комментариям в разработке