In-depth understanding of ListView: Performance Optimization 1. in-depth understanding of listview

Source: Internet
Author: User

In-depth understanding of ListView: Performance Optimization 1. in-depth understanding of listview
One list shows three elements:

1. ListView: displays the view of the list;

2. Adapter: map data to ListView;

3. Data: The specific string, image, or other basic components to be mapped;


2. Optimization Principle: 1. Create only necessary view objects;

How to load data with ListView:

Call the getCount () function to obtain the length of the listView Based on the returned value;

Then, based on the length, call getView () to obtain the view corresponding to each item in the listView.

If every item creates a new object, it will occupy too much memory.

2. Minimize Layout inflate operations;

The Infalte operation will instantiate the corresponding View using the xml file, which is an IO operation and time-consuming operation.

3. Reduce the findViewById () operation. You can use the findViewById () method to find a view in an xml file. This method recursion the entire view tree to find the view that matches the ID.

When items contain complex la S, the frequent callback findViewById () method will undoubtedly affect the sliding performance.


Three optimization mechanisms: 1. Recycler (implementation principle 1 and principle 2)

When the listView item leaves the current view, the corresponding view will be cached in the Recycler.

At this time, an item is generated from the bottom, and the convertView parameter in the getView () function called is the cached view.

Therefore, the key to optimizing listView performance is to reuse convertView.




2. viewHolder static class (implementation principle 3) encapsulates the elements in the view xml into a ViewHolder static class, and binds the view with the corresponding holder object through the setTag of convertView. Avoid a large number of findViewById () operations.

TagsUnlike IDs, tags are not used to identify views. Tags are essentially an extra piece of information that can be associated with a view.                   They are most often used as a convenience to store data related to views in the views themselves rather than by putting them in a separate structure.

(Use tags to associate additional view information)


4. Optimization process: 1. Initial writing
@Overridepublic View getView(int position, View convertView, ViewGroup parent) {View view = LayoutInflater.from(mContext).inflate(R.layout.listview_item, null);ImageView imageView1 = (ImageView)view.findViewById(R.id.imageView1);TextView textView2 = (TextView)view.findViewById(R.id.text2);imageView1.setImageResource(R.drawable.ic_launcher);textView2.setText(mData.get(position));return view;}
(Problems: 1. Too many views are created; 2. Too many inflate operations; 3. Too many times of calling findViewById)


2. Reuse convertView
@Overridepublic View getView(int position, View convertView, ViewGroup parent) {if(convertView == null){convertView = LayoutInflater.from(mContext).inflate(R.layout.listview_item, null);}ImageView imageView1 = (ImageView)convertView.findViewById(R.id.imageView1);TextView textView2 = (TextView)convertView.findViewById(R.id.text2);imageView1.setImageResource(R.drawable.ic_launcher);textView2.setText(mData.get(position));return convertView;}
Use the Recycler mechanism to successfully solve problem 1 and problem 2;


3. Use ViewHolder static class
static class ViewHolder{ImageView imageView1;TextView textView1;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {ViewHolder holder;if(convertView == null){holder = new ViewHolder();convertView = LayoutInflater.from(mContext).inflate(R.layout.listview_item, null);holder.imageView1 = (ImageView)convertView.findViewById(R.id.imageView1);holder.textView1 = (TextView)convertView.findViewById(R.id.text1);convertView.setTag(holder);}else{holder = (ViewHolder)convertView.getTag();}holder.imageView1.setImageResource(R.drawable.ic_launcher);holder.textView1.setText(mData.get(position));return convertView;}
The viewHolder static class is used to successfully solve problem 3;

(It is recommended to write ViewHolder as a static class because the static internal class defined by static is relatively independent and cannot access the methods and entities of the class, consuming less resources. If static is removed, mutual entity access is allowed, which wastes some resources .)


4. Use the dependency injection framework to streamline code:
static class ViewHolder{@InjectView(R.id.imageView1)ImageView imageView1;@InjectView (R.id.text1)TextView textView1;}
@Overridepublic View getView(int position, View convertView, ViewGroup parent) {ViewHolder holder;if(convertView == null){holder = new ViewHolder();convertView = LayoutInflater.from(mContext).inflate(R.layout.listview_item, null);convertView.setTag(holder);}else{holder = (ViewHolder)convertView.getTag();}holder.imageView1.setImageResource(R.drawable.ic_launcher);holder.textView1.setText(mData.get(position));return convertView;}
The dependency injection framework helps you streamline application code. Less code means less problems or bugs, so you can spend more energy on the part of the project that needs to be written or modified, making it easier to read the code.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.