[Android Advanced advanced] tips for improving ListView performance

Source: Internet
Author: User

ListView optimization has always been a commonplace problem, whether in the interview or the usual development, the ListView will never be ignored, then this article we will look at how to maximize the performance of the ListView optimization.
    • 1. Use as little logic as possible in the GetView method in adapter
    • 2. Avoid GC as best as possible
    • 3. Do not load the picture when sliding
    • 4. Set the Scrollingcache and Animatecache of the ListView to False
    • The 5.item layout level is burning better
    • 6. Using Viewholder
1. Use as little logic as possible in the GetView method in adapter

Don't write too much logic code in your GetView (), we can put this code somewhere else, for example:

Pre-Optimization GetView ():

@Override PublicViewGetView(intPosition, View Convertview, ViewGroup paramviewgroup) {Object current_event = mobjects.get (position); Viewholder holder =NULL;if(Convertview = =NULL) {holder =NewViewholder (); Convertview = Inflater.inflate (R.layout.row_event,NULL); Holder.                Threedimension = (ImageView) Convertview.findviewbyid (R.id.threedim); Holder.                Eventposter = (ImageView) Convertview.findviewbyid (r.id.eventposter);        Convertview.settag (holder); }Else{holder = (Viewholder) convertview.gettag (); }//Here to make a logical judgment, which is problematic        if(Doessomecomplexchecking ()) {Holder.        Threedimention.setvisibility (view.visible); }Else{Holder.         Threedimention.setvisibility (View.gone); }//This is the parameter that sets the image, which executes this code every time the GetView method executes, which is obviously problematicRelativelayout.layoutparams Imageparams =NewRelativelayout.layoutparams (Measuredwidth, rowHeight); Holder. Eventposter.setlayoutparams (Imageparams);returnConvertview;}

Optimized GetView ():

@Override PublicViewGetView(intPosition, View Convertview, ViewGroup paramviewgroup) {Object object = Mobjects.get (position); Viewholder holder =NULL;if(Convertview = =NULL) {holder =NewViewholder (); Convertview = Inflater.inflate (R.layout.row_event,NULL); Holder.            Threedimension = (ImageView) Convertview.findviewbyid (R.id.threedim); Holder. Eventposter = (ImageView) Convertview.findviewbyid (r.id.eventposter);//Set parameters mentioned here, only the first time will be executed, then will be reusedRelativelayout.layoutparams Imageparams =NewRelativelayout.layoutparams (Measuredwidth, rowHeight); Holder.            Eventposter.setlayoutparams (Imageparams);    Convertview.settag (holder); }Else{holder = (Viewholder) convertview.gettag (); }//We directly use the getter method of the object to replace those logical judgments, and those logical judgments are placed elsewhere to executeHolder. Threedimension.setvisibility (Object.getvisibility ());returnConvertview;}
2.GC garbage collector

When you create a large number of objects, the GC will be executed frequently, so in the GetView () method do not create a lot of objects, the best optimization is, do not create any objects outside of Viewholder, if your log inside found "GC has freed some Memory "occurs frequently, then your program must have a problem. You can check:
A) the level of the item layout is too deep
b) Is there a large number of objects in the GetView () method
c) Layout properties of the ListView

3. Loading pictures

If your ListView needs to display images downloaded from the network, we do not load the picture when the ListView is sliding, which will cause the ListView to become stuck, so we need to monitor the ListView state again, and if you swipe, stop loading the picture, If there is no swipe, start loading the picture

Listview.setonscrolllistener (NewOnscrolllistener () {@Override             Public void onscrollstatechanged(Abslistview ListView,intScrollstate) {//Stop loading pictures                    if(scrollstate = = AbsListView.OnScrollListener.SCROLL_STATE_FLING)                    {Imageloader.stopprocessingqueue (); }Else{//Start loading picturesImageloader.startprocessingqueue (); }            }@Override             Public void onscroll(Abslistview view,intFirstvisibleitem,intVisibleItemCount,intTotalitemcount) {//TODO auto-generated method stub}    });
4. Set the Scrollingcache and Animatecache of the ListView to False

Scrollingcache: Scrollingcache is essentially drawing cache, you can have a view save his own drawing in the cache (saved as a bitmap), This will not be redrawn the next time the view is displayed, but it is removed from the cache. By default, drawing Cahce is disabled because it consumes too much memory, but it does have a smoother picture. In the ListView, Scrollingcache is turned on by default, and we can turn it off manually.

Animatecache: The ListView turns on Animatecache by default, which consumes a lot of memory, so the GC is called frequently, and we can turn it off manually

The ListView before optimization

<ListView        android:id="@android:id/list"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:cacheColorHint="#00000000"        android:divider="@color/list_background_color"        android:dividerHeight="0dp"        android:listSelector="#00000000"        android:smoothScrollbar="true"        android:visibility="gone"

Optimized ListView

<ListView        android:id="@android:id/list"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:divider="@color/list_background_color"        android:dividerHeight="0dp"        android:listSelector="#00000000"        android:scrollingCache="false"        android:animationCache="false"        android:smoothScrollbar="true"        android:visibility="gone" />
5. Reduce the depth of the item's layout

We should try to reduce the item layout depth, because when we slide the ListView, this leads directly to the measurement and drawing, so it wastes a lot of time, so we should remove some unnecessary layout nesting relationships. Reduce Item Layout Depth

6. Using Viewholder

This people should be very familiar with, but do not underestimate this viewholder, it can greatly improve the performance of our ListView

ListView optimization We have already finished, if in your project, these basic optimization you have not yet done, then your ListView is problematic, there is a great potential for improvement, in the future when using the ListView, it must be taken into account, to play its maximum performance.

Copyright NOTICE: Welcome reprint, Reprint please indicate the source http://blog.csdn.net/nugongahou110

[Android Advanced advanced] tips for improving ListView performance

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.