Android interview in the ListView

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 logic sparingly in the GetView method in adapter 2. Avoid GC to the fullest extent 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. Use ViewHolder1. Use less logic in the GetView method in adapter do not write too much logic code in your GetView (), we can put the code elsewhere, for example:


Public View GetView (int position, view Convertview, ViewGroup parent) {View item = minflater.inflate (r.layout.list_item_i Con_text, NULL);((TextView) Item.findviewbyid (R.id.text)). SetText (Data[position]);((ImageView) Item.findviewbyid ( R.id.icon). Setimagebitmap ((position & 1) = = 1? micon1:micon2); return item;}
What do you think? If there are more than 1000000 items, the consequences are unimaginable! You mustn't write like that!
Public View GetView (int position, View Convertview, ViewGroup parent) {if (Convertview = = null) {Convertview = Minflater.i Nflate (R.layout.item, null);} ((TextView) Convertview.findviewbyid (R.id.text)). SetText (Data[position]);((ImageView) Convertview.findviewbyid ( R.id.icon). Setimagebitmap ((position & 1) = = 1 Micon1:micon2); return convertview;}
Then the above code can also be optimized, the answer is yes.
Public View GetView (int position, View Convertview, ViewGroup parent) {Viewholder holder;if (Convertview = = null) {Convert View = minflater.inflate (R.layout.list_item_icon_text, null); holder = new Viewholder (); holder.text = (TextView) Convertview.findviewbyid (r.id.text); Holder.icon = (ImageView) Convertview.findviewbyid (R.id.icon); Convertview.settag (holder);} else {holder = (Viewholder) Convertview.gettag ();} Holder.text.setText (Data[position]); Holder.icon.setImageBitmap ((position & 1) = = 1? micon1:micon2); return Convertview;}  Static class Viewholder {TextView text;imageview icon;}

2.GC garbage Collector When you create a large number of objects, the GC will be executed frequently, so do not create a lot of objects in the GetView () method, the best optimization is, do not create any objects outside of Viewholder, if your log inside found "GC has Freed some memory "if there are frequent occurrences, then your program must have a problem. You can check: a) is the level of the item layout too deep B) GetView () method has a large number of objects present in C) Layout properties of the ListView 3. Loading pictures If you need to display images downloaded from the network in your ListView, 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 , stop loading the picture, or start loading the picture if it is not sliding

Listview.setonscrolllistener (New Onscrolllistener () {
TODO auto-generated Method stub}});

4. Setting the Scrollingcache and Animatecache of the ListView to Falsescrollingcache:scrollingcache is essentially the drawing cache, You can have a view save his own drawing in the cache (save it as a bitmap) so that the next time you display the view you don't have to redraw it, but take it out of 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:listview default on Animatecache, which consumes a lot of memory, so the GC is called frequently, and we can turn it off manually, so our code can write:
<listviewandroid: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 "/>

5. Reduce the depth of the layout of the item 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 will waste a lot of time, so we should remove some unnecessary layout nesting relationships. Reduce the item layout depth, this I have done an experiment, when the layout nested more than 5 layers of time, Android Decodview will appear error, so please do not nest too deep.
6. Use Viewholder This everyone should be very familiar with, but do not underestimate this viewholder, it can greatly improve the performance of our ListView ListView optimization we have finished.
Above is the most basic, if you have a better solution to add, welcome to the back of the message. Or join our official QQ Group: 278792776













Android interview in the ListView

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.