[英]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 :
有两个要点:
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.