问题
I'm looking at adding a listview to my Android application with a thumbnail image. The thumbnail on the left and text on the right.
Is there any way of populating the list view with seperate xml files, for example, could I have separate xmls for Austria, UK, USA etc and add them to the listview?
The reason I ask is, I plan to reuse the individual countries multiple times, as in, shown in an A to Z but then again by Continent etc.
If this is possible, could I get an example of how this is done or a link with a tutorial etc?
Thanks
回答1:
Do you really want separate .xml files? Or do you want to reuse one .xml file and just replace its content?
If you want separate .xml files (meaning multiple different Views in your Adapter) take a look at the Adapters methods:
getViewTypeCount()
- returns how many different view layouts you havegetItemViewType(int pos)
- returns correct view type id by position
With these methods you can specify the number of different types in your adapter, and load different layout files depending on the type.
You could for example have an Adapter like this supporting multiple layouts. (e.g. one item being an actual item, the other one being a separator between the items)
For more specific information, please take a look at this great tutorial:
http://android.amberfog.com/?p=296
来源:https://stackoverflow.com/questions/18413015/listview-adapter-with-multiple-item-layouts