Adding header view to recyclerview with gridlayout manager
To add a header to a recyclerview with a gridlayout, first the adapter needs to be told that the header view is the first position - rather than the standard cell used for the content. Next, the layout manager must be told that the first position should have a span equal to the *span count of the entire list. *
Take a regular RecyclerView.Adapter class and configure it as follows:
Then in the activity/fragment:
The same approach can be used add a footer in addition to or instead of a header.
When creating a recyclerview with a gridlayout layout manager you have to specify the span count in the constructor. Span count refers to the number of columns. This is fairly clunky and doesn't take into account larger screen sizes or screen orientation. One approach is to create multiple layouts for the various screen sizes. Another more dynamic approach can be seen below.
First we create a custom RecyclerView class as follows:
This class determines how many columns can fit into the recyclerview. To use it you will need to put it into your layout.xml as follows:
Notice that we use the columnWidth attribute. The recyclerview will need it to determine how many columns will fit into the available space.
In your activity/fragment you just get a reference to the recylerview and set an adapter to it (and any item decorations or animations that you want to add). DO NOT SET A LAYOUT MANAGER
(where MyAdapter is your adapter class)
You now have a recyclerview that will adjust the spancount (ie columns) to fit the screen size. As a final addition you might want to center the columns in the recyclerview (by default they are aligned to layout_start). You can do that by modifying the AutofitRecyclerView class a little. Start by creating an inner class in the recyclerview. This will be a class that extends from GridLayoutManager. It will add enough padding to the left and right in order to center the rows:
Then when you set the LayoutManager in the AutofitRecyclerView use the CenteredGridLayoutManager as follows:
And that's it! You have a dynamic spancount, center aligned gridlayoutmanager based recyclerview.
This example adds a list of places with image and name by using an ArrayList of custom Place objects as dataset.
The layout of the activity / fragment or where the RecyclerView is used only has to contain the RecyclerView. There is no ScrollView or a specific layout needed.
Define the data model
You could use any class or primitive data type as a model, like int, String, float or CustomObject. The RecyclerView will refer to a List of this objects / primitives.
When a list item refers to different data types like text, numbers, images (as in this example with places), it is often a good idea to use a custom object.
List item layout
You have to specify a xml layout file that will be used for each list item. In this example, an ImageView is used for the image and a TextView for the name. The LinearLayout positions the ImageView at the left and the TextView right to the image.
Create a RecyclerView adapter and ViewHolder
Next, you have to inherit the RecyclerView.Adapter and the RecyclerView.ViewHolder. A usual class structure would be:
First, we implement the ViewHolder. It only inherits the default constructor and saves the needed views into some fields:
The adapter's constructor sets the used dataset:
To use our custom list item layout, we override the method onCreateViewHolder(...). In this example, the layout file is called place_list_item.xml.
In the onBindViewHolder(...), we actually set the views' contents. We get the used model by finding it in the List at the given position and then set image and name on the ViewHolder's views.
We also need to implement getItemCount(), which simply return the List's size.
(Generate random data)
For this example, we'll generate some random places.
Connect the RecyclerView with the PlaceListAdapter and the dataset
Connecting a RecyclerView with an adapter is very easy. You have to set the LinearLayoutManager as layout manager to achieve the list layout.
Create your RecyclerView in your layout xml file:
Create your Model class for holding your data:
Create a layout file to hold RecyclerView items:
Create the adapter class for the RecyclerView:
Instantiate the RecyclerView in your activity or fragment:
Don't forgot to add the Picasso dependency in your build.gradle file:
This modified text is an extract of the original Stack Overflow Documentation created by following contributors and released under CC BY-SA 3.0