Last gave me the code for testing: Not see please first browse previous: http://www.bkjia.com/kf/201201/116681.html
Now let's verify the magic of listView. The main thing that listview needs to optimize is the getView () method, which implements the convertView Cache Optimization, the difference between using listview to reuse convertView in TraceView is as follows:
First, do nothing:
[Html] public View getView (int position, View convertView, ViewGroup parent ){
// TODO Auto-generated method stub
LinearLayout layout = (LinearLayout) inflater. inflate (R. layout. listview, null );
TextView text = (TextView) layout. findViewById (R. id. text );
ImageView view = (ImageView) layout. findViewById (R. id. iamge );
Text. setText (listData. get (position ));
Int id = position % 2 = 1? R. drawable. icon: R. drawable. default_head;
View. setImageResource (id );
Return layout;
}
Public View getView (int position, View convertView, ViewGroup parent ){
// TODO Auto-generated method stub
LinearLayout layout = (LinearLayout) inflater. inflate (R. layout. listview, null );
TextView text = (TextView) layout. findViewById (R. id. text );
ImageView view = (ImageView) layout. findViewById (R. id. iamge );
Text. setText (listData. get (position ));
Int id = position % 2 = 1? R. drawable. icon: R. drawable. default_head;
View. setImageResource (id );
Return layout;
}
Run the program, drag the listview list at will, and then run the menu key to exit the program: Go to fileExplorer in ddms and click the SD card. You will see the dmtrace. trace file in the early root directory.
Export dmtrace. trace to drive C, and run traceview C: \ dmtrace. trace in the android tools Folder to display the traceview.
The getview optimization operation is displayed at the gate. Therefore, if you directly type getView in "Find:", the method of the program we wrote will be found:
No: If no optimization is performed, getView occupies 35.2% of the resources. inflate occupies 89.7% of the total resources. inflated occupies 33% of the total projects, and getView () occupies of the resources () the method is to be completely filled by layout, which consumes so many resources and cannot be viewed.
Optimization 1
Add two lines of code directly:
[Java] if (convertView = null ){
Layout = (LinearLayout) inflater. inflate (R. layout. listview, null );
} Else {
Layout = (LinearLayout) convertView;
}
If (convertView = null ){
Layout = (LinearLayout) inflater. inflate (R. layout. listview, null );
} Else {
Layout = (LinearLayout) convertView;
}
At run, view getview:
I can't see it. I don't see: 9.4% accounts for 9.4% of the entire program, and inflated only consumes 41.7% in getview, saving more than half!
The two lines of code bring about such a huge increase in efficiency: Don't you realize it! Magic
Optimization 2
The following is an online event: ViewHolder optimization test passed setTAG
[Java] public View getView (int position, View convertView, ViewGroup parent ){
// TODO Auto-generated method stub
// LinearLayout layout;
// If (convertView = null ){
// Layout = (LinearLayout) inflater. inflate (R. layout. listview, null );
//} Else {
// Layout = (LinearLayout) convertView;
//}
//
// TextView text = (TextView) layout. findViewById (R. id. text );
// ImageView view = (ImageView) layout. findViewById (R. id. iamge );
//
// Text. setText (listData. get (position ));
// Int id = position % 2 = 1? R. drawable. icon: R. drawable. default_head;
// View. setImageResource (id );
ViewHolder holder;
If (convertView = null ){
ConvertView = inflater. inflate (R. layout. listview,
Null );
Holder = new ViewHolder ();
Holder. view = (ImageView) convertView. findViewById (R. id. iamge );
Holder. text = (TextView) convertView. findViewById (R. id. text );
ConvertView. setTag (holder );
}
Else {
Holder = (ViewHolder) convertView. getTag ();
}
Int id = position % 2 = 1? R. drawable. icon: R. drawable. default_head;
Holder. view. setImageResource (id );
Holder. text. setText (listData. get (position ));
Return convertView;
}
Static class ViewHolder {
TextView text;
ImageView view;
}
Public View getView (int position, View convertView, ViewGroup parent ){
// TODO Auto-generated method stub
// LinearLayout layout;
// If (convertView = null ){
// Layout = (LinearLayout) inflater. inflate (R. layout. listview, null );
//} Else {
// Layout = (LinearLayout) convertView;
//}
//
// TextView text = (TextView) layout. findViewById (R. id. text );
// ImageView view = (ImageView) layout. findViewById (R. id. iamge );
//
// Text. setText (listData. get (position ));
// Int id = position % 2 = 1? R. drawable. icon: R. drawable. default_head;
// View. setImageResource (id );
ViewHolder holder;
If (convertView = null ){
ConvertView = inflater. inflate (R. layout. listview,
Null );
Holder = new ViewHolder ();
Holder. view = (ImageView) convertView. findViewById (R. id. iamge );
Holder. text = (TextView) convertView. findViewById (R. id. text );
ConvertView. setTag (holder );
}
Else {
Holder = (ViewHolder) convertView. getTag ();
}
Int id = position % 2 = 1? R. drawable. icon: R. drawable. default_head;
Holder. view. setImageResource (id );
Holder. text. setText (listData. get (position ));
Return convertView;
}
Static class ViewHolder {
TextView text;
ImageView view;
}
The test result is better than the optimization: The findviewbyID is much less than the findviewbyTag.
Optimization 3
If (convertView = null ){
ConvertView = inflater. inflate (R. layout. listview, null );
ConvertView. setTag (R. id. text, convertView. findViewById (R. id. text ));
ConvertView. setTag (R. id. iamge, convertView. findViewById (R. id. iamge ));
}
(TextView) convertView. getTag (R. id. text). setText (listData. get (position ));
Int id = position % 2 = 1? R. drawable. icon: R. drawable. default_head;
(ImageView) convertView. getTag (R. id. iamge). setImageResource (id );
As you can see. The final result is similar to optimization 1.
Maybe the results are not very accurate, but at least we know how to use traceView to help us, and also know the optimization of listview. The specific optimization needs to be determined,
For recommendation with high performance requirements, optimization 2 is sufficient for general application optimization, and the use of settag and findbyid has been tested and proved:
View itself occupies more memory and increases the amount of code because of setTag. findViewById temporarily consumes more memory, so it cannot be used blindly, depending on the actual situation.
Hope to help you!
From the column of android interests