Android-Optimizing UI Performance (2)-Improving the efficiency of adapter

Source: Internet
Author: User

android-Optimizing UI Performance (2)-Improving the efficiency of adapter
an increase in the efficiency of adapter
Adapter is the bridge between the data and the ListView, and whenever you need to display the item, the GetView () will be called.
If we have a large amount of data and adapter is inefficient (each time we call inflate to create a new view,inflate (an IO operation), the interface will be

For example, using the following code to GetView () is inefficient when the amount of data is large:

Public View getView (int p_position, view p_contentview, ViewGroup p_parent) {View _item = Minflate. Inflate(R. Layout. List_item_icon_text, NULL);((TextView) _item. Findviewbyid(R. ID. Text)). SetText(Data[p_position]);((ImageView) _item. Findviewbyid(R. ID. Icon)). Setimagebitmap((P_position &1) ==1? micon1:micon2); Return _item; }

1. Reuse the item View that has been generated
Use the following code effect to get a better boost

 PublicViewGetView(intP_position, View P_contentview, ViewGroup p_parent) {//Avoid loading view every time, only the first time you create a view        if(P_contentview = =NULL) {P_contentview = Minflate.inflate (R.layout.list_item_icon_text,NULL); }//The view is already loaded and the data is directly available        Else if(P_contentview! =NULL{View _item = Minflate.inflate (R.layout.list_item_icon_text,NULL);            ((TextView) _item.findviewbyid (R.id.text)). SetText (Data[p_position]); ((ImageView) _item.findviewbyid (R.id.icon)). Setimagebitmap (P_position &1) ==1? micon1:micon2);return_item; }return_item; }

2, add Viewholder,
Avoid repeatedly finding the controls that need to be modified, because using Findviewbyid is a time-consuming operation (traversing nodes), we can use Viewholder to buffer,
This is useful for the amount of data on item or for complex layout of item
The sample code is as follows:

 PublicViewGetView(intP_position, View P_contentview, ViewGroup p_parent) {Viewholder _viewholder;//Set up view for the first time, outgoing a holder altogether back loaded        if(NULL= = P_contentview) {//Load Component ViewP_contentview = Minflate.inflate (R.layout.list_item_icon_text,NULL); _viewholder =NewViewholder ();//Load Components_viewholder.mtext = ((TextView) P_contentview.findviewbyid (R.id.text)); _viewholder.micon = ((ImageView) P_contentview.findviewbyid (R.id.icon));//Set tagP_contentview.settag (_viewholder); }Else{//Get tag_viewholder = (Viewholder) p_contentview.gettag (); }//Set data values_viewholder.mtext.settext (Data[p_position]); _viewholder.micon.setimagebitmap (P_position &1) ==1? Micon1:micon2);//Return        returnP_contentview; }//buffer class defined as static     Public Static classViewholder {TextView mText;    ImageView Micon; }

3, cache the item's data
Buffering the data in the item takes a long time and can be cached in Viewholder
The sample code is as follows:

 PublicViewGetView(intP_position, View P_contentview, ViewGroup p_parent) {Viewholder _viewholder;//Set up view for the first time, outgoing a holder altogether back loaded        if(NULL= = P_contentview) {//Load Component ViewP_contentview = Minflate.inflate (R.layout.list_item_icon_text,NULL); _viewholder =NewViewholder ();//Load Components_viewholder.mtext = ((TextView) P_contentview.findviewbyid (R.id.text)); _viewholder.micon = ((ImageView) P_contentview.findviewbyid (R.id.icon));//Set tag_viewholder.mdata = Data[p_position]; _viewholder.mbitmap = (P_position &1) ==1?             Micon1:micon2;         P_contentview.settag (_viewholder); }Else{//Get tag_viewholder = (Viewholder) p_contentview.gettag (); }//Set data values_viewholder.mtext.settext (Data_viewholder.mdata); _viewholder.micon.setimagebitmap (_VIEWHOLDER.MBITMAP);//Return        returnP_contentview; }//Buffer class definition for four properties, you need to buffer as many as you want to define the number of     Public Static classViewholder {TextView mText;        ImageView Micon;        Data Mdata;    Bitmap Mbitmap; }

4, segmented display
When loading a lot of data, you can consider segmented display.

Copyright NOTICE: Welcome to communicate the error of the article, must be open-minded to answer, QQ872785786

Android-Optimizing UI Performance (2)-Improving the efficiency of adapter

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.