Android Swipe to delete

In my previous article we discuss about a library which gives you swipe to dismiss dialog and this article is well written by Ponglang from Thailand. And today we discuss about Android swipe to delete feature using Swiperoo Adapter which is an extendable adapter which provides swipe-to-delete feature on your customized row item.

Installation of Android Swipe to delete library

Gradle

Add Gradle dependency:

dependencies {
    compile 'us.belka:swiperoo-library:1.0.0'
}

Maven

<dependency>
  <groupId>us.belka</groupId>
  <artifactId>swiperoo-library</artifactId>
  <version>1.0.0</version>
  <type>pom</type>
</dependency>

Usage

In the app folder you will find a working example of the library

You need to do 3 things to make the library works:

1. Create your ViewHolder

public class MyViewHolder extends SwiperooViewHolder<String> {

        static class Factory implements SwiperooViewHolder.Factory {

            @Override
            public SwiperooViewHolder createViewHolder(Context context, ViewGroup parent, int viewType) {
                return new MyViewHolder(from(context).inflate(YOUR_ITEM_LAYOUT, parent, false), context);
            }
        }

        public MyViewHolder(View itemView, Context context) {
            super(itemView, context);
        }

        @Override
        public void bindViewHolder(String data) {

        }
    }

Pay attention to the Factory class, you need to return an instance of your ViewHolder (Factory pattern)

2. Create your Adapter

public class MyAdapter extends SwiperooAdapter<String> {

        public MyAdapter(Context context, List<String> items, Listener listener, SwiperooViewHolder.Factory factory) {
            super(context, items, listener, factory);
        }
    }

3. Put it all togheter

mSwiperooRecyclerView.setLayoutManager(new LinearLayoutManager(this));
    MyAdapter adapter = new MyAdapter(this, items, this, new MyViewHolder.Factory());

    mSwiperooRecyclerView.setAdapter(adapter);

    adapter.addSupportToSwipeToDelete(this, mSwiperooRecyclerView);

You can use “this” as third parameter in the Adapter or you can directly pass in an implementation of the Listener, both ways you will implement the interface to handle item delete

Listeners

@Override
    public void onItemDeleted(String item) {
       ///Do your stuff with the deleted item
    }

Pay attention

Your item layout container must be a Relative Layout, otherwise the Holder will throw you a NotSupportedException.

Hope you like this tutorial please comment us with your suggestions.

By Tell Me How

It is a technology blog and admin has excellent experience in programming from 5+ year. You can contact us at ceo.tellmehow@gmail.com

Share your thoughts

Leave a Reply

Loading Facebook Comments ...
Loading Disqus Comments ...