Skip to content

An Android Library that provide Long press drag and swipe functionality for recycler view items

License

Notifications You must be signed in to change notification settings

Rishabhk07/Recycler-swipe-drag

Repository files navigation

Recycler-swipe-drag

Download Build Status Android Arsenal codebeat badge Codacy Badge

This library provides long press drag and swipe for recycler view items, based on the awesome explanation by Paul Burk & implemented in kotlin, video demo can be seen here

How to use

Include the library

Add the following lines in your app level dependencies

dependencies {
    compile 'me.rishabhkhanna:RecyclerviewSwipeDrag:1.3.1'
}

NOTE: If you get errors regarding conflicting versions of Android Support Library (Manifest merger error), exclude the transitive dependency when including my library

dependencies {
    compile ('me.rishabhkhanna:RecyclerviewSwipeDrag:1.3.1') {
        exclude group: 'com.android.support', module: 'recyclerview-v7'
    }
}

Usage

To use Recyclerview Swipe and Drag in your project, add the following lines and attach ItemTouchHelper with your Recyclerview object

RecyclerHelper touchHelper = new RecyclerHelper<T>(dataArrayList, (RecyclerView.Adapter) recyclerAdapter);
touchHelper.setRecyclerItemDragEnabled(true);
touchHelper.setRecyclerItemSwipeEnabled(true);
ItemTouchHelper itemTouchHelper = new ItemTouchHelper(touchHelper);
itemTouchHelper.attachToRecyclerView(recyclerView);
  • here T is the generic, pass your arrayList model class here like String, Integer or Students
  • you have to pass the data array list and your recyclerview adapter to RecyclerHelper constructor
  • you have to cast your Recyclerview custom adapter in Recyclerview.Adapter, while passing it in RecyclerHelper
  • you can select if you want to enable your recycler view to have dragging items or swiping items or both , you can do this by passing boolean, in touchHelper.setRecyclerItemDragEnabled(true); or touchHelper.setRecyclerItemSwipeEnabled(true);, default is false for both.
  • if you want to do some tasks like storing the list in db or sending a network request after swiping or dragging the recyclerview item then you can this with these 2 callbacks
//  callback for recyclerview item dragged from one position to other
   touchHelper.setOnDragItemListener(new OnDragListener() {
       @Override
       public void onDragItemListener(int fromPosition, int toPosition) {
           Log.d(TAG, "onDragItemListener: callback after dragging recycler view item");
       }
   });

// callback for recyclerview item swiped left or right        
   touchHelper.setOnSwipeItemListener(new OnSwipeListener() {
       @Override
       public void onSwipeItemListener() {
           Log.d(TAG, "onSwipeItemListener: callback after swiping recycler view item");
       }
   });
  • you can also use this library in fluid api like this
RecyclerHelper touchHelper = new RecyclerHelper<String>(dataArrayList, (RecyclerView.Adapter) recyclerAdapter);
touchHelper
       .setRecyclerItemDragEnabled(true)
       .setRecyclerItemSwipeEnabled(true)
       .setOnDragItemListener(new OnDragListener() {
           @Override
           public void onDragItemListener(int fromPosition, int toPosition) {
               Log.d(TAG, "onDragItemListener: callback after dragging recycler view item");
           }
       })
       .setOnSwipeItemListener(new OnSwipeListener() {
           @Override
           public void onSwipeItemListener() {
               Log.d(TAG, "onSwipeItemListener: callback after swiping recycler view item");
           }
       });

Example

  • example usage of library with model class for arraylist as Students
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
ArrayList<Students> dataArrayList = new ArrayList();
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(recyclerAdapter);

//      Library addition from here
RecyclerHelper touchHelper = new RecyclerHelper<Students>(dataArrayList, (RecyclerView.Adapter) recyclerAdapter);
touchHelper.setRecyclerItemDragEnabled(true).setOnDragItemListener(new OnDragListener() {
    @Override
    public void onDragItemListener(int fromPosition, int toPosition) {
        Log.d(TAG, "onDragItemListener: callback after dragging recycler view item");
    }
});
touchHelper.setRecyclerItemSwipeEnabled(true).setOnSwipeItemListener(new OnSwipeListener() {
    @Override
    public void onSwipeItemListener() {
        Log.d(TAG, "onSwipeItemListener: callback after swiping recycler view item");
    }
});
ItemTouchHelper itemTouchHelper = new ItemTouchHelper(touchHelper);
itemTouchHelper.attachToRecyclerView(recyclerView);

License

This software is licensed under the Apache License V2.0 A copy can be found here

About

An Android Library that provide Long press drag and swipe functionality for recycler view items

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published