Android的列表视图不同的布局每行 [英] Android Listview with different layout for each row
问题描述
我想确定有一个列表视图的最佳方式包含不同的行样式。我知道如何创建一个自定义行+自定义阵列适配器以支持自定义行整个列表视图。但怎么看的ListView支持许多不同的行样式?
I am trying to determine the best way to have a single listview contains different rows styles. I know how to create a custom row + custom array adapter to support a custom row for the entire list view. But how can one listview support many different row styles?
推荐答案
既然你知道你有多少种布局有 - 有可能使用这些方法
Since you know how many types of layout you would have - it's possible to use those methods.
getViewTypeCount()
- 这个方法返回多少种排,你有你的列表信息。
getViewTypeCount()
- this methods returns information how many types of rows do you have in your list
getItemViewType(INT位置)
- 返回其布局类型,您应该使用基于位置信息
getItemViewType(int position)
- returns information which layout type you should use based on position
那你吹大的布局,只有当它是无效,并确定型使用 getItemViewType
。
Then you inflate layout only if it's null and determine type using getItemViewType
.
看 本教程 了解更多信息。
Look at this tutorial for further information.
要实现结构优化的一些您在评论所描述我建议:
To achieve some optimizations in structure that you've described in comment I would suggest:
- 在对象存储的看法名为
ViewHolder
。这将提高速度,因为不再需要调用findViewById()
在getView
方法每次。请参阅<一href="http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/List14.html">List14在API演示的。 - 创建一个通用的布局,将符合特性的所有组合和隐藏一些元素,如果当前位置没有了。
- Storing views in object called
ViewHolder
. It would increase speed because you won't have to callfindViewById()
every time ingetView
method. See List14 in API demos. - Create one generic layout that will conform all combinations of properties and hide some elements if current position doesn't have it.
我希望这会帮助你。如果你能提供一些XML存根与您的数据结构,你想究竟如何将其映射到行的信息,我就可以给你更多的precise提醒。通过像素。
I hope that will help you. If you could provide some XML stub with your data structure and information how exactly you want to map it into row, I would be able to give you more precise advise. By pixel.
这篇关于Android的列表视图不同的布局每行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!