0

Should I use convertView or create a new View in the following method?

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v = inflater.inflate(R.layout.content, null);
    return v;
}

or

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    convertView = inflater.inflate(R.layout.content, null);
    return convertView;
}
stefana
  • 2,606
  • 3
  • 29
  • 47

1 Answers1

2

Inflating every time the View is a bad decision from the performance perspective. The common pattern is to inflate your view only if the convertView is null. So, yes use the view the system is providing you. This way you can enable some magic optmization that allows to do not waste memory (view's recycle)

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    convertView = inflater.inflate(R.layout.content, null);
    }
    return convertView;
}
Blackbelt
  • 156,034
  • 29
  • 297
  • 305