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

查看:31
本文介绍了每行具有不同布局的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:

  • 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.

希望对你有所帮助.如果您可以提供一些带有数据结构的 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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