为每个列表项创建自定义视图的最佳方法

发布于 2024-09-30 09:17:13 字数 470 浏览 0 评论 0原文

当我审查和原型化我的 Android 项目时,我注意到有一个视图,其中包含非常定制的项目列表。我已经弄清楚了我计划使用的大部分工具,但我需要一些建议。

我有一个项目列表,其中可以包含照片、右侧的一些文本、底部的图像以及下面的更多文本。我知道非常令人困惑。其中每一个都可能出现在一个项目中。唯一永远存在的就是标题。

所以我的问题 - 创建每个自定义列表项视图的最佳方法是什么?我想我必须使用这个 getView 创建每个视图。但是,在创建每个视图时,最好是 1) 动态创建视图并添加图像(例如,如果存在),或者 2) 创建包含所有可能元素的 xml 文件并根据项目隐藏它们?

As I am reviewing and prototyping my android project, I noticed that there is a view that has a list of items that are very customized. I have figured out most of my tools that I plan to use but I need some advice.

I have a list of items which can contain photos, some text to the right, an image to the bottom and more text below that. Very confusing I know. Each of these might be present for an item. The only thing that will always be there is the title.

So my question - what is the best way to create each custom list item view? I am thinking I have to use this getView to create each view. But when creating each view, is it best to 1) create a view dynamically and adding an image, for example, if it exists, or 2) create an xml file with all possible elements and hide them depending on the item?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

海之角 2024-10-07 09:17:13

就性能而言,#2 更好,因为您将能够将 ListView 提供给适配器的 ConvertView 作为 getView() 参数重用。为了允许 ListView 平滑滚动,您必须避免在 getView() 调用期间创建任何视图。

您可以在 开发人员指南中的示例。有两点很重要:

  • 在可用时重用convertView,
  • 使用附加到View的ViewHolder,以避免在每次调用getView()时再次找到子视图

In terms of performance #2 is better because you will be able to reuse the convertView given by the ListView to your Adapter as a getView() argument. To allow smooth ListView scrolling, you have to avoid any View creation during the getView() call.

You can find an efficient implementation of getView() in this sample from the Developer Guide. There are 2 important points :

  • reuse the convertView when available
  • use a ViewHolder attached to the View to avoid having to find again your sub-views in each call to getView()
暮年慕年 2024-10-07 09:17:13

我会选择#2。您可以创建一个包含所有项目的 xml 布局,然后编写一个列表适配器,您可以在其中根据需要隐藏和显示项目。这样,如果您需要更改布局,只需调整 xml 即可。

I'd go with #2. You can make an xml layout with all of your items present and then write a list adapter where you can hide and show items as needed. That way, if you need to change he layout you can just tweak the xml.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文