SubscribeSubscribe with Androidlift to get new post on mail.

Android RecyclerView, CardView Example

What is RecyclerView?

Latest widgets introduced in Android is RecyclerView. So many say its a replacement of existing ListView widget, also termed as ListView 2.0. we browsed though the entire web, in search of a working Android RecyclerView example with explanation. But couldn’t find one. After a lot of struggle we finally made one and decided to write a tutorial for it ourself. So here it is, in this Android RecyclerView Tutorial we demonstrate a working example of RecyclerView, with some basic functionality. Where we shows you how to include a RecyclerView into an Android app project with an onItemClick event along with CardView.

Android RecyclerView falls under the material design of android. Hence to maintain backward compatibility, we will use AppCompat library. We will try to keep this tutorial on Android RecyclerView short and simple as it consists a huge number of classes and each one of them is designed to be customized.
So lets start.

Output

ANDROID RECYCLERVIEW, CARDVIEW EXAMPLE
ANDROID RECYCLERVIEW, CARDVIEW EXAMPLE

RecyclerView with different layouts

One of the new concepts introduced in RecyclerView is the Layout managers. This class basically defines the type of layout which RecyclerView should use. In RecyclerView you can define three types of LayoutManager (s).

LinearLayoutManager – This LayoutManager can be used to display linear lists, they could be vertical or horizontal.
GridLayoutManager – Earlier in android GridView was the only widget to display grids, but now with RecyclerView, GridLayoutManager can be used to display grids.
StaggeredGridLayoutManager – Another great customization is StaggeredGridLayoutManager, this is used to display a staggered grid.
In this Android RecyclerView Tutorial for simplicity I will be using a GridLayoutManager for displaying a vertical list of items.

Android RecyclerView ViewHolder

The concept of RecyclerView.ViewHolder is same as it was in the ListView. Simply said: when a view goes out of visible area it is kept for recycling. Earlier this was done through the custom view holder pattern. But now with RecyclerView a ViewHolder class is included in the adapter by default. Therefore now its a compulsion for everyone to implement this class. There are many advantages to this approach, like it keeps references to the views in your items, which in turn decreases the overhead of creating new references every time an item is displayed.

CardView widgets

Use the CardView widget to create cards that will look the same wherever you use them in your apps.
You can specify their background, colors, whether their corners should be rounded and if they should have a shadow.
We’ll use both the CardView and RecyclerView widgets in our tutorial app.

Android RecyclerView Adapter

The adapter provides access to our data. It also provides the views for the items. We create our custom recycler adapter by extending the RecyclerView.Adapter class.

There are three methods that you must implement:

onCreateViewHolder () – creates a new ViewHolder to contain our text and image
onBindViewHolder() – displays our text and image at the specified position in the list
getItemCount() – gets the number of items in the adapter

For RecyclerView, CardView, we need to add the following dependencies to our project build.xml

Create project as RecyclerViewDemo in AndroidStudio 1.4.1.

build.gradle – app

 

styles.xml

 

colors.xml

 

activity_main.xml

 

grid_item.xml

 

MainActivity.java

 

GridAdapter.java

 

ItemClickListener.java

 

AndroidManifest.xml

 

Best luck and do research on androidlift.info..!

 

(278)

  • vinod

    sir, how to implement add to cart using recyclerview cardview by using php mysql in android

    • galaxystechnology@gmail.com

      Maintain one Model Arraylist for of selected products from recycle view and post it to server using HTTPURLCONNECTION POST method.

  • Asif Iqbal

    How can i implement Search view on a Recycler list view card view ?