We all knowAndroidMediumAdapterThe role isListviewThe bridge between the interface and data. When each item in the list is displayed on the pageAdapterOfGetviewMethod returnsView. Ever wondered? In our list, we have1000000 itemsWhat will happen? Does it occupy a lot of system resources?
Let's take a look at the following code:
Java code:
- Copy to clipboardJava code
- Public View getview (INT position, view convertview, viewgroup parent ){
- View Item = minflater. Inflate (R. layout. list_item_icon_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;
- }
You don't need to tell me what's going on. If there are more than 1000000 items, the consequences are unimaginable! You must never write this!
Let's take a look at the following code:
Java code:
Copy to clipboardJava code
- Public View getview (INT position, view convertview, viewgroup parent ){
- If (convertview = NULL ){
- Convertview = minflater. Inflate (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;
- }
How is the above Code much better? The system will reduce the numberView. The performance has been greatly improved.
Are there any optimization methods? The answer is yes: Let's take a look at the following code!
Java code:
Copy to clipboardJava code
- Public View getview (INT position, view convertview, viewgroup parent ){
- Viewholder holder;
- If (convertview = NULL ){
- Convertview = 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;
- }