Home

Awesome

SwipeToAction

Download

An easy way to add a simple 'swipe-and-do-something' behavior to your RecyclerView items. Just like in Gmail or Inbox apps.

SwipeToAction Sample

Integration

The lib is available on Maven Central, you can find it with Gradle, please

dependencies {
    compile 'co.dift.ui.swipetoaction:library:1.1'
}

Usage

Check a demo project in the sample/ folder.

  1. The view you'll use for the items should have at least 3 children:
  1. In the adapter for your RecyclerView create a ViewHolder that extends SwipeToAction.ViewHolder<T>. For instance, in the sample project I have a list of books and my items are instances of a Book class, therefore, the ViewHolder inside the adapter looks like this:

       public class BookViewHolder extends SwipeToAction.ViewHolder<Book> {
         ...
         public BookViewHolder(View v) {
           super(v);
           ...
         }
       }
    

    Also, in the onBindViewHolder method remember to set the item data to the view holder instance

      @Override
      public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        Book item = items.get(position);
        BookViewHolder vh = (BookViewHolder) holder;
        vh.data = item;
    
        ...
    
      }
    
  2. Set the RecyclerView as usual to create a vertical list, set the previous adapter and instantiate SwipeToAction providing the recyclerView instance and a swipe listener SwipeToAction.SwipeListener<T>

      recyclerView = (RecyclerView) findViewById(R.id.recycler);
      LinearLayoutManager layoutManager = new LinearLayoutManager(this);
      recyclerView.setLayoutManager(layoutManager);
      recyclerView.setHasFixedSize(true);
    
      adapter = new BooksAdapter(this.books);
      recyclerView.setAdapter(adapter);
    
      swipeToAction = new SwipeToAction(recyclerView, new SwipeToAction.SwipeListener<Book>() {
        @Override
        public boolean swipeLeft(final Book itemData) {
            //do something
            return true; //true will move the front view to its starting position
        }
    
        @Override
        public boolean swipeRight(Book itemData) {
            //do something
            return true;
        }
    
        @Override
        public void onClick(Book itemData) {
            //do something
        }
    
        @Override
        public void onLongClick(Book itemData) {
            //do something
        }
      });
    

License

Copyright (C) 2015 Dift.co

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.