Reyclerview android

Android Recyclerview Material Design with Example

Android Recyclerview in the advanced version of the listview. RecyclerView simplifies the display and handle of the large set of data.It’s coming with default animations.RecyclerView provides layout managers for positioning items.

Android recyclerview material design with Example downloadAndroid recyclerview material design with Example github

Advantages of Android Recyclerview

1) ViewHolder Pattern
In a ListView, it was recommended to use the ViewHolder pattern but it was never a compulsion. In case of RecyclerView, this is mandatory using the RecyclerView.ViewHolder class. This is one of the major differences between the ListView and the RecyclerView.

It makes things a bit more complex in RecyclerView but a lot of problems that we faced in the ListView are solved efficiently.

2) LayoutManager
This is another massive enhancement brought to the RecyclerView. In a ListView, the only type of view available is the vertical ListView. There is no official way to even implement a horizontal ListView.

Now using a RecyclerView, we can have a

i) LinearLayoutManager – which supports both vertical and horizontal lists,

ii) StaggeredLayoutManager – which supports Pinterest like staggered lists,

iii) GridLayoutManager – which supports displaying grids as seen in Gallery apps.

And the best thing is that we can do all these dynamically as we want.

3) Item Animator
ListViews are lacking in support of good animations, but the RecyclerView brings a whole new dimension to it. Using the RecyclerView.ItemAnimator class, animating the views becomes so much easy and intuitive.

4) Item Decoration
In case of ListViews, dynamically decorating items like adding borders or dividers was never easy. But in case of RecyclerView, the RecyclerView.ItemDecorator class gives huge control to the developers but makes things a bit more time consuming and complex.

5) OnItemTouchListener
Intercepting item clicks on a ListView was simple, thanks to its AdapterView.OnItemClickListener interface. But the RecyclerView gives much more power and control to its developers by the RecyclerView.OnItemTouchListener but it complicates things a bit for the developer.

Cardview Android Material Design with Example

Android Recyclerview Search Filter Example

Recyclerview Android Support Library

For using RecyclerView in your project you need to add the recycler view support library to your project. Add the following gradle dependency to project build.gradle file.

compile ''
compile ''
compile ''

Add into activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android=""



public class Items {

    private String name;
    private int price;

    Items(String mName,int mPrice){ = mName;
        this.price = mPrice;

    public int getPrice() {
        return price;

    public void setPrice(int price) {
        this.price = price;

    public String getName() {
        return name;

    public void setName(String name) { = name;

Listener for Click & Long Press Events for the Recyclerview

public interface ClickListener {  
   void onClick(View view, int position);  
   void onLongClick(View view, int position);  

Adapter for the Recyclerview

public class RecyclerviewItemAdapter extends RecyclerView.Adapter<RecyclerviewItemAdapter.MyViewHolder> {

    private List<Items> itemsList;

     class MyViewHolder extends RecyclerView.ViewHolder{

        public TextView name,price;

        public MyViewHolder(View itemView) {
            name = (TextView) itemView.findViewById(;
            price = (TextView) itemView.findViewById(;
    RecyclerviewItemAdapter(List<Items> mItemList){
        this.itemsList = mItemList;
    public RecyclerviewItemAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_row,parent,false);
        return new MyViewHolder(view);

    public void onBindViewHolder(RecyclerviewItemAdapter.MyViewHolder holder, int position) {
        Items item = itemsList.get(position);;

    public int getItemCount() {
        return itemsList.size();

Implementation of the Touch & Long Press Events

public class RecyclerTouchListener implements RecyclerView.OnItemTouchListener {

    private GestureDetector gestureDetector;
    private ClickListener clickListener;

    public RecyclerTouchListener(Context context, final RecyclerView recyclerView, final ClickListener clickListener) {
        this.clickListener = clickListener;
        gestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
            public boolean onSingleTapUp(MotionEvent e) {
                return true;

            public void onLongPress(MotionEvent e) {
                View child = recyclerView.findChildViewUnder(e.getX(), e.getY());
                if (child != null && clickListener != null) {
                    clickListener.onLongClick(child, recyclerView.getChildPosition(child));

    public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {

        View child = rv.findChildViewUnder(e.getX(), e.getY());
        if (child != null && clickListener != null && gestureDetector.onTouchEvent(e)) {
            clickListener.onClick(child, rv.getChildPosition(child));
        return false;

    public void onTouchEvent(RecyclerView rv, MotionEvent e) {

    public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {


In the MainActivity setup the Recyclerview

public class MainActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private RecyclerTouchListener recycleCTouchListener;
    private RecyclerviewItemAdapter recyclerviewItemAdapter;

    private List<Items> itemsList;

    protected void onCreate(Bundle savedInstanceState) {
        itemsList = new ArrayList<>();
        recyclerView = (RecyclerView)findViewById(;

        recyclerviewItemAdapter = new RecyclerviewItemAdapter(itemsList);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
        recyclerView.setItemAnimator(new DefaultItemAnimator());

        recyclerView.addOnItemTouchListener(new RecyclerTouchListener(getApplicationContext(), recyclerView, new ClickListener() {
            public void onClick(View view, int position) {
                Toast.makeText(getApplicationContext(),itemsList.get(position).getName()+" selected ",Toast.LENGTH_SHORT).show();

            public void onLongClick(View view, int position) {
                Toast.makeText(getApplicationContext(),itemsList.get(position).getName()+" long pressed ",Toast.LENGTH_SHORT).show();

    private void prepareItems(){
        Items items = new Items("Pen",20);
        items = new Items("Pencil",5);
        items = new Items("Book",30);
        items = new Items("ink",15);
        items = new Items("scale",10);

setLayoutManager – The layout manager decides how the data in the RecyclerView is displayed.

addOnItemTouchListener – Add the Touch Listener for the RecycularView.

setAdapter – Set the adapter for the RecycularView.

Android Bottom Sheet Material Design with Example

Android Bottom Navigation Material Design with Example


Recyclerview example

Android recyclerview material design with Example download