Home » Android » The optimization of ListView

The optimization of ListView


1. ListView needs to set adapter, and its item is obtained by the adapter method getView (int, position, View, convertView, parent, ViewGroup) .



In 2. ListView, only the item of the first screen needs to be new, and its reference will be in the RecycleBin object. When dragging, the following item is actually heavy from the item.
created earlier

According to the 3., ListView in item to display, the first screen, getView (int position, View convertView, ViewGroup parent) of the parameters for the second null, second display screen or rollback displays the first screen, getView (span < >int
Position, View, convertView, ViewGroup, parent
) the second parameter is a item of the original cache, and we just need to update its internal data in
getView,.


4. if the item structure is more complicated, when the item internal data update an existing, find item inside each element also need to occupy a lot of resources, therefore, can put these elements and reference cached, direct the assignment, the most effective way is to put these references to the corresponding item that is a good idea to use the setTag (.
) method





according to the last four, you can get:


1. in adapter getView (int position, View convertView, ViewGroup parent) method, every time a new new View return, is the worst performance, wasted a piece of ListView cleverly designed painstakingly.


2., if the item internal element is simple, in getView, if the incoming view is not empty, it can be assigned directly to.


3., if the item internal element is complex, you can use the method discussed in the Google IO conference to optimize (see below):

publicViewgetView (intposition, ViewconvertView, ViewGroupparent) {


ViewHolderholder;

If (convertView==null) {

ConvertView=mInflater.inflate (R.layout.list_item_icon_text, null);

Holder=newViewHolder ();

Holder.icon1= (ImageView) convertView.findViewById (R.id.icon1);

Holder.text1= (TextView) convertView.findViewById (R.id.text1);

ConvertView.setTag (holder);

}

Else{

Holder= (ViewHolder) convertView.getTag ();

}

Holder.icon1.setImageResource (R.drawable.icon);

Holder.text1.setText (mData[position]);

Returnholder;


}


staticclassViewHolder{

TextViewtext1;

ImageViewicon1;



}



Latest