0

General task: I want to implement “drag & drop” and “swipe-to-dismiss” functionalyty to FirebaseRecyclerAdapter. It should work something like this: Drag and swipe .gif expample. First I’ll created an interface that allows to pass event callbacks:

public interface ItemTouchHelperAdapter {
boolean onItemMove(int fromPosition, int toPosition);
void onItemDismiss(int position);  }

Than create ItemTouchHelperCallback, the interface that allows to listen for “move” and “swipe” events:

public class ItemTouchHelperCallback extends ItemTouchHelper.Callback  {
private final ItemTouchHelperAdapter mAdapter;
public ItemTouchHelperCallback(ItemTouchHelperAdapter adapter) {
    mAdapter = adapter;
}

@Override
public boolean isLongPressDragEnabled() {
    return true;
}

@Override
public boolean isItemViewSwipeEnabled() {
    return true;
}
@Override
public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
    int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
    int swipeFlags = ItemTouchHelper.START | ItemTouchHelper.END;
    return makeMovementFlags(dragFlags, swipeFlags);
}

@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
    mAdapter.onItemMove(viewHolder.getAdapterPosition(),target.getAdapterPosition());
    return true;
}

@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
    mAdapter.onItemDismiss(viewHolder.getAdapterPosition());
}}

And finally create FirebaseRecyclerAdapter which implements created earlier ItemTouchHelperAdapter:

public class PurchaseListAdapter extends FirebaseRecyclerAdapter<Purchase, PurchaseListHolder> implements ItemTouchHelperAdapter {
public PurchaseListAdapter(Class<Purchase> modelClass, int modelLayout, Class<PurchaseListHolder> viewHolderClass, Query ref) {
    super(modelClass, modelLayout, viewHolderClass, ref);
}

@Override
protected void populateViewHolder(PurchaseListHolder viewHolder, final Purchase model, final int position) {
    viewHolder.mName.setText(model.getName());
}

@Override
protected void onChildChanged(ChangeEventListener.EventType type, int index, int oldIndex) {
    super.onChildChanged(type, index, oldIndex);
}

@Override
public boolean onItemMove(int fromPosition, int toPosition) {
   // TODO: Add functionality for swapping items in FirebaseDatabase
    return true;
}

@Override
public void onItemDismiss(int position) {
    getRef(position).removeValue();
}}

Functionality for "swipe-to-dismiss" (onItemDismiss) works pretty well. But I can not figure out how to implement onItemMove function. Ideally it should swipe items in Firebase database, but I did not find a way to do it.

Maybe someone know any other way implement for RecyclerView "swipe-to-dismiss" and "drug-and-drop" functionality which will work with Firebase?

ALZ313
  • 1
  • 1
  • To change the order in the view/adapter, change the order of the items in the database. It's hard to say more with the code you shared. For a better answer, edit your question to include the [minimum, complete code that reproduces the problem](http://stackoverflow.com/help/mcve) (read the link, it's quite useful). – Frank van Puffelen Apr 12 '17 at 05:53
  • Thanks for comment @FrankvanPuffelen, I made question more specific. Unfortunately I cannot find anywhere how changing the order of the items in the Firebase. If you know, I will be grateful if you point me. – ALZ313 Apr 12 '17 at 12:08
  • Instead of creating an adapter with a `DatabaseReference`, create an adapter with a `Query`. Then you can create a query that has the correct order (and if needed: filter). E.g. `new PurchaseListAdapter(Purchase.class, layout, PurchaseViewHolder.class, ref.orderByChild("propertyThatYouOrderOn"))`. Also see https://firebase.google.com/docs/database/android/lists-of-data#sorting_and_filtering_data – Frank van Puffelen Apr 12 '17 at 12:12

0 Answers0