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

查看:113
本文介绍了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 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天全站免登陆