Android RecyclerView Tutorial

Google was introduced the Material Design in Lollipop 5.0 API 21 which RecyclerView is one of the Material design.   Android RecyclerView is much more better than old generation ListView because it is more advanced and more flexible version of ListView. Besides, it can control the layout of the list by using the LayoutManager unlike ListView are always remain the vertical list. Moreover, the CardView widget is a new component that display the item in RecyclerView to make it look better.

In this tutorial, I will teach you some simple steps to coding the RecyclerView in your application. You will just take a little time to complete and make you easy to understand.

Creating a new Project

1. Open Android Studio IDE in your computer.
2. Create a new project and Edit the Application name to “RecyclerViewExample”.
(Optional) You can edit the company domain or select the suitable location for current project tutorial. Then click next button to proceed.
3. Select Minimum SDK (API 15:Android 4.0.3 (IceCreamSandwich). I choose the API 15 because many android devices currently are support more than API 15. Click Next button.
4. Choose “Empty Project” and Click Next button
5. Lastly, press finish button.

Add Dependency in build.gradle

To activate the RecyclerView in your project, you should add dependency in your build.gradle (Module:app). Just add “compile ‘com.android.support:recyclerview-v7:23.3.0′” in your dependencies.

Create a new class

After add your dependency, create a new java class and name it as MyAdapter. This class is the adapter of data set.

Android RecyclerView pic 1

Edit MyAdapter.java class

In the MyAdapter.java class extends RecyclerView.Adapter to get the RecycleView method in the class. This class will handler the views (eg. textview) of recyclerview.

RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
– It will automatically called when the RecyclerView need the ViewHolder to represent an item.

void onBindViewHolder(RecyclerView.ViewHolder holder, int position)
– It called by the RecyclerView to display data in specific position.

int getItemCount()
– It return a total item in the dataset.

Edit activity.main.xml layout

In the layout, open activity.main.xml and add recyclerview widget to your layout.

Edit MainActivity.java class

After that, copy below code to your MainActivity.java. This activity set RecyclerView to linear layout and set the adapter that you created above “MyAdapter” class. You also able to set your layout to Grid View by using GridLayoutManager. In this example, I am using LinearLayoutManager to display the recyclerview.

Run you Project

Now, you can start your project in your emulator or android device. RecyclerView is very easy to implement, you just need more practice and understanding the source code.

(Android RecyclerView)

Source Code

[sociallocker][/sociallocker]
(Visited 326 times, 1 visits today)
Advertisements

Kristofer Ng

A crazy android and web developer. Passion on developing android application and exploring new technology as a hobby.

You may also like...

1 Response

  1. November 17, 2016

    […] previous tutorial, I have implement a simple recyclerview tutorial to let you understand how its work. The recyclerview only display the static content is […]

Leave a Reply

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

GET YOUR EBOOK FOR FREE

Unlock free ebook "Beginning Programming ALL-IN-ONE Desk Reference for DUMMIES" now!

We will send a copy to your email