We created a new library, AdapterLayout!
If you would take a quick look at AdapterLayout, you would probably wonder why anyone would ever create added complication around the simple act of adding views to a layout. The problem really came about when using FlowLayout, a great library by Blaž Šolar which allows you to add views to a ViewGroup and they intelligently break to a new line when needed.
Android implementation of FlowLayout. Layout arranges its children in multiple rows depending on their width.github.com
FlowLayout is a great library, but if you use it for some amount of time, you will run into a scenario where you are taking a data set, looping through each item, constructing a view, then adding it to the root FlowLayout. Why does this sound familiar? This is exactly the purpose of an Adapter. In the most basic sense, it maps models to views within a ViewGroup. And that is exactly the reason we created AdapterLayout. It is really not so much about the layout, but more the delegate, AdapterLayoutDelegate, which allows you to take any ViewGroup, and make it behave similar to the way RecyclerView behaves. All you have to do is create an adapter, set the adapter on the AdapterLayout, then add the data to that adapter and views are added to the ViewGroup.
//CheeseAdapter is a RecyclerView adapter
mCheeseAdapter = new CheeseAdapter();
adapterFlowLayout adapterFlowLayout = (AdapterFlowLayout) findViewById(R.id.adapter_flow_layout);
// Do other view setup...
// Make a network call to load the cheeses
// Now we are in a callback, having successfully loaded the cheeses
The FlowLayout will adapt to the changing dataset and show the right views by using the onCreateViewHolder and onBindViewHolder methods of the adapter. This should feel very familiar if you have ever used RecyclerView, ListView, or anything else where you add a collection of items to a ViewGroup. See the sample app for further usage details.
I Still Don’t Get It
If you still don’t see why this is useful, think about when you are creating a complicated screen which requires use of a ScrollView and then you suddenly have to support a dynamic data set midway within the page. This can be a real challenge, since RecyclerView and ScrollView do not get along very well. You could change your screen to use RecyclerView with headers and footers, but sometimes that is just not needed. AdapterLayout allows you to insert this data using AdapterLinearLayout with its orientation set to horizontal or vertical, all within an existing layout.
When NOT to use
AdapterLayout is NOT designed to be a replacement for RecyclerView. In fact, 9 times out of 10 RecyclerView is the right way to go. AdapterLayout solves a pretty specific problem of needing to bind data to a ViewGroup, without the need for recycling. Therefore, if you find your data exceeding a just a few items, you should really be using RecyclerView, since AdapterLayout does no recycling whatsoever.
Pull Requests Welcome
Right now AdapterLayout out of the box only supports LinearLayout. In the sample, we show how to create AdapterFlowLayout, which is very useful, but the AdapterLayout library should not depend on FlowLayout since not all users are interested in that use case. That being said, there may be other examples of AdapterLayouts that would be useful to others to showcase, or even add some to the library itself. Let us know what you think!