Android: optimization of ListView, androidlistview

Source: Internet
Author: User

Android: optimization of ListView, androidlistview

First version:

The ListView screen displays the number of objects in which the View object is created. The cached objects that exit when sliding are left to the convertView passed by getView when sliding. Because if you use findViewById to find and create a view object every time, performance and memory are wasted. Therefore, we use the layout to create a View for convertView. In the layout, view objects are saved to a set object-ViewHolder: view holder by using findViewById. Put the ViewHolder object on the Tag of convertView and return it to the Adapter through getView. When the next screen is displayed, the cached View object will be passed in. In this case, the Tag attribute of convertView contains the object inside the ViewHoler object-convertView layout. In this way, the object in ViewHoler is used for processing.
The general usage of this version is as follows:

@ Overridepublic View getView (int position, View convertView, ViewGroup parent) {ViewHoler holer; if (convertView = null) {// convertView = View. inflate (mActivity, R. layout. list _ **, null); holer = new ViewHoler (); holer. iv_New_Image = (ImageView) convertView. findViewById (R. id. iv_New_Image); holer. TV _Content = (TextView) convertView. findViewById (R. id. TV _Content); holer. TV _Date = (TextView) convertView. findViewById (R. id. TV _Date); convertView. setTag (holer);} else {holer = (ViewHoler) convertView. getTag () ;}// enter the complete return convertView;} class ViewHoler {public ImageView iv_New_Image; public TextView TV _Content; public TextView TV _Date ;}View Code

Because I am lazy, this findViewById is too troublesome, so I wrote a tool myself. Based on the automatically generated code. Custom ViewHoler code generation (the current page is only played once, view ctrl + f5 again ). Tool download link: http://pan.baidu.com/s/1i4tXwvr

 


Version 2:
This version only changes the design of the first version, and the Code related to ViewHoler is concentrated on ViewHoler. GetView has clearer responsibilities and logic.

@ Overridepublic View getView (int position, View convertView, ViewGroup parent) {ViewHoler holer; if (convertView = null) {// convertView = View. inflate (mActivity, R. layout. list _ **, null); holer = new ViewHoler ();} else {holer = (ViewHoler) convertView. getTag () ;}// enter the complete return convertView;} class ViewHoler {public ImageView iv_New_Image; public TextView TV _Content; public TextView TV _Date; public ViewHoler (View convertView) {this. iv_New_Image = (ImageView) convertView. findViewById (R. id. iv_New_Image); this. TV _Content = (TextView) convertView. findViewById (R. id. TV _Content); this. TV _Date = (TextView) convertView. findViewById (R. id. TV _Date); convertView. setTag (this );}}View Code

The tool has updated the http://pan.baidu.com/s/1bnVNbPl on this version

 

Version 3:
This version is essentially different from the previous version. The previous version is static code. In this ViewHoler, you want to obtain the View based on the resource Id. However, our View exists in a HashMap, because HashMap has O (1) time complexity for searching, and does not worry about repetition because it is a KeyValue. If the returned View type is changed to generic, the upper layer does not need to be converted to a strong level when obtaining the object.

// Usage ViewHolerHelper helper = new ViewHolerHelper (convertView); ImageView view = helper. getView (R. id. iv_New_Image); public class ViewHolerHelper {private HashMap <Integer, View> mViews; View convertView; public ViewHolerHelper (View convertView) {mViews = new HashMap <Integer, View> (); convertView. setTag (this); this. convertView = convertView;} public <T> T getView (Integer R_Id) {View view = null; if (! MViews. containsKey (R_Id) {view = convertView. findViewById (R_Id); if (view! = Null) {mViews. put (R_Id, view) ;}} else {view = mViews. get (R_Id) ;}return (T) view ;}}View Code

 

Related Article

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.