Android RecyclerView is 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. Here is the complete information about RecyclerView and other examples. In this tutorial we are going to learn how to render a simple RecyclerView with a custom layout. We’ll also learn writing a adapter class, adding list divider and row click listener.

First Add Dependency: –

compile 'com.android.support:recyclerview-v7:25.1.1'

Java File MainActivity.java: –

package safal.bhatia.recylerviewexample;
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
Activity sActivity;
ArrayList<String> arrayList;
RecyclerView recycler_view;
RecyclerAdapter adapter;
RecyclerView.LayoutManager layoutManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recycler_view = (RecyclerView) findViewById(R.id.recycler_view);
recycler_view.setNestedScrollingEnabled(true);
adapter = new RecyclerAdapter(sActivity, arrayList);
layoutManager = new LinearLayoutManager(sActivity);
recycler_view.setLayoutManager(layoutManager);
recycler_view.setHasFixedSize(true);
recycler_view.setAdapter(adapter);
}
}

Java File RecyclerAdapter.java

package safal.bhatia.recylerviewexample;
import android.app.Activity;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
Activity sActivity;
private ArrayList<String> arrayList1;
public RecyclerAdapter(Activity sActivity, ArrayList<String> arrayList1) {
this.sActivity = sActivity;
this.arrayList1 = arrayList1;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_recycler, parent, false);
return new FindHolder(view);
}
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
if (holder instanceof FindHolder) {
final FindHolder findHolder = (FindHolder) holder;
findHolder.item_recycler.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(sActivity, "You Click on Recycler Item", Toast.LENGTH_SHORT).show();
}
});
}
}
@Override
public int getItemCount() {
return 10;
}
public static class FindHolder extends RecyclerView.ViewHolder {
RelativeLayout item_recycler;
public FindHolder(View view) {
super(view);
item_recycler = (RelativeLayout) view.findViewById(R.id.item_recycler);
}
}
}

Xml File activity_main: –

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="safal.bhatia.recylerviewexample.MainActivity">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/recycler_view">
</android.support.v7.widget.RecyclerView>
</LinearLayout>

Xml File item_recycler: –

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:id="@+id/item_recycler"
android:layout_height="50dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:paddingLeft="10dp"
android:text="Safal Bhatia" />
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/colorAccent" />
</LinearLayout>

The best CSS cleanup tool helps you to organize your style files.