Simple RecyclerView Example on Android

You might have rad about the RecyclerView what is recyclerview how it works as i posted on my previous Post.

Now we will discuss actully how to use recyclerview.

Okey lets start it….!!!!!!!!!!!!

1st of all we need to create these Files:

Java Files:

1: Simple_recycler_view
2: Rc_Data
3: MyRecyclerViewAdapter

Layout Files:

1: rcycler_view.xml
2: rc_simple_single_item.xml

Add the entries to your buld.gradle

1: compile 'com.android.support:recyclerview-v7:+'
2: compile 'com.android.support:cardview-v7:23.4.+'

So now the are the main files you just have to copy and and paste into your packed folder

You can also just copy the code and without creating a java file just point your cursor onto the folder and press
ctrl+v in windows
cmd+v in MAC

Here we are with the block of code:

Simple_recycler_view.java

package info.infiniteloops.pkg_recycler_view;

/**
 * Created by asna on 8/11/16.
 */

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import java.util.ArrayList;

import info.infiniteloops.R;


public class Simple_recycler_view extends ActionBarActivity {
    private RecyclerView mRecyclerView;
    private RecyclerView.Adapter mAdapter;
    private RecyclerView.LayoutManager mLayoutManager;
    private static String LOG_TAG = "Simple_recycler_view";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.rcycler_view);

        mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
        mRecyclerView.setHasFixedSize(true);
        mLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(mLayoutManager);
        mAdapter = new MyRecyclerViewAdapter(getDataSet());
        mRecyclerView.setAdapter(mAdapter);
        RecyclerView.ItemDecoration itemDecoration =
                new DividerItemDecoration(this, LinearLayoutManager.VERTICAL);
        mRecyclerView.addItemDecoration(itemDecoration);

        // Code to Add an item with default animation
        //((MyRecyclerViewAdapter) mAdapter).addItem(obj, index);

        // Code to remove an item with default animation
        //((MyRecyclerViewAdapter) mAdapter).deleteItem(index);
    }

    @Override
    protected void onResume() {
        super.onResume();
        ((MyRecyclerViewAdapter) mAdapter).setOnItemClickListener(new
              MyRecyclerViewAdapter.MyClickListener() {
                  @Override
                  public void onItemClick(int position, View v) {
                      Log.i(LOG_TAG, " Clicked on Item " + position);
                  }
              });
    }

    private ArrayList<Rc_Data> getDataSet() {
        ArrayList results = new ArrayList<Rc_Data>();
        for (int index = 0; index < 100; index++) {
            Rc_Data obj = new Rc_Data("Title " + index,
                    "Short Description " + index);
            results.add(index, obj);
        }
        return results;
    }
}

Rc_data.java

package info.infiniteloops.pkg_recycler_view;

/**
 * Created by asna on 8/11/16.
 */

public class Rc_Data {
    private String mText1;
    private String mText2;

    Rc_Data (String text1, String text2){
        mText1 = text1;
        mText2 = text2;
    }

    public String getmText1() {
        return mText1;
    }

    public void setmText1(String mText1) {
        this.mText1 = mText1;
    }

    public String getmText2() {
        return mText2;
    }

    public void setmText2(String mText2) {
        this.mText2 = mText2;
    }
}

MyRecyclerViewAdapter.java

package info.infiniteloops.pkg_recycler_view;

import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.ArrayList;

import info.infiniteloops.R;

public class MyRecyclerViewAdapter extends RecyclerView
        .Adapter<MyRecyclerViewAdapter
        .Rc_DataHolder> {
    private static String LOG_TAG = "MyRecyclerViewAdapter";
    private ArrayList<Rc_Data> mDataset;
    private static MyClickListener myClickListener;

    public static class Rc_DataHolder extends RecyclerView.ViewHolder
            implements View
            .OnClickListener {
        TextView label;
        TextView dateTime;

        public Rc_DataHolder(View itemView) {
            super(itemView);
            label = (TextView) itemView.findViewById(R.id.textView);
            dateTime = (TextView) itemView.findViewById(R.id.textView2);
            Log.i(LOG_TAG, "Adding Listener");
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            myClickListener.onItemClick(getPosition(), v);
        }
    }

    public void setOnItemClickListener(MyClickListener myClickListener) {
        this.myClickListener = myClickListener;
    }

    public MyRecyclerViewAdapter(ArrayList<Rc_Data> myDataset) {
        mDataset = myDataset;
    }

    @Override
    public Rc_DataHolder onCreateViewHolder(ViewGroup parent,
                                               int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.rc_simple_single_item, parent, false);

        Rc_DataHolder Rc_DataHolder = new Rc_DataHolder(view);
        return Rc_DataHolder;
    }

    @Override
    public void onBindViewHolder(Rc_DataHolder holder, int position) {
        holder.label.setText(mDataset.get(position).getmText1());
        holder.dateTime.setText(mDataset.get(position).getmText2());
    }

    public void addItem(Rc_Data dataObj, int index) {
        mDataset.add(dataObj);
        notifyItemInserted(index);
    }

    public void deleteItem(int index) {
        mDataset.remove(index);
        notifyItemRemoved(index);
    }

    @Override
    public int getItemCount() {
        return mDataset.size();
    }

    public interface MyClickListener {
        public void onItemClick(int position, View v);
    }
}

Now we will work on the layout
so first:
for fastscroller you need to add the dependency in you java App level Gradle

 compile 'com.futuremind.recyclerfastscroll:fastscroll:0.1.5'

rcycler_view.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

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

    <com.futuremind.recyclerviewfastscroll.FastScroller
        android:id="@+id/fastscroll"
        android:orientation="vertical"
        android:layout_width="wrap_content"
        app:fastscroll__handleColor="#8f93d1"
        app:fastscroll__bubbleColor="#5e64ce"
        android:layout_height="match_parent"
        android:layout_alignParentRight="true"/>
</RelativeLayout>

 

rc_simple_single_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?android:attr/selectableItemBackground"
    android:orientation="vertical">
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@mipmap/ic_launcher"
        android:id="@+id/imageView2" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_toRightOf="@id/imageView2"
        android:layout_height="wrap_content"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingTop="5dp"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />


    <TextView
        android:id="@+id/textView2"
        android:layout_below="@+id/textView"
        android:layout_toRightOf="@+id/imageView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingBottom="5dp"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:text="Small Text"
        android:textAppearance="?android:attr/textAppearanceSmall" />
</RelativeLayout>

your entire Build.Gradle will looks like this

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "info.infiniteloops"
        minSdkVersion 14
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.4.0'
    compile 'com.android.support:design:23.4.0'
    compile 'com.android.support:recyclerview-v7:+'
    compile 'com.android.support:cardview-v7:23.4.+'
    compile 'com.futuremind.recyclerfastscroll:fastscroll:0.1.5'

}

Here is your Manifest files:


<activity android:name=".pkg_recycler_view.Simple_recycler_view"
            android:parentActivityName=".HomeScreen"></activity>

For project fork me on GitHub