问题描述
我正在尝试确定拥有一个包含每行不同布局的单个 ListView 的最佳方法.我知道如何创建自定义行+自定义数组适配器来支持整个列表视图的自定义行,但是如何在 ListView 中实现许多不同的行样式?
既然您知道您将拥有多少种布局类型 - 可以使用这些方法.
getViewTypeCount()
- 此方法返回您的列表中有多少行类型的信息
getItemViewType(int position)
- 根据位置返回您应该使用哪种布局类型的信息
然后仅当布局为空时才膨胀布局并使用 getItemViewType
确定类型.
查看本教程了解更多信息.>
为了实现您在评论中描述的结构优化,我建议:
- 在名为
ViewHolder
的对象中存储视图.它会提高速度,因为您不必每次在getView
方法中都调用findViewById()
.请参阅 API 演示中的 List14. - 创建一个通用布局,该布局将符合所有属性组合,并在当前位置没有时隐藏一些元素.
希望能帮到你.如果您可以提供一些包含数据结构的 XML 存根以及您希望如何将其映射到行的信息,我将能够为您提供更准确的建议.按像素.
Since you know how many types of layout you would have - it's possible to use those methods.
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:
- 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.
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 ListView 每行具有不同的布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!