Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/wdullaer/swipeactionadapter
The Mailbox-like Swipe gesture library for Android
https://github.com/wdullaer/swipeactionadapter
android android-library jar listview swipe-gestures
Last synced: about 6 hours ago
JSON representation
The Mailbox-like Swipe gesture library for Android
- Host: GitHub
- URL: https://github.com/wdullaer/swipeactionadapter
- Owner: wdullaer
- License: apache-2.0
- Created: 2014-07-06T11:48:22.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2019-11-10T08:31:15.000Z (about 5 years ago)
- Last Synced: 2025-01-31T14:16:08.955Z (7 days ago)
- Topics: android, android-library, jar, listview, swipe-gestures
- Language: Java
- Homepage:
- Size: 205 KB
- Stars: 213
- Watchers: 12
- Forks: 61
- Open Issues: 15
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
SwipeActionAdapter - The Mailbox-like Swipe gesture library
===========================================================![Maven Central](https://img.shields.io/maven-central/v/com.wdullaer/swipeactionadapter.svg)
SwipeActionAdapter is a library that provides a simple API to create Mailbox-like action when swiping in a ListView.
The idea is to make it simple enough to implement while still providing sufficient options to allow you to
integrate it with the design of your application.Support for Android 4.0 and up. It might work on lower API versions, but I haven't tested it and I don't intend to
make any effort in that direction.Feel free to fork or issue pull requests on github. Issues can be reported on the github issue tracker.
![Normal Swipe Gesture](https://raw.github.com/wdullaer/SwipeActionAdapter/gh-pages/images/swipe_1.png)
![Far Swipe Gesture](https://raw.github.com/wdullaer/SwipeActionAdapter/gh-pages/images/swipe_2.png)
Setup
=====
There are multiple options to add SwipeActionAdapter to your project:* **Grade**
Add it as a dependency to your `build.gradle`
```java
dependencies {
compile 'com.wdullaer:swipeactionadapter:2.0.0'
}
```* **Jar File**
[Download the jar file](https://github.com/wdullaer/SwipeActionAdapter/releases/latest) and add it to your project* **Build from source**
If you would like to get the most recent code in a jar, clone the project and run ```./gradlew jar``` from
the root folder. This will build a `swipeactionadapter.jar` in ```library/build/libs/```.You may also add the library as an Android Library to your project. All the library files live in ```library```.
The library also uses some Java 8 features, which Android Studio will need to transpile. This requires the following stanza in your app's `build.gradle`.
See https://developer.android.com/studio/write/java8-support.html for more information on Java 8 support in Android.
```groovy
android {
...
// Configure only for each module that uses Java 8
// language features (either in its source code or
// through dependencies).
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
```Creating your ListView with swipe actions
-----------------------------------------If you'd rather just start with a working example, clone the project and take a look.
For a basic implementation, you'll need to
1. Wrap the Adapter of your ListView with a SwipeActionAdapter
2. Create a background layout for each swipe direction you wish to act upon.
3. Implement the SwipeActionAdapter### Wrap the adapter of your ListView
The majority of this libraries functionality is provided through an adapter which wraps around the content ```Adapter```
of your ```ListView```. You will need to set the SwipeActionAdapter to your ListView and because
we need to set some properties of the ListView, you will also need to give a reference of the ```ListView```
to the ```SwipeActionAdapter```.
This is typically done in your ```Activity```'s or ```Fragments``` onCreate/onActivityCreated callbacks.```java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);// Create an Adapter for your content
String[] content = new String[20];
for (int i=0;i<20;i++) content[i] = "Row "+(i+1);
ArrayAdapter stringAdapter = new ArrayAdapter(
this,
R.layout.row_bg,
R.id.text,
new ArrayList(Arrays.asList(content))
);// Wrap your content in a SwipeActionAdapter
mAdapter = new SwipeActionAdapter(stringAdapter);// Pass a reference of your ListView to the SwipeActionAdapter
mAdapter.setListView(getListView());// Set the SwipeActionAdapter as the Adapter for your ListView
setListAdapter(mAdapter);
}
```### Create a background layout for each swipe direction
I'm just supplying an empty layout with a background for each direction.
You should have a layout for at least ```SwipeDirection.DIRECTION_NORMAL_LEFT``` and ```SwipeDirection.DIRECTION_NORMAL_RIGHT```.
The other directions are optional.
It is important that the background layouts scale properly vertically.
The ```onCreate``` callback from the previous section now becomes:```java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);// Create an Adapter for your content
String[] content = new String[20];
for (int i=0;i<20;i++) content[i] = "Row "+(i+1);
ArrayAdapter stringAdapter = new ArrayAdapter(
this,
R.layout.row_bg,
R.id.text,
new ArrayList(Arrays.asList(content))
);// Wrap your content in a SwipeActionAdapter
mAdapter = new SwipeActionAdapter(stringAdapter);// Pass a reference of your ListView to the SwipeActionAdapter
mAdapter.setListView(getListView());// Set the SwipeActionAdapter as the Adapter for your ListView
setListAdapter(mAdapter);// Set backgrounds for the swipe directions
mAdapter.addBackground(SwipeDirection.DIRECTION_FAR_LEFT,R.layout.row_bg_left_far)
.addBackground(SwipeDirection.DIRECTION_NORMAL_LEFT,R.layout.row_bg_left)
.addBackground(SwipeDirection.DIRECTION_FAR_RIGHT,R.layout.row_bg_right_far)
.addBackground(SwipeDirection.DIRECTION_NORMAL_RIGHT,R.layout.row_bg_right);
}
```Layout code
```xml
```
### Implement the SwipeActionListener
The final thing to do is listen to swipe gestures. This is done by implementing the ```SwipeActionListener```.
This interface has three methods:
* ```boolean hasActions(int position, SwipeDirection direction)```: return true if you want this item to be swipeable in this direction
* ```boolean shouldDismiss(int position, SwipeDirection direction)```: return true if you want the item to be dismissed,
return false if it should stay visible. This method runs on the interface thread so if you want to trigger any
heavy actions here, put them on an ```ASyncThread```
* ```void onSwipe(int[] position, SwipeDirection[] direction)```: triggered when all animations on the swiped items have finished.
You will receive an array of all swiped items, sorted in descending order with their corresponding directions.You should pass a reference of your ```SwipeActionListener``` to the ```SwipeActionAdapter```
Here's the final implementation of our example's ```onCreate``` method:
```java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);// Create an Adapter for your content
String[] content = new String[20];
for (int i=0;i<20;i++) content[i] = "Row "+(i+1);
ArrayAdapter stringAdapter = new ArrayAdapter(
this,
R.layout.row_bg,
R.id.text,
new ArrayList(Arrays.asList(content))
);// Wrap your content in a SwipeActionAdapter
mAdapter = new SwipeActionAdapter(stringAdapter);// Pass a reference of your ListView to the SwipeActionAdapter
mAdapter.setListView(getListView());// Set the SwipeActionAdapter as the Adapter for your ListView
setListAdapter(mAdapter);// Set backgrounds for the swipe directions
mAdapter.addBackground(SwipeDirection.DIRECTION_FAR_LEFT,R.layout.row_bg_left_far)
.addBackground(SwipeDirection.DIRECTION_NORMAL_LEFT,R.layout.row_bg_left)
.addBackground(SwipeDirection.DIRECTION_FAR_RIGHT,R.layout.row_bg_right_far)
.addBackground(SwipeDirection.DIRECTION_NORMAL_RIGHT,R.layout.row_bg_right);// Listen to swipes
mAdapter.setSwipeActionListener(new SwipeActionListener(){
@Override
public boolean hasActions(int position, SwipeDirection direction){
if(direction.isLeft()) return true; // Change this to false to disable left swipes
if(direction.isRight()) return true;
return false;
}@Override
public boolean shouldDismiss(int position, SwipeDirection direction){
// Only dismiss an item when swiping normal left
return direction == SwipeDirection.DIRECTION_NORMAL_LEFT;
}@Override
public void onSwipe(int[] positionList, SwipeDirection[] directionList){
for(int i=0;i