Android Basics – Adding a ViewHolder to the RecyclerViewAdapter

When you create a RecyclerView Adapter then you need to create a view holder by extending the RecyclerView.ViewHolder class.

Like the name suggests a ViewHolder is used to hold and display the View. Recycler view maintains a set of these views.

The number of these holders depends on the size of the view.

When the Recycler view is first constructed it builds this set by calling the onCreateViewHolder() method.

public static class ViewHolder extends RecyclerView.ViewHolder{
    CardView mCardView;
    public ViewHolder(View itemView) {
        super(itemView);
        mCardView = (CardView) itemView;
    }
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    CardView cardView = (CardView) LayoutInflater.from(parent.getContext())
            .inflate(R.layout.card_view_sample,parent,false);

    return new ViewHolder(cardView);
}

Source Code is available here

Android Basics – Create the basic Adapter for RecyclerView

To create a basic Adapter which can be used by the RecyclerView create a new class and extend RecyclerView.Adapter

Once you do this you need to override the following three methods

  • The getItemCount method is used to return the number of items in the data set.
  • The onCreateViewHolder method is used to create the views.
  • The onBindViewHolder method is used to bind views to data.
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    return null;
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

}

@Override
public int getItemCount() {
    return 0;
}

Source Code is available here

Android Basics – An introduction to the Recycler View Adapter

Recycler View is an efficient and an advanced version of a List View. It can be used to display a list of Card Views.

This widget is a container for displaying large data sets that can be scrolled very efficiently by maintaining a limited number of views.

Courtesy – http://developer.android.com/training/material/lists-cards.html

In order to do this it uses an Adapter and a Layout Manager. The Adapter needs to be created by you and should be a sub class of RecyclerView.Adapter

The Adapter is responsible to create and configure the View.

Android Basics – Learn how to create a Card View

The last Article introduced you to Material Design basics. In this Article you will learn how to create a Card View.

Steps to add a CardView

  • Add support libraries – com.android.support.cardview and recyclerview
  • Create a layout xml file for the card view.
  • Refer to the below code snippet
<?xml version="1.0" encoding="utf-8"?>

<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:layout_margin="5dp"
    card_view:cardCornerRadius="4dp">

    <LinearLayout android:layout_width="match_parent"
                  android:layout_height="fill_parent"
                  android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Hello Card View"
            android:layout_margin="5dp"/>

    </LinearLayout>

</android.support.v7.widget.CardView>

Source Code is available here

Android Basics – An Introduction to Material Design

Material Design was launched with Android 5.0 (API level 21). The purpose being to provide consistent look and feel to all the Apps. So your App looks consistent with a Google App like Gmail.

It uses Animation and 3D effects.

Android provides the following elements for you to build material design apps:

  1. A new theme – Provides a new style for your App.
  2. New widgets for complex views
  3. New APIs for custom shadows and animations

Two of the most important Material Design widgets are Recycler and Card Views.

RecyclerView widget is a more pluggable version of ListView that supports different layout types and provides performance improvements.

CardView widget lets you display important pieces of information inside cards that have a consistent look and feel.

In addition to the X and Y properties, views in Android now have a Z property. Views with higher Z values cast bigger shadows and appear on top of other views.
To learn more about the principles of Material Design visit the following URL

Source : Android Documentation