androidandroid-layoutandroid-recyclerviewrecyclerview-layoutstaggeredgridlayoutmanager

How do I implement staggered views in reading order?


Here is what I want to implement:

Staggered Views

I'm trying to display definitions I'm getting from the web (right now they're coming from dummy data, but that's neither here nor there).

My first thought was to use a horizontal StaggeredGridView. There are two problems with this. The views are not laid out in the correct order and the span count is not set dynamically. Also, I found out that calling setSpanCount() in onLayoutChildren() causes an error at run time.

Staggered View Attempt

Of course, I could just have a LinearLayout and forego my attempt to recycle views. That would work in most cases, but if the user did happen upon a length definition, it would be very memory inefficient.

With that in mind, I resolved to subclass StaggeredGridLayoutManager. And this is where I need your help. Here are the resources I found in my own search:

Google's Implementation of a LinearLayoutManager

A Guide Implementing a Custom LayoutManager

Another Guide for Implementing a Custom LayoutManager

An Overview of a Few Principles to Follow when Implementing a Custom LayoutManager

I went through each one of these, but I cannot mentally separate out what parts are there for their specific use-cases and what parts are common to all custom implementations. If someone could be so kind as to point me in the direction of a resource or explain what all the methods I have to call in what order and for what purpose, I would be immensely grateful. Thank you!


Solution

  • Your first screenshot looks like a perfect case for Google's FlexboxLayout library. You can use the FlexboxLayoutManager with RecyclerView to get exactly what you showed.