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.开发者_JAVA百科 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?
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()
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.
精彩评论