• Stars
    star
    180
  • Rank 208,827 (Top 5 %)
  • Language
    Java
  • License
    Apache License 2.0
  • Created over 9 years ago
  • Updated about 7 years ago

Reviews

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

Repository Details

Implementation of an Android CardView list in a RecyclerView that allows dismissing/deleting elements by swiping them to the left or right.

Android SwipeableRecyclerView

Based on romannurik`s Android-SwipeToDismiss.

Sample project implementation of a single column list of CardViews in a RecyclerView with a TouchListener that allows dismissing of elements by swiping the elements to the left or right.

Output sample

How to use

  • Add these Gradle dependencies to your app's module:

    dependencies {
        ...
        
        // already includes 'com.android.support:recyclerview-v7:23.1.1'
        compile 'com.github.brnunes:swipeablerecyclerview:1.0.2'
    
        // only necessary if you are using CardView
        compile 'com.android.support:cardview-v7:23.1.1'
    }
    

The RecyclerView and CardView widgets are part of the v7 Support Libraries.

  • Instantiate a SwipeableRecyclerViewTouchListener passing as parameters the RecyclerView and a SwipeableRecyclerViewTouchListener.SwipeListener that will receive the callbacks.

  • Add the instantiated SwipeableRecyclerViewTouchListener as a RecyclerView.OnItemTouchListener.

    SwipeableRecyclerViewTouchListener swipeTouchListener =
            new SwipeableRecyclerViewTouchListener(mRecyclerView,
                    new SwipeableRecyclerViewTouchListener.SwipeListener() {
                        @Override
                        public boolean canSwipeLeft(int position) {
                            return true;
                        }
    
                        @Override
                        public boolean canSwipeRight(int position) {
                            return true;
                        }
    
                        @Override
                        public void onDismissedBySwipeLeft(RecyclerView recyclerView, int[] reverseSortedPositions) {
                            for (int position : reverseSortedPositions) {
                                mItems.remove(position);
                                mAdapter.notifyItemRemoved(position);
                            }
                            mAdapter.notifyDataSetChanged();
                        }
    
                        @Override
                        public void onDismissedBySwipeRight(RecyclerView recyclerView, int[] reverseSortedPositions) {
                            for (int position : reverseSortedPositions) {
                                mItems.remove(position);
                                mAdapter.notifyItemRemoved(position);
                            }
                            mAdapter.notifyDataSetChanged();
                        }
                    });
    
    mRecyclerView.addOnItemTouchListener(swipeTouchListener);