


Detailed introduction to several writing methods for android listview optimization
listview
A view that showssitemsinaverticallyscrollinglist.
A list view that displays a vertical scrolling child item. In android development, listview is used in many places. It is used to display data into a vertical view. Using listview is a standard adapter mode, using data--, interface--xml and adapter--adapter. The data is displayed by the adapter in the required way. XML describes how the data is displayed, and these activities are controlled in the activity.
If you use a custom adapter, you will need to override the getView method, and generate the view and data for the user item in the getView method.
See the picture:
There is an optimization here, which is to reuse views, which reduces memory consumption and speeds up item loading.
Everyone must be in trouble when it comes to optimizing in getView. Below I have summarized three ways of optimizing. Please correct me.
First:
Reuse convertView, which greatly reduces memory consumption. By judging whether convertView is null, if so, you need to generate a view, then give the view data, and finally return the view to the bottom layer and present it to the user.
Features: If the current convertView is null, a view will be generated through LayoutInflat.
ViewCode publicViewgetView(intposition,ViewconvertView,ViewGroupparent) { if(convertView==null) { convertView=LayoutInflater.from(context).inflate(R.layout.section_list_item1,null); } TextViewtv_name=(TextView)convertView.findViewById(R.id.contact_contactinfoitem_tv_name); TextViewtv_phone=(TextView)convertView.findViewById(R.id.contact_contactinfoitem_tv_phoneNum); ContactInfo1confo=contacts.get(position); if(confo!=null){//toseteveryitem'stext tv_name.setText(confo.getContactName()); tv_phone.setText(confo.getContact_Phone()); } returnconvertView; }
Second:
The above writing method has a disadvantage, that is, every time you getVIew, you need to findViewById again, find the control again, and then assign the value of the control and set the event accordingly. This is actually doing repetitive things, because geiview actually contains these controls, and the IDs of these controls are all the same. That is to say, you only need to find ViewById once in the view, and there is no need to find ViewById every time.
The second way of writing is given below.
The characteristics of writing are that there is usually an internal class classViewHolder. This ViewHolder is used to identify some controls in the view to facilitate the setting of corresponding operations for some events, such as onClick, etc. This eliminates the need for each You have to findViewById every time, which reduces performance consumption. At the same time, convertView is reused, which greatly reduces memory consumption.
ViewCode publicViewgetView(intposition,ViewconvertView,ViewGroupparent) { ViewHolderholder; if(convertView==null){ convertView=LayoutInflater.from(context).inflate(R.layout.section_list_item1,null); holder=newViewHolder(); holder.tv_name=(TextView)convertView.findViewById(R.id.contact_contactinfoitem_tv_name); holder.tv_phone=(TextView)convertView.findViewById(R.id.contact_contactinfoitem_tv_phoneNum); convertView.setTag(holder); } else { holder=(ViewHolder)convertView.getTag(); } ContactInfo1confo=contacts.get(position); Log.i("my","confo"+confo.getContactName()); if(confo!=null){//toseteveryitem'stext holder.tv_name.setText(confo.getContactName()); holder.tv_phone.setText(confo.getContact_Phone()); } returnconvertView; } classViewHolder { TextViewtv_name,tv_phone; }
Third:
Personally, I think this way of writing is the most comfortable. The most comfortable way of writing means that it’s very refreshing to look at the code and it’s very clear.
Features: Use internal class classViewHolder and reuse convertView.
The difference between the second way of writing is that it uses a temporary variable Viewview=convertView, then modifies the view, and finally returns to the view
ViewCode @Override publicViewgetView(intposition,ViewconvertView,ViewGroupparent) { Viewview=convertView; ViewHolderholder; if(view==null){ view=LayoutInflater.from(context).inflate(R.layout.section_list_item1,null); holder=newViewHolder(); holder.tv_name=(TextView)view.findViewById(R.id.contact_contactinfoitem_tv_name); holder.tv_phone=(TextView)view.findViewById(R.id.contact_contactinfoitem_tv_phoneNum); view.setTag(holder); } else { holder=(ViewHolder)view.getTag(); } ContactInfo1confo=contacts.get(position); Log.i("my","confo"+confo.getContactName()); if(confo!=null){//toseteveryitem'stext holder.tv_name.setText(confo.getContactName()); holder.tv_phone.setText(confo.getContact_Phone()); } returnview; } classViewHolder { TextViewtv_name,tv_phone; }
The above is a centralized writing method for novices to learn and summarize.
The source code is as follows: LisViewTest.zip
According to the suggestions provided by friends downstairs, I found that there are still areas for optimization. The latest update is as follows:
ViewCode @Override publicViewgetView(intposition,ViewconvertView,ViewGroupparent) { Viewview=convertView; ViewHolderholder; if(view==null){ view=LayoutInflater.from(context).inflate(R.layout.section_list_item1,null); holder=newViewHolder(); holder.tv_name=(TextView)view.findViewById(R.id.contact_contactinfoitem_tv_name); holder.tv_phone=(TextView)view.findViewById(R.id.contact_contactinfoitem_tv_phoneNum); view.setTag(holder); } else { holder=(ViewHolder)view.getTag(); } ContactInfo1confo=contacts.get(position); Log.i("my","confo"+confo.getContactName()); if(confo!=null){//toseteveryitem'stext holder.tv_name.setText(confo.getContactName()); holder.tv_phone.setText(confo.getContact_Phone()); } returnview; } <fontcolor="\"#0000ff\""></font>staticclassViewHolder { TextViewtv_name,tv_phone; }
Note: staticclassViewHolder
Set ViewHolder to static here, which is static. Static classes will only It will take a long time to load for the first time, but it can help with loading later. At the same time, it ensures that there is only one ViewHolder in the memory, saving memory overhead.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

Field mapping processing in system docking often encounters a difficult problem when performing system docking: how to effectively map the interface fields of system A...

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

Start Spring using IntelliJIDEAUltimate version...

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

When using TKMyBatis for database queries, how to gracefully get entity class variable names to build query conditions is a common problem. This article will pin...
