In this tutorial, I am going to explain how we can create and use RecyclerView in our Android Application in simple steps. RecyclerView is an advance version of ListView.


Step 1. Create a new Android project and name It in my case my project name is RecyclerGridView.

Step 2. Now before we start we need to add some dependencies in our app so open app -> build.gradle and add below code in it

Step 3. Now open AndroidManifest.xml and add Internet permission to make a network calls to load data.

Step 4. Now create two packages inside your main package as shown in the image below

Adapter and model are two folders which I create, model folder holds data which we load from API and in adapter folder, we can create Adapter which set data in View

Step 5. Now we can create a Model my model name is ImageModel and paste below code in it

Step 6. Now create ImagesAdapter file inside the adapter folder and paste below code in it.

In the above code first, we extend RecyclerView Adapter and pass or customViewHolder class in it in CustomViewHolder class we basically find view by its id as shown in the above code. Now in our main class ImagesAdapter we create a constructor and pass three thing ArrayList(Data), Parent context and activity. In the above code, you also see I import some method from the adapter class.

In above code pass our View which we can Inflate in our RecyclerView and also pass that view to our CustomViewHolder where we can find view by its Id.

Now above code is easy to understand by its name so there is no need for explanation. In the above code, we find position get the size of the array we pass from the constructor.

Now in the above code, we can set data to view. In my case, I set an image and text in our app.

Step 7. In this step, I am creating a Layout file which I am using in the above code in onCreateViewHolder method

Step 8. Now we can call all this code in our MainActivity class as shown in below code and I am also using API to load the image using OkHttp if you want to learn how we can load data from API follow this post GET And POST request using OKHttp in Android Application.

You need to add your API to request images you get API from there site. In the above code, I am using RecycleView as Gridview with the help of GridLayoutManager.

With the help of GridLayoutManager, we can define no of the column we want to show and pass manager to our RecycleView as shown in the above code.

That’s it hope this above example help you to understand RecyclerView in the android application.