RecyclerView and GridLayoutManager Example

RecyclerView items can be displayed in a grid using GridLayoutManager. In this post, I’ll show how to use RecyclerView and GridLayoutManager.

To know basics about RecyclerView, you can read my previous post on Recycler View.

Below are details of all the required components for recycler view and grid layout manager example. This example is one of the screen in coupons apps which displays list of stores.

RecyclerView Item Layout Using ConstraintLayout

As you know RecyclerView is used to display list of items. So we need to define layout for each item. Let’s use ConstraintLayout to define RecyclerView item layout for this example as there are five widgets with different positional and alignment requirement and using ConstraintLayout improves UI performance. If you want to know more about constraint layout, read my previous post on constraint layout examples.






RecyclerView ConstraintLayout Output

Below is the output screen with recycler view, constraint layout, and grid layout manager in vertical orientation.

recycler view and constraint layout example

Activity Layout



RecyclerView Adapter

In RecyclerView, adapter binds data to view. Below is the explanation of adapter for our example, which binds store data to views in the item layout.

RecyclerView adapter is created by extending RecyclerView.Adapter. RecyclerView adapter needs a list of data items to be displayed in recycler view and context, which are passed to its constructor.

private List<ZoftinoStore> storesList;
private Context context;

public ZoftinoStoreAdapter(List<ZoftinoStore> stLst, Context ctx) {
    storesList = stLst;
    context = ctx;

ViewHolder class is defined in RecyclerView adapter. The nested view holder class is used to hold reference to views in recycler view item layout. When ViewHolder is instantiated, root view of recyclerview’s item layout is passed to constructor.

Click events can be handled by defining click event listeners. For our example, there are three click events that are handled separately to take different action. This example shows how to handle click event at RecyclerView item level and it also shows how to handle click events of child views in RecyclerView item.

 public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    public TextView maxCashback;
    public TextView totalCoupons;
    public ImageView storeImg;

    public ViewHolder(View view) {
        maxCashback = (TextView) view.findViewById(R.id.maxCbT);
        totalCoupons = (TextView) view.findViewById(R.id.cpnCntT);
        storeImg = (ImageView) view.findViewById(R.id.storeImg);

        Button viewcb = (Button) view.findViewById(R.id.viewCbB);
        Button viewcpn = (Button) view.findViewById(R.id.viewCpnB);


        viewcb.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Toast.makeText(context, "You view Cashback button for item position "+ getAdapterPosition(),
        viewcpn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Toast.makeText(context, "You view coupons button for item position "+ getAdapterPosition(),

    public void onClick(View v) {
        Toast.makeText(context, "You store item for item position "+ getAdapterPosition(),

RecyclerView calls createViewHolder method of recycler view adapter which in turn calls onCreateViewHolder method. Below is the implementation of onCreateViewHolder method, which inflates item layout, passes it to view holder and returns the view holder object to recycler view.

public ZoftinoStoreAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclergrid_item, parent, false);

    ViewHolder viewHolder = new ViewHolder(view);
    return viewHolder;

RecyclerView calls bindViewHolder method of recycler view adapter, which in turn calls onBindViewHolder method. This method populates the given view holder with item data from items list for the given position.

public void onBindViewHolder(ViewHolder holder, int position) {
    ZoftinoStore store = storesList.get(position);


In the activity onCreate method, GridLayoutManager and RecyclerView Adapter are set to RecyclerView. GridLayoutManager offers different constructors which allow you to specify how items will be displayed in RecyclerView.

protected void onCreate(Bundle savedInstanceState) {

    rv = (RecyclerView) findViewById(R.id.store_rv);

    GridLayoutManager gridlm = new GridLayoutManager(this, 2);
    ArrayList<ZoftinoStore> al = CouponStoreData.getStoreData();
    ZoftinoStoreAdapter rvadapter = new ZoftinoStoreAdapter(al,this);

GridLayoutManager Orientation

RecyclerView items can be displayed in horizontal or vertical orientation by setting the orientation property of GridLayoutManager with setOrientation method. Below is the output when orientation is set to GridLayoutManager.HORIZONTAL.

recyclerview and gridlayoutmanager example

GridLayoutManager SpanCount

With GridLayoutManager, number of rows in horizontal orientation or number of columns in vertical orientation can be configured by setting spancount with setSpanCount method. Below screen shows that RecyclerView items are displayed in three rows.

recycler view and grid layout manager spancount example