• This repository has been archived on 09/May/2021
  • Stars
    star
    526
  • Rank 82,454 (Top 2 %)
  • Language
    Java
  • License
    Other
  • Created over 9 years ago
  • Updated over 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Drag and drop re-ordering adapter for RecyclerView

DragSortAdapter

Maven Central Android Arsenal

Drag and Drop adapter implementation for RecyclerView. Targeted to support any LayoutManager and ItemAnimator.

Note: This is an advanced library meant to be flexible and customizable which leads to more complexity in integration. It is not meant to be a simple drop-in. If you need even more customization I suggest exploring the source and copying relevant code that you need.

Usage

Add Snapshot repository and add to dependencies:

dependencies {
  compile 'com.makeramen:dragsortadapter:X.X.X'
}

Override DragSortAdapter<T extends DragSortAdapter.ViewHolder>, see ExampleAdapter.java: required functions:

// this function should be reasonable performant as it gets called a lot on the UI thread
public abstract int getPositionForId(long id);
  
// this needs to re-order the positions **live** during dragging
public abstract boolean move(int fromPosition, int toPosition);

// not required but you probably want to override this to save the re-ordering after drop event
public void onDrop() { }

Set adapter in code:

recyclerView.setAdapter(new ExampleAdapter(recyclerView));

Call startDrag() when you want to start dragging (e.g. onLongClick):

    @Override public boolean onLongClick(@NonNull View v) {
      startDrag();
      return true;
    }

Works with any LayoutManager

recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setLayoutManager(new GridLayoutManager(this, 3));
recyclerView.setLayoutManager(new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL));

And any ItemAnimator:

recyclerView.setItemAnimator(new DefaultItemAnimator());