ListView Adapter with multiple Item layouts

落爺英雄遲暮 提交于 2019-12-10 10:58:41

问题


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 have
  • getItemViewType(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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!