Android ListView 每行具有不同的布局 [英] Android ListView with different layouts for each row

查看:38
本文介绍了Android ListView 每行具有不同的布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试确定拥有一个包含每行不同布局的单个 ListView 的最佳方法.我知道如何创建自定义行+自定义数组适配器来支持整个列表视图的自定义行,但是如何在 ListView 中实现许多不同的行样式?

解决方案

既然您知道您将拥有多少种布局类型 - 可以使用这些方法.

getViewTypeCount() - 此方法返回您的列表中有多少行类型的信息

getItemViewType(int position) - 根据位置返回您应该使用哪种布局类型的信息

然后仅当布局为空时才膨胀布局并使用 getItemViewType 确定类型.

查看本教程了解更多信息.>

为了实现您在评论中描述的结构优化,我建议:

希望能帮到你.如果您可以提供一些包含数据结构的 XML 存根以及您希望如何将其映射到行的信息,我将能够为您提供更准确的建议.按像素.

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() - this methods returns information how many types of rows do you have in your list

getItemViewType(int position) - returns information which layout type you should use based on position

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 call findViewById() every time in getView 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 每行具有不同的布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆