| Start |
|---|
| 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. Source: Chiu-Ki Chan's Square Island blog (opens new window) |
| GridLayoutManager with dynamic span count 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. Sources: |
| Simple list with LinearLayoutManager This example adds a list of places with image and name by using an Activity layoutThe 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 modelYou could use any class or primitive data type as a model, like 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 layoutYou have to specify a xml layout file that will be used for each list item. In this example, an Create a RecyclerView adapter and ViewHolderNext, you have to inherit the First, we implement the The adapter's constructor sets the used dataset: To use our custom list item layout, we override the method In the We also need to implement (Generate random data)For this example, we'll generate some random places. Connect the RecyclerView with the PlaceListAdapter and the datasetConnecting a Done! |
StaggeredGridLayoutManager
Don't forgot to add the Picasso dependency in your build.gradle file: |
Read Article
https://develop.horje.com/learn/1434/reference