Drag and drop items in RecyclerView with GridLayoutManager
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();
}
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.