Android Basics – Learn how to handle the user clicks in a RecyclerView

You are familiar with how to respond to user clicks in a ListView using an OnItemClickListener. RecyclerView does not have a similar set of built in functionality so you will have to write some code to handle user clicks.

The code you will add will be in the Adapter’s onBindViewHolder. You will also add an interface, similar to the ay you did for Fragment interaction.

Follow the steps below:

Add a Listener interface to the Adapter.

public static interface Listener{
    public void onClick(int position);
}

Add a setListener method. This method will be called from the Activity.

public void setListener(Listener listener) {
    mListener = listener;
}

Add setOnClickListener

cardView.setOnClickListener(new View.OnClickListener(){
    @Override
    public void onClick(View view){
        if(mListener != null){
            mListener.onClick(position);
        }
    }
});

In Activity class set the Listener defined in Adapter.

SampleAdapter adapter = new SampleAdapter(inputData);
adapter.setListener(new SampleAdapter.Listener() {
    @Override
    public void onClick(int position) {
        //Add code here
    }
});

Source code is available here

Android Basics – Learn how to use the onBindViewHolder method to bind the data to the View

So far you have created an Adapter for the RecyclerView. You have also added a View Holder.

In order to display data you need to write code to the onBindViewHolder method. This method will map the data to the Card View.

@Override
public void onBindViewHolder(ViewHolder holder, int position) {
    CardView cardView = holder.mCardView;
    TextView cardText = (TextView) cardView.findViewById(R.id.cardTextId);
    cardText.setText(textData[position]);
}

Source code is available here.

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 use Adapter with ListView

In the last Post you learnt about the ListActivity class. In this Tutorial I want to introduce you to Adapters and how to use them with ListView.

Adapters are used to bind or connect View with Data Source. Now a data source can be an array, a database or a web service.

An ArrayAdapter enables us to work with Arrays. Creating one is pretty simple.

ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, colors);

You can follow along with me in the Video above to understand about Adapters.

Source code can be found here