![License](https://img.shields.io/badge/license-Apache 2.0-brightgreen.svg?style=flat)
A library that provides an easy and customizable way to implement a swipe to dismiss pattern with RecyclerView and works back to API level 7.
Note: This library is currently in active development and might thus not be suitable for production versions as of yet. If you are comfortable experimenting with this library though feel free to give it a spin and report any issues you find. A list of issues currently on the roadmap can be found here.
Here's how the demo application looks and behaves (you can download a debug apk of the demo here):
This library is available as a gradle dependency via JitPack.io. Just add the following lines to your app module build.gradle
:
repositories {
maven { url "https://jitpack.io" }
}
dependencies {
compile 'com.github.TR4Android:Swipeable-RecyclerView:0.2.0'
}
For a full example check out the implementation of the SampleAdapter in the sample folder of this repository.
To be able to use the swipe to dismiss pattern in your RecyclerView you'll have to extend the SwipeAdapter
in your Adapter class. After that there are only a few minor changes you have to do to get everything going:
Override onCreateSwipeViewHolder(ViewGroup parent, int viewType)
and onBindSwipeViewHolder(ViewHolder holder, int position)
instead of the usual onCreateViewHolder(ViewGroup parent, int viewType)
and onBindViewHolder(ViewHolder holder, int position)
. This is needed to wrap your list item in a ViewGroup that handles swiping (namely SwipeItem
) and handle its configuration. In additon to that you'll also have to replace the boolean attachToRoot
with true
so your list item gets attached to the wrapping SwipeItem.
A full implementation might look something like this:
public class SampleAdapter extends SwipeAdapter {
...
@Override
public RecyclerView.ViewHolder onCreateSwipeViewHolder(ViewGroup parent, int i) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_sample, parent, true);
SampleViewHolder sampleViewHolder = new SampleViewHolder(v);
return sampleViewHolder;
}
@Override
public void onBindSwipeViewHolder(RecyclerView.ViewHolder holder, int position) {
// handle data
}
...
}
There also are some new methods related to the swiping pattern in the SwipeAdapter
that you'll have to override. Those are:
onCreateSwipeConfiguration(Context context, int position)
: This is used to determine the configuration of a particular list item and allows flexible control on a per item basis. You'll have to return aSwipeConfiguration
using the built inBuilder
class. More customization options can be found in the SwipeConfiguration section below.onSwipe(int position, int direction)
: This gets called whenever an item is removed using a swipe. Be sure to callnotifyItemRemoved(position)
there after changing your data to properly allow removal using the default ItemAnimator of the RecyclerView.int direction
is one of eitherSWIPE_LEFT
orSWIPE_RIGHT
indicating the direction in which the user has dismissed the item.
An implementation might look like this:
public class SampleAdapter extends SwipeAdapter {
...
public SampleAdapter() {
// retrieve your data
}
...
@Override
public SwipeConfiguration onCreateSwipeConfiguration(Context context, int position) {
return new SwipeConfiguration.Builder(context)
.setBackgroundColorResource(R.color.color_delete)
.setDrawableResource(R.drawable.ic_delete_white_24dp)
.build();
}
@Override
public void onSwipe(int position, int direction) {
mDataset.remove(position);
notifyItemRemoved(position);
}
...
}
There's practically no setup needed for the layout itself, but you'll probably want to add a background to the root of your item. If you just want to use the window background add android:background="?android:attr/windowBackground"
.
You can easily customize the actions when swiping by using the SwipeConfiguration
class which gives you full control over various aspects of this library. The following is a list of all currently available options. For all those there is also a corresponding setLeft...()
and setRight...()
flavor.
setBackgroundColor(int color)
: The background color that appears behind the list item.setBackgroundColorResource(int resId)
: The resource id of the background color that appears behind the list item.setDrawableResource(int resId)
: The resource id of the drawable shown as a hint for the action.setDescriptionTextColor(int color)
: The text color used for the description and undo text.setDescriptionTextColorResource(int resId)
: The resource id of the text color used for the description and undo text.setDescription(CharSequence description)
: The text shown as a hint for the action.setDescriptionResource(int resId)
: The resource id of the text shown as a hint for the action.setUndoDescription(CharSequence description)
: The text shown when the user has dismissed the item and is shown the option to undo the dismissal.setUndoDescriptionResource(int resId)
: The resource id of the text shown when the user has dismissed the item and is shown the option to undo the dismissal.setUndoable(boolean undoable)
: Whether the action is undoable. If set totrue
the user will have the option to undo the action for 5 seconds, if set tofalse
the item will be dismissed immediately.setSwipeBehaviour(SwipeBehavior swipeBehavior)
: The behaviour of the item when swiping. Takes one of the provided default behavioursNORMAL_SWIPE
,RESTRICTED_SWIPE
orNO_SWIPE
.setSwipeBehaviour(float range, Interpolator interpolator)
: The more customized behaviour of the item when swiping, whererange
indicates how far the item can be swiped (percentage of item width) andinterpolator
is the custom Interpolator used when calculating the item position while swiping.setCallbackEnabled(boolean enabled)
: Whether the swipe callback should be triggered on this action. If set totrue
you will receive a swipe action throughonSwipe(int position, int direction)
, if set tofalse
you won't.
Copyright 2015 Thomas Robert Altstidl
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Copyright 2015 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.