GridView

Hello friends we discussed RecyclerView as RecyclerView is robust and advance more than ListView or GridView we will use RecyclerView to make a grid.
As we discuss LayoutManagers in RecyclerView, to make GridView we will use GridLayoutManagers to make GridView.

So don't waste your time we start with a project and create layout file i.e. main_activity.xml as below

main_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerGridView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</androidx.appcompat.widget.LinearLayoutCompat>

Now we will create grid_view_item.xml as below

create grid_view_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <androidx.appcompat.widget.AppCompatImageView
        android:id="@+id/imgView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerInParent="true"
        android:layout_margin="5dp"
        android:scaleType="fitXY" />
</RelativeLayout>

All done here now create your MainActivity.java and you will find in onCreate() method GridLayoutManager, int constructor second argument i.e. an integer and represents column int grid,

MainActivity.java
package com.theitbulls.gridviewex;

import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class MainActivity extends AppCompatActivity {
    private int[] imgResIds = new int[]{ //
            R.drawable.wall_thumb_1, R.drawable.wall_thumb_2, R.drawable.wall_thumb_3, //
            R.drawable.wall_thumb_4, R.drawable.wall_thumb_5, R.drawable.wall_thumb_6, //
            R.drawable.wall_thumb_7, R.drawable.wall_thumb_8, R.drawable.wall_thumb_9, //
            R.drawable.wall_thumb_10, R.drawable.wall_thumb_11, R.drawable.wall_thumb_12, //
            R.drawable.wall_thumb_13, R.drawable.wall_thumb_14, R.drawable.wall_thumb_15, //
            R.drawable.wall_thumb_16, R.drawable.wall_thumb_17, R.drawable.wall_thumb_18, //
            R.drawable.wall_thumb_19, R.drawable.wall_thumb_20, R.drawable.wall_thumb_21, //
            R.drawable.wall_thumb_22, R.drawable.wall_thumb_23, R.drawable.wall_thumb_24, //
            R.drawable.wall_thumb_25, R.drawable.wall_thumb_26, R.drawable.wall_thumb_27, //
            R.drawable.wall_thumb_28, R.drawable.wall_thumb_29, R.drawable.wall_thumb_30, //
            R.drawable.wall_thumb_31, R.drawable.wall_thumb_32, R.drawable.wall_thumb_33, //
            R.drawable.wall_thumb_34, R.drawable.wall_thumb_35, R.drawable.wall_thumb_36};

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main_activity);

        GridLayoutManager layoutManager = new GridLayoutManager(this, 3);
        layoutManager.setOrientation(RecyclerView.VERTICAL);

        RecyclerView recyclerGridView = findViewById(R.id.recyclerGridView);
        recyclerGridView.setLayoutManager(layoutManager);

        GridViewAdapter adapter = new GridViewAdapter(imgResIds);

        recyclerGridView.setAdapter(adapter);
    }
}


And now most important we will create GridViewAdapter.java

GridViewAdapter.java
package com.theitbulls.gridviewex;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.recyclerview.widget.RecyclerView;

public class GridViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private int[] imgResIds;

    public GridViewAdapter(int[] imgResIds) {
        this.imgResIds = imgResIds;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        final View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.grid_view_item, parent, false);
        return new ViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ViewHolder myHolder = (ViewHolder) holder;

        myHolder.imgView.setImageResource(imgResIds[position]);
    }

    @Override
    public int getItemCount() {
        return imgResIds.length;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private final AppCompatImageView imgView;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);

            imgView = itemView.findViewById(R.id.imgView);
        }
    }
}


Download: GridViewExample.zip