BaseAdapter optimization


Introduction to this section:

In the previous section we learned how to use a ListView and customize a simple BaseAdapter. We start with the code We can see the two more important methods: getCount() and getView(). getView will be called as many times as there are columns on the interface. You may see some clues at this time. Every time a View is newly inflated, the XML must be parsed. This will It is a waste of resources. Of course, a list with dozens or hundreds of columns does not reflect any problems, but what if there are more or the layout is more complicated? Therefore, it is very important to learn the optimization of ListView, and this section is aimed at the optimization of BaseAdapter. The two optimization points are: reuse convertView And use ViewHolder to reuse components without having to find ViewById every time. Let’s experience it through the code!


1. Reuse ConvertView:

As mentioned above, how many Items are on the interface, then how many times will the getView method be called! Let’s take a look at the getView() code we wrote in the previous section:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = LayoutInflater.from(mContext).inflate(R.layout.item_list_animal,parent,false);
ImageView img_icon = (ImageView) convertView.findViewById(R.id.img_icon);
TextView txt_aName = ( TextView) convertView.findViewById(R.id.txt_aName);
TextView txt_aSpeak = (TextView) convertView.findViewById(R.id.txt_aSpeak);

img_icon.setBackgroundResource(mData.get(position). getaIcon());
txt_aName.setText(mData.get(position).getaName());
txt_aSpeak.setText(mData.get(position).getaSpeak());
return convertView;
}

Yes, inflate() has to load xml every time. In fact, this convertView is a View provided by the system for us to take. cache object, then sit down and make a judgment. Modify the optimized code:

##@Override
public View getView(int position, View convertView, ViewGroup parent) {

if(convertView == null){
convertView = LayoutInflater.from(mContext).inflate(R.layout.item_list_animal,parent,false);
}

ImageView img_icon = (ImageView) convertView.findViewById(R.id.img_icon);
TextView txt_aName = (TextView) convertView.findViewById(R.id.txt_aName);
TextView txt_aSpeak = (TextView) convertView.findViewById(R. id.txt_aSpeak);

img_icon.setBackgroundResource(mData.get(position).getaIcon());
txt_aName.setText(mData.get(position).getaName());
txt_aSpeak .setText(mData.get(position).getaSpeak());
return convertView;
}

2. ViewHolder reuse component

Hey, getView() will be called multiple times, so findViewById will have to be called multiple times, and the Item of our ListView Generally the layout is the same. We can optimize this part by defining a ViewHolder class ourselves. Perform performance optimization! The modified code is as follows:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if( convertView == null){
convertView = LayoutInflater.from(mContext).inflate(R.layout.item_list_animal,parent,false);
holder = new ViewHolder();
holder.img_icon = (ImageView ) convertView.findViewById(R.id.img_icon);
holder.txt_aName = (TextView) convertView.findViewById(R.id.txt_aName);
holder.txt_aSpeak = (TextView) convertView.findViewById(R.id .txt_aSpeak);
ConvertView.setTag(holder); //Storage Holder into convertView
}else{
holder = (ViewHolder) convertView.getTag();
}
holder .img_icon.setBackgroundResource(mData.get(position).getaIcon());
holder.txt_aName.setText(mData.get(position).getaName());
holder.txt_aSpeak.setText(mData.get (position).getaSpeak());
return convertView;
}

static class ViewHolder{
ImageView img_icon;
TextView txt_aName;
TextView txt_aSpeak;
}

Yes, it’s that simple. You can just write BaseAdapter according to this template in the future, haha, and this one modifies ViewHolder. static, whether it is defined as static has nothing to do with the number of objects in it. Static is added to use this in multiple places. When using Holder, the class only needs to be loaded once. If it is only used once, it doesn’t matter whether it is added or not! ——Berial (B God) original words~


Summary of this section:

Okay, about the optimization of BaseAdapter The above two are very simple, reusing ConvertView and custom ViewHolder Reduce the number of calls to findViewById()~ If you have other suggestions for BaseAdapter optimization, please feel free to make them, thank you~