Android Performance Optimization Save View lookups with the ViewHolder pattern

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Example

Especially in a ListView, you can run into performance problems by doing too many findViewById() calls during scrolling. By using the ViewHolder pattern, you can save these lookups and improve your ListView performance.

If your list item contains a single TextView, create a ViewHolder class to store the instance:

static class ViewHolder {
    TextView myTextView;
}

While creating your list item, attach a ViewHolder object to the list item:

public View getView(int position, View convertView, ViewGroup parent) {
    Item i = getItem(position);
    if(convertView == null) {
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_item, parent, false);

        // Create a new ViewHolder and save the TextView instance
        ViewHolder holder = new ViewHolder();
        holder.myTextView = (TextView)convertView.findViewById(R.id.my_text_view);
        convertView.setTag(holder);
    }

    // Retrieve the ViewHolder and use the TextView
    ViewHolder holder = (ViewHolder)convertView.getTag();
    holder.myTextView.setText(i.getText());
   
    return convertView;
}

Using this pattern, findViewById() will only be called when a new View is being created and the ListView can recycle your views much more efficiently.



Got any Android Question?