Android ListView with different layouts for each row(每行具有不同布局的Android ListView)
问题描述
我正在尝试确定拥有单个 ListView 的最佳方式,该 ListView 包含每行的不同布局.我知道如何创建自定义行 + 自定义数组适配器来支持整个列表视图的自定义行,但是如何在 ListView 中实现许多不同的行样式?
I am trying to determine the best way to have a single ListView that contains different layouts for each row. I know how to create a custom row + custom array adapter to support a custom row for the entire list view, but how can I implement many different row styles in the ListView?
推荐答案
既然你知道你会有多少种布局类型 - 就可以使用这些方法.
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 position)
- 根据位置返回您应该使用哪种布局类型的信息
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
的对象中.它会提高速度,因为您不必每次都在getView
方法中调用findViewById()
.请参阅 API 演示中的 List14一个>. - 创建一个通用布局,该布局将符合所有属性组合并在当前位置没有元素时隐藏一些元素.
- 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 存根以及您希望如何将其映射到行中的信息,我将能够为您提供更准确的建议.按像素.
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:每行具有不同布局的Android ListView


- Android - 拆分 Drawable 2022-01-01
- Android viewpager检测滑动超出范围 2022-01-01
- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01
- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
- 想使用ViewPager,无法识别android.support.*? 2022-01-01
- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01
- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01
- android 4中的android RadioButton问题 2022-01-01
- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01
- 用 Swift 实现 UITextFieldDelegate 2022-01-01