Code Monkey home page Code Monkey logo

swipeable-recyclerview's Introduction

Release API ![License](https://img.shields.io/badge/license-Apache 2.0-brightgreen.svg?style=flat)

Swipeable-RecyclerView

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):

Demo GIF

Usage

Importing the library

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'
}

Code Setup

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:

Migrating from normal adapter

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
    }
    ...
}
Special setup for swipeable adapter

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 a SwipeConfiguration using the built in Builder 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 call notifyItemRemoved(position) there after changing your data to properly allow removal using the default ItemAnimator of the RecyclerView. int direction is one of either SWIPE_LEFT or SWIPE_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);
    }
    ...
}
Special setup for item layout

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".

Customization

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 to true the user will have the option to undo the action for 5 seconds, if set to false the item will be dismissed immediately.
  • setSwipeBehaviour(SwipeBehavior swipeBehavior): The behaviour of the item when swiping. Takes one of the provided default behaviours NORMAL_SWIPE, RESTRICTED_SWIPE or NO_SWIPE.
  • setSwipeBehaviour(float range, Interpolator interpolator): The more customized behaviour of the item when swiping, where range indicates how far the item can be swiped (percentage of item width) and interpolator 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 to true you will receive a swipe action through onSwipe(int position, int direction), if set to false you won't.

License

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.

License of RecyclerView and AppCompat v4

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.

swipeable-recyclerview's People

Contributors

taltstidl avatar fountaingeyser avatar jeansarlon avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.