Android-RecyclerView-Swipe-Gestures
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
GestureManager
3.2 Import In your Activity add
import swipe.gestures.GestureManager;
4. Usage
SwipeCallbackLeft
and/or SwipeCallbackLeft
4.1 Implement the 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);
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 |
---|---|
4.4 Set Icons
Optionally you can use icons for the swipe acions which will be displayed when the swpie 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 |
4.5 Text
You can set a text (insead or with and 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 |
RecylerViewAdapter
4.6 Attach to the You need to attach the swipe gestures to the RecyyclerView 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