Home Java javaTutorial Detailed introduction to several writing methods for android listview optimization

Detailed introduction to several writing methods for android listview optimization

Dec 13, 2016 pm 04:47 PM

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:

android listview优化

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; 
}
Copy after login

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; 
}
Copy after login

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; 
}
Copy after login

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; 
}
Copy after login

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.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Apr 19, 2025 pm 04:51 PM

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. ...

How to simplify field mapping issues in system docking using MapStruct? How to simplify field mapping issues in system docking using MapStruct? Apr 19, 2025 pm 06:21 PM

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...

How to elegantly obtain entity class variable names to build database query conditions? How to elegantly obtain entity class variable names to build database query conditions? Apr 19, 2025 pm 11:42 PM

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...

How do I convert names to numbers to implement sorting and maintain consistency in groups? How do I convert names to numbers to implement sorting and maintain consistency in groups? Apr 19, 2025 pm 11:30 PM

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

How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? Apr 19, 2025 pm 11:45 PM

Start Spring using IntelliJIDEAUltimate version...

How to safely convert Java objects to arrays? How to safely convert Java objects to arrays? Apr 19, 2025 pm 11:33 PM

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...

E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products? E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products? Apr 19, 2025 pm 11:27 PM

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...

How to elegantly get entity class variable name building query conditions when using TKMyBatis for database query? How to elegantly get entity class variable name building query conditions when using TKMyBatis for database query? Apr 19, 2025 pm 09:51 PM

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...

See all articles