In some situations, it is better to use a ViewGroup
to create lists, acting as something similar to a ListView
, or RecyclerView
.
For example, I have a situation where I need to display a list of items in a ViewGroup
which is a child of the root layout. The root layout is scrollable (e.g. a ScrollView
or NestedScrollView
) so it would be inappropriate to use another scrolling View
to display list items.
An example of this could be displaying list items in a CardView
(of course in this case, you would not want too many list items as Cards are not meant for this). It would clearly not be right to use a scrolling layout inside your Cards, especially if the Cards are part of a scrolling root layout.
Another example could be creating a Navigation Drawer (of course after Google I/O 2015, there is the Design Support Library, so using a NavigationView
would be far simpler and easier). But before the Design Support Library, you had to populate a ViewGroup
(likely a LinearLayout
with items to display in the Navigation drawer, as the root layout of the drawer had to be scrollable. For more information about this scenario, I asked a similar question.
The point of the question is that there is no simple way to use a ViewGroup
as a list layout, so many of us tend to use complicated solutions to disable scrolling on a ListView
or RecyclerView
. Yes, the scrolling on the ListView
can be disabled, but there is usually still a Lint warning. Therefore, using ViewGroup
lists can be also thought of as an alternate solution to disabling scrolling on scroll View
s.
So is there a simple way to use ViewGroup
s as lists?
Inspired by how Google populated list items in a LinearLayout
in a Navigation Drawer, in their 2014 Google I/O app, I created a Gist that can populate list items in a ViewGroup
.
The Gist, titled ViewGroupAdapter
, can be found here.
I will continue to update and improve the Gist, if this solution becomes popular.