recyclerview1

Android RecyclerView

Android RecyclerView is a more advanced version of ListView with improved performance and other benefits. Using RecyclerView and CardView together, both lists and grids can be created very easily.In this tutorial, we will know how to render a RecyclerView. Also, we will be writing RecyclerView adapter and adding row click listener.

Before you start, make sure that you updated your Android Studio to the latest version. Currently, my Android Studio is updated to  Android Studio 2.0 Preview 6.

Below is the Recycler View widget:

<android.support.v7.widget.RecyclerView 
android:id="@+id/recycler_view" 
android:scrollbars="vertical" 
android:layout_width="match_parent" 
android:layout_height="wrap_content"/>

1. Creating New Project

1. In Android Studio, go to File ⇒ New Project & create a new project. When it prompts to select a default activity, select Blank Activity and proceed.

2. Open build.gradle and add recycler view dependency. com.android.support:recyclerview-v7:23.1.1 and rebuild the project.

build.gradle
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.android.support:design:23.1.1'
    compile 'com.android.support:recyclerview-v7:23.1.1'
}

3. With the latest version of build tools, Android Studio creates two layout files for each activity. For main activity, it created activity_main.xml (contains CoordinatorLayout and AppBarLayout) and content_main.xml (for the actual content). Open content_main.xml and the recycler view widget.

content_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
app:layout_behavior="@string/appbar_scrolling_view_behavior" 
tools:showIn="@layout/activity_main" 
tools:context=".MainActivity">
 
<android.support.v7.widget.RecyclerView 
android:id="@+id/recycler_view" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:scrollbars="vertical" />
 
</RelativeLayout>

Writing the Adapter Class

To write the adapter, extend the RecyclerView.Adapter class and override the mandatory callback methods.

package com.techhiking.recyclerview;
 
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
 
import java.util.List;
 
public class RecycleAdapter extends RecyclerView.Adapter<RecycleAdapter.MyViewHolder> {
 
 
    public class MyViewHolder extends RecyclerView.ViewHolder {
        public TextView title, year, genre;
 
        public MyViewHolder(View view) {
            super(view);
            title = (TextView) view.findViewById(R.id.title);
            genre = (TextView) view.findViewById(R.id.genre);
            year = (TextView) view.findViewById(R.id.year);
        }
    }
 
 
    public RecycleAdapter(List list) {
      // initialize data list here.
    }
 
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//Inflate Row view
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.row_view, parent, false);
 
        return new MyViewHolder(itemView);
    }
 
    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
       //set view data depending upon position.
    }
 
    @Override
    public int getItemCount() {
        return list.size();
    }
}

In your Activity add the following code in onCreate() method-

 recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
 
        mAdapter = new RecycleAdapter(list);//pass the fatalist
        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
        recyclerView.setLayoutManager(mLayoutManager);
        recyclerView.setAdapter(mAdapter);

Now if you run the app, you can see the list displayed in a list manner.

Leave a Reply

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