Android's ListView performance optimization
If you have seen the 15k interview questions that I have written, you must know that the ListView is optimized in the following ways:
- Reuse of Convertview
- Viewholder
- Static Class Viewholder
- In the case of a picture in the list, the monitor slide does not load the picture
The above is roughly said below, to meet the interview is enough, if you want to use the project, some beginners may be confused. Next I'll explain in detail how this is optimized. Reuse of Convertviewgetview the number of times this method will be called is your data entry number, reused the Convertview, greatly reducing the memory consumption. By judging whether Convertview is null, it is necessary to generate a view, then give the view data, and finally return the view to the bottom, presented to the user.
features : If the current convertview is null, a view is generated through Layoutinflat.
Viewholder
There is a drawback to the above notation, which is that each time you getview, you need to re-findviewbyid, re-locate the control, and then assign the control and set the event accordingly. This is actually doing the duplication of things, because the geiview, in fact, contains these controls, and the IDs of these controls are the same, that is, as long as the view in Findviewbyid once, there is no need to findviewbyid each time.
Here's a second way to write
The characteristics of writing, usually have an internal class classviewholder, this viewholder, used to identify some of the controls in the view, to facilitate the operation of some events corresponding settings, such as onclick, etc. This can not be findviewbyid every time, reducing the consumption of performance. At the same time, the Convertview is reused, which greatly reduces the memory consumption.
public View getView (int position,view convertview,view groupparent) {viewholderholder; if (convertview==null) {convertview=layoutinflater.from (context). Inflate (R.layout.section_list_item1,null); Holder=newviewholder (); Holder.tv_name= (TextView) Convertview.findviewbyid (r.id.contact_contactinfoitem_tv_name); Holder.tv_phone= (TextView) Convertview.findviewbyid (r.id.contact_contactinfoitem_tv_phonenum); Convertview.settag (holder); } else {holder= (viewholder) Convertview.gettag ();} Contactinfo1confo=contacts.get (position); if (confo!=null) {//toseteveryitem ' Stext holder.tv_name.setText (Confo.getcontactname ()); Holder.tv_phone.setText ( Confo.getcontact_phone ()); } return Convertview; } classviewholder {TextView tv_name,tv_phone;}
Static class Viewholder Combine the above two, and then put viewholder as static, static, static class will only be used for the first time the load will take a long time, but the latter can be very good to help load, also ensures that there is only one viewholder in memory, saving memory overhead. we will find that whatever adapter is the way of these optimizations, So there's no way to think about it. There is no method to extract the part of the code reuse, if the idea of a friend can see me write this Adapter code optimization In the case of a picture in the list, the listener slide does not load the picture /span> another situation is that a list loads a lot of pictures, and when the picture has not been loaded down, the fast sliding list will find that the phenomenon of stalling occurs. in this case we need to listen, the ListView slide monitoring inside the settings do not load the picture, slide stop to start loading.
1, judging the ListView state abslistview.onscrolllistener Onscrolllistener = new Abslistview.onscrolllistener () {//listview// Touch event public void onscroll (Abslistview view, int firstvisibleitem,int visibleitemcount, int totalitemcount) {}public void O Nscrollstatechanged (abslistview view, int scrollstate) {switch (scrollstate) {case abslistview.onscrolllistener.scroll_state_fling://Slide state Threadflag = False;break;case abslistview.onscrolllistener.scroll_state_idle://Stop Threadflag = True;startthread (); break;case abslistview.onscrolllistener.scroll_state_touch_scroll://Touch Listviewthreadflag = false;break;default:// Toast.maketext (CONTEXTT, "Default",//Toast.length_short). Show (); break;}}; 2, get the current screen display items:mListView.getFirstVisiblePosition (); Mlistview.getlastvisibleposition ();
Ym--android's ListView performance optimization