


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.

JVM works by converting Java code into machine code and managing resources. 1) Class loading: Load the .class file into memory. 2) Runtime data area: manage memory area. 3) Execution engine: interpret or compile execution bytecode. 4) Local method interface: interact with the operating system through JNI.

JVM enables Java to run across platforms. 1) JVM loads, validates and executes bytecode. 2) JVM's work includes class loading, bytecode verification, interpretation execution and memory management. 3) JVM supports advanced features such as dynamic class loading and reflection.

Java applications can run on different operating systems through the following steps: 1) Use File or Paths class to process file paths; 2) Set and obtain environment variables through System.getenv(); 3) Use Maven or Gradle to manage dependencies and test. Java's cross-platform capabilities rely on the JVM's abstraction layer, but still require manual handling of certain operating system-specific features.

Java requires specific configuration and tuning on different platforms. 1) Adjust JVM parameters, such as -Xms and -Xmx to set the heap size. 2) Choose the appropriate garbage collection strategy, such as ParallelGC or G1GC. 3) Configure the Native library to adapt to different platforms. These measures can enable Java applications to perform best in various environments.

OSGi,ApacheCommonsLang,JNA,andJVMoptionsareeffectiveforhandlingplatform-specificchallengesinJava.1)OSGimanagesdependenciesandisolatescomponents.2)ApacheCommonsLangprovidesutilityfunctions.3)JNAallowscallingnativecode.4)JVMoptionstweakapplicationbehav

JVMmanagesgarbagecollectionacrossplatformseffectivelybyusingagenerationalapproachandadaptingtoOSandhardwaredifferences.ItemploysvariouscollectorslikeSerial,Parallel,CMS,andG1,eachsuitedfordifferentscenarios.Performancecanbetunedwithflagslike-XX:NewRa

Java code can run on different operating systems without modification, because Java's "write once, run everywhere" philosophy is implemented by Java virtual machine (JVM). As the intermediary between the compiled Java bytecode and the operating system, the JVM translates the bytecode into specific machine instructions to ensure that the program can run independently on any platform with JVM installed.

The compilation and execution of Java programs achieve platform independence through bytecode and JVM. 1) Write Java source code and compile it into bytecode. 2) Use JVM to execute bytecode on any platform to ensure the code runs across platforms.


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

SublimeText3 Linux new version
SublimeText3 Linux latest version

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

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Chinese version
Chinese version, very easy to use

Dreamweaver CS6
Visual web development tools
