Android's--listview optimization

Source: Internet
Author: User

Reprint Please specify source: http://blog.csdn.net/l1028386804/article/details/47209253

As a client, its most important task is the most intuitive and user interaction. From the server to take the data, after parsing display data, according to the user operation according to a certain protocol to return data, to achieve the user desired results. This is my own understanding, so our program must give the user a good experience.

The ListView can be said to be a very important control in Android development. Almost every page in my project will have a ListView. Adapter is the middleman between the ListView and the data source. When each piece of data enters the visible area, adapter's GetView () is called to return a view that represents the specific data. Frequently called when scrolling is touched. Supports hundreds of thousands of data. However, the ListView is also very complex, it is not easy to optimize, below is my own collation of the ListView optimization mode

1, the simplest method, the slowest and least practical

Public View getView (int pos, view Convertview, ViewGroup parent) {View item = minflater.inflate (R.layout.list_item, NULL); ((TextView) Item.findviewbyid (R.id.text)). SetText (Data[pos]); ((ImageView) Item.findviewbutid (R.id.icon)). Setimagebitmap (pos & 1) = = 1 Micon1:micon2); return item;}
We're going to parse the layout for each piece of data. The equivalent of every time you go to new object, write the first ListView when I write this, the function can be achieved naturally, but in fact very low efficiency we

2, can use Convertview recycling view, efficiency can increase 200%

Public View getView (int pos, View Convertview, ViewGroup parent) {if (Convertview = = null) {Convertview = Minflater.inflate (R.layout.list_item, null);} This place is equivalent to doing a caching mechanism. It is only when Convertview is empty that we parse the layout, because parsing the layout is actually cumbersome and time-consuming. We only parse the layout once, others we use the same cached layout ((TextView) Convertview.findviewbyid (R.id.text)). SetText (Data[pos]);((ImageView) Convertview.findviewbutid (R.id.icon)). Setimagebitmap ((pos & 1) = = 1? micon1:micon2); return convertview;}

3, the use of Viewholder mode, efficiency in the increase of 50%

Static class Viewholder {TextView text;imageview icon;}  Public View getView (int pos, View Convertview, ViewGroup parent) {Viewholder holder;if (Convertview = = null) {Convertview = Minflater.inflate (R.layout.list_item, null); holder = new Viewholder (); holder.text = (TextView) Convertview.findviewbyid (R.id.text)); Holder.icon = (ImageView) convertview.findviewbutid (R.id.icon)); Convertview.settag (holder);} Else{holder = (Viewholder) Convertview.gettag ();} Holder.text.setText (Data[pos]); Holder.icon.setImageBitmap ((pos & 1) = = 1 Micon1:micon2); return convertview;}
The above 3 points are commonly used in the ListView, and of course the most basic optimization method. If there are no special requirements, there are 3 optimizations that must exist for Android. There are some other optimization methods:

4. Backgrounds and images

The view background image always fills the entire view area
1) Inappropriate image size will cause automatic scaling
2) Avoid real-time scaling
3) It is best to pre-scale to view size

Originalimage = Bitmap.createscaledbitmap (Originalimage,    //scaled Image view.getwidth (),//  View width view.getheight (),/ /view height true); Linear filter
By default, the window has an opaque background
Sometimes you don't need
The top-level view is opaque layout_width = fill_parent
Top-level view covers the entire window Layout_height = fill_parent
Updating an unseen background is a waste of time

5. Delete Window background:

1) Modify the code

public void OnCreate (Bundle icicle) {super.oncreate (icicle); Setcontentview (R.layout.mainview);//Delete window background GetWindow (). Setbackgrounddrawable (null);.. }

6. Modify the XML

First make sure that your res/values/styles.xml has

Parent= "Android:theme" >
Then edit Androidmainfest.xml
Android:theme= "@style/nobackgroundtheme" > ...   

7. Update request

When the screen needs to be updated, it is simple and convenient to call the invalidate () method, but it is too expensive to update the entire view.
It is best to find the invalid zone first and then call

Invalidate (Rect dirty); invalidate (int left, int top, int. right, int bottom);

8. Views and layouts

If a window contains many views, it starts too slowly, draws longer, and the user interface reacts slowly
Workaround:
1) Use TextView compound drawable to reduce levels

Android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "@string/hello" Android: drawableleft= "@drawable/icon"/>
2) Expand view with Viewstuf delay

Defining VIEWSTUF in an XML file

Android:inflatedid= "@+id/panel_import" android:layout= "@layout/progress_overlay" android:layout_width= "Fill_ Parent "android:layout_height=" wrap_content "android:layout_gravity=" Bottom "/>
When you need to expand the view
Findviewbyid (R.id.stub_import). setvisibility (view.visible);//or View Importpanel = ((viewstub) Findviewbyid ( R.id.stub_import)). Inflate ();

9. Using the Merge Intermediate view

By default, the root of the layout file is added to the parent view as a node, and if you use merge, you can avoid the root node

"Http://schemas.android.com/apk/res/android" >

10, reduce the Android container layout nesting

Android layout nesting is actually time-taken when parsing, so we try to avoid nesting as many layers as possible based on the ability to implement functionality. When you write a layout, you get used to the same code as we do when we write Java code and try to optimize as much as possible.

11. Using Custom Views

Class CustomView extends View {@Overrideprotected void OnDraw (canvas canvas) {//Add your drawing encoding} @Overrideprotected void Onmeasu Re (int widthmeasurespec,int heightmeasurespec) {//Calculate view size setmeasureddimension (widthspecsize, heightspecsize);}}

12. Memory allocation: Try to avoid creating Java objects in performance-sensitive code

Measurement onmeasure () layout onlayout () Drawing OnDraw () Dispatchdraw () event handling ontouchevent () dispatchtouchevent () Adapter:getview () BindView ()

13, the management of good objects:

1) applicable soft reference: Best choice for Memory cache
2) Apply weak reference: Avoid memory leaks

14. Memory Cache:

Private final hashmap> mcache;public void put (String key, T value) {Mcache.put (Key, new SoftReference (value));} Public T get (String key, Valuebuilder builder) {T value = null; Softreferece reference = Mcache.get (key), if (reference! = NULL) {value = Reference.get ();}    return value;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android's--listview optimization

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.