Android


Autofit grid layout Recyclerview

Autofit grid layout Recyclerview

Recycler view is the powerfull android tool to create a list as well as a grid in android. Nowadays, the Autofit grid layout RecyclerView is trending just because different types of screens are in the market.

Everyone is now familiar with the Grid view with the help of GridLayoutManager. But, there is something silly in this layout manager. This layout manager is not working properly on all types of screens.

So now, this example will give you an amazing trick to creating an autofit grid layout RecyclerView.

What is Recyclerview?

RecyclerView is the powerful tool to draw a list or grid in your android application When RecyclerView is not there then the user needs ListView for the list and GridView for the grid, But now android provides a great tool RecyclerView to draw both with one single view. Android RecyclerView is a more advanced, powerful and flexible version of the ListView. Android RecyclerView is similar to ListView.

You can visit this URL to know more about Recyclerview.

What is Cardview?

As with material design, a new view was introduced through the support v7 library, called CardView. We will show you how Android CardView can be implemented in a RecyclerView list. Android CardView provides a more advanced and flexible way of implementing complex and custom listview with more functionality that is required for your apps.

AutoFit Gridview

Autofit Recyclerview is used to show the number of a column which can be auto adjust with screen size. We extend RecyclerView to get access to its width. As for the width of each item, when GridView has android:numColumns=”auto_fit”, it uses android: column width to compute the number of columns. You can see it in this tutorial.

AutoFitGridRecyclerView

In the constructor, we read the value of android: column width and save it in a member variable.

Gradle dependency

All these basics dependency needs to add in the Gradle file to implement this project.

Activity Main XML

The main layout file will be like this.

AutoFit Grid RecyclerView with content scrolling XML

Adapter layout

To achieve your grid layout you can make your own style like this layout.

Dataset for your list

Data Model

Adapter for view

Autofit grid layout RecyclerView
Autofit grid layout RecyclerView
Autofit grid layout RecyclerView
Autofit grid layout RecyclerView

Main Activity

Conclusion

Finally, in conclusion, I can say that we can create an autofit grid layout with the help of RecyclerView. Hope that this code will help you to create your list as well as a grid for all type of screens either landscape mode or portrait mode.

Published by Amit Pandey

Professional Android app Developer, Full Stack Web Developer, Blogger, Entrepreneur And Founder of Coding Issue. I am always trying to learn new things or new possible ways to do things.

Did you find this page helpful?

X

Leave a Reply

Your email address will not be published. Required fields are marked *

*

*

*