Contributions / PRs are welcome.
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
- 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)
- 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'
}
- 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>
In your Activity add
import swipe.gestures.GestureManager;
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
}
};
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);
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.
Blue | Yellow |
---|---|
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 |
---|---|
Small | Small |
Big | Big |
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 |
Text and icon color | Only Text |
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