I couldn't find any better example for using RecyclerView
with StaggeredGridLayoutManager
. Not even in Android Docs.
Q1. I need some examples which can give proper explanation about how to use
RecyclerView
withStaggeredGridLayoutManager
.Q2. Can anyone tell me if it is possible to create following layout using
RecyclerView
withStaggeredGridLayoutManager
So far i have found this link which is not at all useful.
I also found this link for cardslib but it is too much complex in implementation and has too much dependencies which will increase my app size unnecessarily.
Assuming that you've already created an adapter and initialized the RecyclerView, the following code should do what you're looking for.
For further reference and documentation please check the following link: https://developer.android.com/reference/android/support/v7/widget/StaggeredGridLayoutManager.html
Our friends "Henry" have a good and simple explain here.
And I think below constructor is suitable for most uses :
For those who are still landing up on this question.
You could modify the following code as per your needs:
First add dependency libraries for Android RecyclerView and CardView
Your main activity layout activity_main.xml will simply be like
Define layout of a card in a layout file named book_list_item.xml
Define this layout as a class ItemObject.java
Define a custom adapter SampleRecyclerViewAdapter to populate the cards
We would also need a viewholder for each ItemObject. So define a class SampleViewHolders
Now in MainActivity, assign an instance of StaggeredGridLayoutManager to recycler_view to define how the components will appear.
Also populate the cards using instance of SampleRecyclerViewAdapter, as follows
Output will look something like this
For your requirement, you can incorporate an ImageView in book_list_item.xml and populate it accordingly in SampleViewHolders
Also note, to change number of columns from 2 to 3.
You could change declaration in MainActivity as
Which will give an output like this
Here's another simple tutorial