Skip to content

WilliBoelke/simple-recycler-view-swipe-gestures

Repository files navigation

Android-RecyclerView-Swipe-Gestures

Alt

Codacy Badge GitHub last commit Issues
PR's Welcome

Contribution

Contributions / PRs are welcome.

1. Introduction

An easy to use and highly customizable implementation of swipe gestures for an android RecyclerView.

  • Support for left and right swipes for any RecyclerView
  • Set colours as background for each swipe direction
  • Set icons for each swipe direction
  • Set texts in addition to icons

2. Planned

  • The actions will be executed only when clicking on the coloured button which will be schown when swiped - not directly after swiping.
  • More then one action for each swipe direction (several buttons will be displayed)

3. Setup

3.1. Add JitPack to your Project

Gradle

  • Add it in your root build.gradle at the end of repositories:

Note check the JitPack link for newer version, the following ones may not be up to date

	allprojects {
		repositories {
			...
			maven { url 'https://jitpack.io' }
		}
	}
  • Add the dependencie
dependencies {
	implementation 'com.github.WilliBoelke:simple-recycler-view-swipe-gestures:1.3'
}

Maven

  • Add the JitPack Repository to your pom.xml
<repositories>
	<repository>
		<id>jitpack.io</id>
		<url>https://jitpack.io</url>
	</repository>
</repositories>
  • Add the dependency to your pom.xml
<dependency>
    <groupId>com.github.WilliBoelke</groupId>
    <artifactId>simple-recycler-view-swipe-gestures</artifactId>
    <version>1.3</version>
</dependency>

Or find other versions here

JitPack-SompleSwipeGestures

3.2 Import GestureManager

In your Activity add

import swipe.gestures.GestureManager;

4. Usage

4.1 Implement the SwipeCallbackLeft and/or SwipeCallbackLeft

In your activity implement the interfaces.
Here you put the code which will be executed when the recycler item was swiped.

  private GestureManager.SwipeCallbackLeft leftCallback leftCallback = new SwipeCallbackLeft()
    {
        @Override
        public void onLeftSwipe(int position)
        {
            // your code here 
        }
    }; 

4.2 Initialize 'GestureManager'in your activity

 GestureManager recyclerAdapterSwipeGestures = new GestureManager(rightCallback, leftCallback);

If you just need one swipe gesture the just implement one of the interfaces and pass it:

GestureManager recyclerAdapterSwipeGestures = new GestureManager(rightCallback);

4.3 Set a colour

Use the setter to set a colour:

recyclerAdapterSwipeGestures.setBackgroundColorLeft(new ColorDrawable(Color.RED));

You can set a different colour for the two directions. The standard colours are RED and GREEN.

Colours

Blue Yellow
BLUE YELLOW

4.4 Set Icons

Optionally you can use icons for the swipe acions which will be displayed when the swipe is performed.

recyclerAdapterSwipeGestures.setIconRight(ContextCompat.getDrawable(this, R.drawable.your_icon));

That again works for both actions. You also can change the size of the icons by using

recyclerAdapterSwipeGestures.setIconSizeMultiplier(2);
Icon Icon
RIGHT LEFT
Small Small
SMALLLEFT SMAllRIGHT
Big Big
BIGLEFT BIGRIGHT

4.5 Text

You can set a text (insead of / with an icon), the text can be customized by using the setters.

recyclerAdapterSwipeGestures.setTextLeft("LEFT");
recyclerAdapterSwipeGestures.setTextRight("RIGHT");

Customize the text :

//Set text size
recyclerAdapterSwipeGestures.setTextSize(60);

//Set text colour
recyclerAdapterSwipeGestures.setTextColor(Color.BLACK);

Texts can also be customized seperatly by using the setters as follows:

//Set text size
recyclerAdapterSwipeGestures.setTextSize(60, 100

//Set text colour
recyclerAdapterSwipeGestures.setTextColor(Color.BLACK, Color.YELLOW);
Text Text
Right Text Small
RIGHTTEXT TEXTWITHICON
Text and icon color Only Text
COLOURTEXT ONLYTEXT

4.6 Attach to the RecylerViewAdapter

You need to attach the swipe gestures to the RecyclerView Adapter using a ItemTouchHelper

ItemTouchHelper itemTouchHelper = new ItemTouchHelper(recyclerAdapterSwipeGestures);
itemTouchHelper.attachToRecyclerView(recyclerView);

And thats it for now. You can find an example implementation in the MainActivity