我应该使用convertView吗?

时间:2013-12-12 10:21:06

标签: android listview

我应该使用convertView还是在以下方法中创建一个新的视图?

@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;
}

@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;
}

1 个答案:

答案 0 :(得分:2)

每次View从绩效角度来看都是错误的决定。常见的模式是仅在convertView为空时才会使视图膨胀。所以,是的,请使用系统为您提供的视图。通过这种方式,您可以启用一些不浪费内存的魔术优化(查看循环)

@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;
}