简体   繁体   English

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

[英]Best way to create a custom view for every list item

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. 当我审查和制作我的android项目原型时,我注意到有一个视图具有非常定制的项目列表。 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. 我在想我必须使用此getView来创建每个视图。 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? 但是在创建每个视图时,最好是1)动态创建视图并添加图像(例如,如果图像存在的话),或2)创建包含所有可能元素的xml文件并根据项目隐藏它们?

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. 就性能而言,#2更好,因为您将能够将ListView给出的convertView作为getView()参数重用到适配器。 To allow smooth ListView scrolling, you have to avoid any View creation during the getView() call. 为了允许ListView平滑滚动,您必须避免在getView()调用期间创建任何View。

You can find an efficient implementation of getView() in this sample from the Developer Guide . 您可以从《开发人员指南》中的此示例中找到getView()的高效实现。 There are 2 important points : 有两个要点:

  • reuse the convertView when available 在可用时重用convertView
  • use a ViewHolder attached to the View to avoid having to find again your sub-views in each call to getView() 使用附加到View的ViewHolder避免每次调用getView()时都不必再次查找子视图

I'd go with #2. 我会选择#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. 您可以使用所有项目进行xml布局,然后编写列表适配器,在其中可以根据需要隐藏和显示项目。 That way, if you need to change he layout you can just tweak the xml. 这样,如果您需要更改布局,则只需调整xml。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从Java中的项目列表创建项目列表的最佳方法是什么? - What is the best way to create a item list from items list in Java? 为每个列表项创建具有costum背景的列表 - Create a list with a costum background for every list item 按项目名称长度排序列表的最佳方法 - The best way to sort list by item name length 创建自定义方法安全表达式的最佳方法 - Best way to create custom method security expression 在java中创建邻接列表的最佳方法? - Best Way to create Adjacency List in java? 如何为列表中的每个项目添加文本视图? - How can I add a text view for every item in my list? 是否可以选择具有列表项ID的自定义列表视图项,而不是在Android中选择列表项的位置? - Is it possible to select a custom list view item with its list item id instead of list item position in android? 如何在列表视图中的选定列表项目上设置自定义默认铃声? - How to set custom default ringtone on the selected list item in list View? Android,用Retrofit填充Endless List View数据的最佳方法? - Android, Best way to populate Endless List View's data with Retrofit? 在 MVC 程序的视图中将类型转换为列表的最佳方法是什么? - What is the best way to cast a type to a List in the View of an MVC program?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM