Hello, ladies and gentlemen. Last time we talked about the ListView extension of UI controls in Android. This time we talked about the ListView optimization of UI controls. Don't mention gossip, turn right. Let's talk about Android!
Lookers, we all know that ListView is a control for displaying large amounts of data. If the amount of data is too large, the efficiency will be very low. Therefore, it is necessary to optimize ListView. Here's how to optimize ListView controls by combining code with text
public class DIYAdapter extends ArrayAdapter<DataType>{
private int itemId;
public DIYAdapter(Context context, int textViewId, List<DataType> dataTypeList){
super(context,textViewId,dataTypeList);
itemId = textViewId;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
DataType itemData = getItem(position);
ViewHolder viewHolder;
View view;
//View view = LayoutInflater.from(getContext()).inflate(itemId,parent,false);
if(convertView != null) {
view = convertView;
viewHolder = (ViewHolder)view.getTag();
}else {
view = LayoutInflater.from(getContext()).inflate(itemId,parent,false);
ImageView imageView = (ImageView)view.findViewById(R.id.list_item_image);
TextView textView = (TextView)view.findViewById(R.id.list_item_text);
viewHolder = new ViewHolder(imageView,textView);
view.setTag(viewHolder);
}
viewHolder.mImageView.setImageResource(itemData.getImageId());
viewHolder.mTextView.setText(itemData.getTitle());
// ImageView imageView = (ImageView)view.findViewById(R.id.list_item_image);
// TextView textView = (TextView)view.findViewById(R.id.list_item_text);
// imageView.setImageResource(itemData.getImageId());
// textView.setText(itemData.getTitle());
return view;
}
public class ViewHolder {
private ImageView mImageView;
private TextView mTextView;
public ViewHolder(ImageView img, TextView text){
mImageView = img;
mTextView = text;
}
}
}
Look at the officers, the optimization is mainly the adapter, you focus on the code annotated above, which is the optimized code.
The first optimization is about view.
view is obtained every time before optimization, and only when the layout is first loaded after optimization. So how do you determine the first loading layout? This depends on the second parameter of getView, which is empty the first time the layout is loaded, and then ListView buffers the loaded layout, passing the buffered layout to the second parameter every time the getView method is called.The second area of optimization is about the controls in the layout.
Before optimization, we need to get the control in the layout every time. After optimization, we only need to get the control in the layout when the layout is first loaded. How does this work? We use buffer technology, first we define the buffer class ViewHolder. The data members in the class need to correspond to the controls in the layout file. When we first load the layout, we initialize a buffer class object and store it in View using the view.setTag() method. In the future, you only need to use view.getTag() method to get the buffered object.
Officials, the optimization of ListView mainly uses the idea of buffer, which can greatly improve the efficiency of ListView operation. It may not reflect the benefits of optimization when the data volume is small, but it can be reflected when the data volume is large. Therefore, I hope you can optimize when using ListView.
Ladies and gentlemen, here's an example of ListView optimization for UI controls in Android. For further examples, listen to the next decomposition!