The ListView optimization has always been a commonplace issue. Whether it's an interview or an ordinary development, the ListView will never be ignored, so this article looks at how to maximize the performance of the ListView.
- 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 the code somewhere else. Like what:
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 (); }//Make logical inference here. This is a problem. if(Doessomecomplexchecking ()) {Holder. Threedimention.setvisibility (view.visible); }Else{Holder. Threedimention.setvisibility (View.gone); }//This is the set of parameters for the image, which is run every time the GetView method runs. This is obviously a problem.Relativelayout.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);//settings reference here, only the first time will be run, then will be reusedRelativelayout.layoutparams Imageparams =NewRelativelayout.layoutparams (Measuredwidth, rowHeight); Holder. Eventposter.setlayoutparams (Imageparams); Convertview.settag (holder); }Else{holder = (Viewholder) convertview.gettag (); }//We replace those logical inferences directly with the getter method of the object. Those logical inferences were put somewhere else to run.Holder. Threedimension.setvisibility (Object.getvisibility ());returnConvertview;}
2.GC garbage collector
When you create a large number of objects. The GC will run frequently. Therefore, do not create very many objects in the GetView () method. The best optimization is to not create any objects outside of Viewholder. Suppose your log found "GC has freed some memory" to appear frequently. Then there must be something wrong with your program.
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 images
Let's say your ListView needs to display images downloaded from the web. 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. If you are sliding, stop loading the picture, assuming that there is no slide, 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, and 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. Since it consumes too much memory, it does have a much smoother picture.
In the ListView, Scrollingcache is turned on by default, and we are able to turn it off manually.
Animatecache: The ListView turns on Animatecache by default, which consumes a lot of memory, so it calls the GC frequently, and we can shut it down 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 sliding the ListView, this directly leads to measurement and drawing, so it will waste a lot of time. So we should get rid of some unnecessary layout nesting relationships. Lower 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
We have finished the optimization of the ListView. Assume that you are in your project. These basic optimizations you haven't done yet. Then your listview is problematic, and there is a great potential for improvement. When you use the ListView later, be sure to take this into account and play its maximum performance.
Android performance optimization tips for improving ListView performance