本文介绍了我应该使用convertView吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该通过以下方法使用convertView还是创建一个新的View?

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

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

推荐答案

从性能的角度来看,每次View都是错误的决定.常见的模式是仅在convertView为null时才使视图膨胀.因此,是的,请使用系统为您提供的视图.这样,您可以启用一些魔术优化,以免浪费内存(视图的回收)

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

这篇关于我应该使用convertView吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-25 03:33