I have an app that I am trying to work out. In one layout of the app, I have multiple ListView components. The outline looks like this:
<ScrollView>
<RelativeLayout>
<TextView />
<TextView />
<ListView />
<ListView />
<ListView />
<Button />
</RelativeLayout>
</ScrollView>
The three separate ListView components are listing 3 different types of items. My problem comes in, that when encapsulated withing the ScrollView, the ListViews decide to only display 1 item each, and then fall to their scroll behavior. What I prefer is for them to show all their items, and let the ScrollView do the scrolling. Is this possible?
I’ve read a few other questions on here, and it seems maybe the convention is to NOT use multiple ListView’s per layout. I would rather make this work if possible, as the 3 separate items listed are related, and make sense to be shown together.
The solution I ended up going with was a Sectioned List Adapter, used to populate a single ListView control in my layout. The Sectioned Adapter causes a ListView to closely resemble a Preferences List. But the Sectioned Adapter is more versatile because you can customize the section seperator items, and include multiple list item layouts. Here’s the breakdown on how to achieve this, assuming you already know the basics of Mono for Android.
First you need a Section Object, which will describe each individual list section.
This object stores all the information for each section of the list, a caption which will be the title for the section, as well as a column header for each of the 3 columns I want my list to have. Additionally we store a unique list adapter that will provide the Views for this section of the list. This allows you to provide a different adapter for each section. You could extend this section object to further describe the separator section if you wanted, giving you more flexibility, and the chance to change what each section’s basic structure looks like.
Next you need an XML template to describe the separator for the list. Since each of my sections will have the same basic structure, I can recycle the same template each time rather than complicate it further.
You’ll notice, the Inner LinearLayout control I gave a style=”?android:attr/listSeparatorTextViewStyle” tag. This tells android to give that view a bottom border. If you just wanted a simple TextView separator, you could do this, and just give it this same style tag.
Now my ListAdapters are all basically the same, they just extend a different data object. All three populate 3 columns, all of which fit their data in the same size columns, they are just different logical objects. Each adapter is a BaseAdapter extension that populates a view with the 3 columns of data. I’ve made the assumption here that you know how to create a standard extension of BaseAdapter. What I will show is how to create the ListSectionAdapter.
Now all you have to do is in your code, when you are populating the Activity or Layout containing the ListView, create your separate adapters, then create your sectioned adapter and add a section for each separate list type you want in it.
For the ItemClick event, there may be a better way to do this, but I used the following method, which compares the ToString of the returned Object Types from the GetItem(int) method of the sectioned list, which we’ve extended to return the base list adapter object type.
private void MyList_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
{
ListSectionAdapter adapter = (sender as ListView).Adapter as ListSectionAdapter;
if(adapter.GetItem(e.Position).ToString() == typeof(ObjectA).ToString())
{
// respond accordingly to object type A being clicked
}
// so on and so forth on each different object type contained in the sectioned list
}
My click event just populates and opens a new Layout describing the item clicked. The differentiation of object type is necessary because I use a different layout based on the object type clicked, because static information on the layout differs per object type.
Hope this helps. I gleaned this example from Wrox’s book, Professional Android Programming with Mono C#/.Net, and modified it to meet my needs, hopefully you can see how it works so you can modify it to meet your own needs.