Android Swipe To Dismiss : RecyclerView – Remove Items [ItemTouchHelper]

Android Swipe To Dismiss : RecyclerView – Remove Items [ItemTouchHelper]







Great.Now guys today we are at Java Android again.Discussing Drag/Swipe to Dismiss in our Custom RecyclerView.We are implementing it using ItemTouchHelper class,so no third party libraries.In short this is what we do :

  • Custom RecyclerView with images and text,with help of our Adapter and ViewHolder subclasses.
  • Implement Swipeability of our Custom Views with help of Simple callbacks.
  • Remove an item onSwiped(), that is when swiped,from our adapter.
  • We then call notifyItemRemoved(adapterPosition), to refresh our RecyclerView and Adapter.

 

RecyclerView Swipe To Dismiss Project Structure

RecyclerView Swipe To Dismiss Project Structure

 

First we have our Adapter class a below.Of course it inherits from RecyclerView.Adapter base class.

 

package com.tutorials.hp.recyclerswipetodismiss.mRecycler;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.tutorials.hp.recyclerswipetodismiss.R;
import com.tutorials.hp.recyclerswipetodismiss.mDataObject.TVShow;
import java.util.ArrayList;
/**
 * Created by Oclemmy on 4/29/2016 for ProgrammingWizards Channel and http://www.Camposha.info.
 */
public class MyAdapter extends RecyclerView.Adapter<MyHolder> {
    Context c;
    ArrayList<TVShow> tvShows;
    public MyAdapter(Context c, ArrayList<TVShow> tvShows) {
        this.c = c;
        this.tvShows = tvShows;
    }
    //INITIALIE VH
    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v= LayoutInflater.from(parent.getContext()).inflate(R.layout.model,parent,false);
        MyHolder holder=new MyHolder(v);
        return holder;
    }
    //BIND DATA
    @Override
    public void onBindViewHolder(MyHolder holder, int position) {
        holder.nametxt.setText(tvShows.get(position).getName());
        holder.img.setImageResource(tvShows.get(position).getImg());
    }
    @Override
    public int getItemCount() {
        return tvShows.size();
    }
    //DISMISS
    public void dismissTVShow(int pos)
    {
        tvShows.remove(pos);
        this.notifyItemRemoved(pos);
    }
}

 

Here's our ViewHolder class.

 

package com.tutorials.hp.recyclerswipetodismiss.mRecycler;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.tutorials.hp.recyclerswipetodismiss.R;
/**
 * Created by Oclemmy on 4/29/2016 for ProgrammingWizards Channel and http://www.Camposha.com.
 */
public class MyHolder extends RecyclerView.ViewHolder {
    TextView nametxt;
    ImageView img;
    public MyHolder(View itemView) {
        super(itemView);
        this.nametxt= (TextView) itemView.findViewById(R.id.nameTxt);
        this.img= (ImageView) itemView.findViewById(R.id.movieImage);
    }
}

 

RecyclerView CardViews with Images and Text

RecyclerView CardViews with Images and Text

 

We have our SwipeHelper class below.It derives from ItemTouchHelper.SimpleCallback.Take note we have two constructors,one of them,our custom constructor,is going to take our adapter reference.Hence giving us access to the dimsissTV show method that we define in our RecyclerView.Adapter subclass.

 

package com.tutorials.hp.recyclerswipetodismiss.mSwiper;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import com.tutorials.hp.recyclerswipetodismiss.mRecycler.MyAdapter;
/**
 * Created by Oclemmy on 4/29/2016 for ProgrammingWizards Channel and http://www.Camposha.com.
 */
public class SwipeHelper extends ItemTouchHelper.SimpleCallback {
    MyAdapter adapter;
    public SwipeHelper(int dragDirs, int swipeDirs) {
        super(dragDirs, swipeDirs);
    }
    public SwipeHelper(MyAdapter adapter) {
        super(ItemTouchHelper.UP | ItemTouchHelper.DOWN,ItemTouchHelper.LEFT);
        this.adapter = adapter;
    }
    @Override
    public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
        return false;
    }
    @Override
    public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
        adapter.dismissTVShow(viewHolder.getAdapterPosition());
    }
}

 

We finally have our MainActivity class :

 

package com.tutorials.hp.recyclerswipetodismiss;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import com.tutorials.hp.recyclerswipetodismiss.mDataObject.TVShow;
import com.tutorials.hp.recyclerswipetodismiss.mDataObject.TVShowsCollection;
import com.tutorials.hp.recyclerswipetodismiss.mRecycler.MyAdapter;
import com.tutorials.hp.recyclerswipetodismiss.mSwiper.SwipeHelper;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
    RecyclerView rv;
    MyAdapter adapter;
    ArrayList<TVShow> tvShows;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        //RECYCLER
        rv= (RecyclerView) findViewById(R.id.mRecycler);
        rv.setLayoutManager(new LinearLayoutManager(this));
        //DATA
        tvShows= TVShowsCollection.getTVShows();
        //ADAPTER
        adapter=new MyAdapter(this,tvShows);
        rv.setAdapter(adapter);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, String.valueOf(tvShows.size()), Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
        ItemTouchHelper.Callback callback=new SwipeHelper(adapter);
        ItemTouchHelper helper=new ItemTouchHelper(callback);
        helper.attachToRecyclerView(rv);
    }
}

 

RecyclerView Swipe To Dismiss Cards

RecyclerView Swipe To Dismiss Cards

 

 

Look for more please the full source code is attached above.

 

For more explanations and demo please check the video tutorial below :

Android Swipe To Dismiss : RecyclerView - Remove Items [ItemTouchHelper]

 



    Leave a Reply

    Your email address will not be published. Required fields are marked *

    5 × 4 =

    COMMENTS