【问题标题】:Inflating custom View in ListView according to position根据位置膨胀 ListView 中的自定义视图
【发布时间】:2013-04-19 06:53:24
【问题描述】:

我有一个ListView,我想在某个位置为自定义View 充气。它实际上是第 11 项(位置 10)。

getCount() 方法返回 11。

这是getView()方法的顶部:

public View getView(final int position, View convertView, ViewGroup parent) {
    final ViewHolder holder;
    if (convertView == null) {              
        if (position < 10) {
            convertView = mInflater.inflate(R.layout.standard, null);           
        } else {
            convertView = mInflater.inflate(R.layout.custom, null);
            Log.i(TAG, "Inflated custom layout");
        }
        Log.i(TAG, "getView, position = "+position);
        holder = new ViewHolder();
        holder.tv = (TextView) convertView.findViewById(R.id.tv);
        holder.radioButton = (RadioButton) convertView.findViewById(R.id.radioButton1);             
        convertView.setTag(holder);             
    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    ...
    Log.i(TAG, "getView() : position = " + position);
}

这是我在 logcat 中看到的:

getView, position = 0
getView, position = 1
getView, position = 2

其余的 getView 日志不显示;我假设这条线会一直出现到getView, position = 10

这行:convertView = mInflater.inflate(R.layout.custom, null); 永远不会被调用,因为Inflated custom layout 不会出现在 logcat 中。

这很有趣,因为底部的 logcat 总是被调用(当我向下滚动 ListView 时):

getView() : position = 0
getView() : position = 1
getView() : position = 2
getView() : position = 3
getView() : position = 4
getView() : position = 5
getView() : position = 6
getView() : position = 7
getView() : position = 8
getView() : position = 9
getView() : position = 10

为什么我的自定义布局没有膨胀?

【问题讨论】:

    标签: java android android-listview android-custom-view android-inflate


    【解决方案1】:

    getView() 方法View convertView 不为空时,您需要扩展布局。从getView() 返回的View 将被重用。如果您检查视图是否为空并膨胀,它只会在第一次返回 View 时膨胀。

    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView == null) {              
            if (position < 10) {
                convertView = mInflater.inflate(R.layout.standard, null);           
            }
            Log.i(TAG, "getView, position = "+position);
            holder = new ViewHolder();
            holder.tv = (TextView) convertView.findViewById(R.id.tv);
            holder.radioButton = (RadioButton) convertView.findViewById(R.id.radioButton1);             
            convertView.setTag(holder);             
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        ...
    
        if(position > 10) {
             convertView = mInflater.inflate(R.layout.custom, null);
             Log.i(TAG, "Inflated custom layout");
        }
    
        Log.i(TAG, "getView() : position = " + position);
    }
    

    【讨论】:

    • 但是在这种情况下,每次当位置超过 10 时,自定义视图都会被夸大,对吗?假设如果 getView 方法调用 100 次,位置为 11,那么将创建该视图的 100 个实例..
    • 是的,这是要求。每次列表项超出可见视图时,列表视图都会调用 Runtime.gc()。
    【解决方案2】:

    convertView == null 之外扩展自定义布局。因为,convertView 通常会返回一个以前回收的视图,因此您的自定义视图永远不会膨胀。

    【讨论】:

      猜你喜欢
      • 2012-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-10
      • 1970-01-01
      相关资源
      最近更新 更多