我应该使用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;
}
答案 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;
}