【问题标题】:Understanding convertView了解转换视图
【发布时间】:2014-03-30 05:17:55
【问题描述】:

我正在尝试了解 convertView 的工作原理。我确实阅读了我遇到的大部分文档以及有关 StackOverflow 的问题。我以为我已经理解了它的工作原理,但是在实施时,我无法做到。

我现在的代码:

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    if(Const.DEBUGGING){
        Log.d(Const.DEBUG, "Position = "+position);
    }

    if (convertView == null) {

        if(Const.DEBUGGING){
            Log.d(Const.DEBUG, "convertView is NULL");
        }

        convertView = getActivity().getLayoutInflater().inflate(
                R.layout.item_mtf_results, parent, false);

        holder = new ViewHolder();
        holder.txtViewResults = (TextView) convertView
                .findViewById(R.id.textview_item_mtf_results);

        convertView.setTag(holder);
    } else {

        if(Const.DEBUGGING){
            Log.d(Const.DEBUG, "convertView is NOT Null");
        }

        holder = (ViewHolder) convertView.getTag();
    }

    holder.txtViewResults.setText(dummyText[position]);

    return convertView;
}

上面代码的我的 Logcat :

03-30 10:27:29.433: D/TYM(29043): Position = 0
03-30 10:27:29.433: D/TYM(29043): convertView is NULL
03-30 10:27:29.433: D/TYM(29043): Position = 1
03-30 10:27:29.433: D/TYM(29043): convertView is NOT Null
03-30 10:27:29.433: D/TYM(29043): Position = 2
03-30 10:27:29.433: D/TYM(29043): convertView is NOT Null
03-30 10:27:29.433: D/TYM(29043): Position = 3
03-30 10:27:29.433: D/TYM(29043): convertView is NOT Null
03-30 10:27:29.433: D/TYM(29043): Position = 4
03-30 10:27:29.433: D/TYM(29043): convertView is NOT Null
03-30 10:27:29.433: D/TYM(29043): Position = 5
03-30 10:27:29.433: D/TYM(29043): convertView is NOT Null
03-30 10:27:29.433: D/TYM(29043): Position = 6
03-30 10:27:29.433: D/TYM(29043): convertView is NOT Null
03-30 10:27:29.433: D/TYM(29043): Position = 7
03-30 10:27:29.433: D/TYM(29043): convertView is NOT Null
03-30 10:27:29.433: D/TYM(29043): Position = 8
03-30 10:27:29.433: D/TYM(29043): convertView is NOT Null
03-30 10:27:29.453: D/TYM(29043): Position = 0
03-30 10:27:29.453: D/TYM(29043): convertView is NOT Null
03-30 10:27:29.453: D/TYM(29043): Position = 1
03-30 10:27:29.453: D/TYM(29043): convertView is NULL
03-30 10:27:29.453: D/TYM(29043): Position = 2
03-30 10:27:29.453: D/TYM(29043): convertView is NULL
03-30 10:27:29.453: D/TYM(29043): Position = 3
03-30 10:27:29.453: D/TYM(29043): convertView is NULL
03-30 10:27:29.453: D/TYM(29043): Position = 4
03-30 10:27:29.453: D/TYM(29043): convertView is NULL
03-30 10:27:29.463: D/TYM(29043): Position = 5
03-30 10:27:29.463: D/TYM(29043): convertView is NULL
03-30 10:27:29.463: D/TYM(29043): Position = 6
03-30 10:27:29.463: D/TYM(29043): convertView is NULL
03-30 10:27:29.463: D/TYM(29043): Position = 7
03-30 10:27:29.463: D/TYM(29043): convertView is NULL
03-30 10:27:29.463: D/TYM(29043): Position = 8
03-30 10:27:29.463: D/TYM(29043): convertView is NULL
03-30 10:27:29.503: D/TYM(29043): Position = 0
03-30 10:27:29.503: D/TYM(29043): convertView is NULL
03-30 10:27:29.503: D/TYM(29043): Position = 1
03-30 10:27:29.503: D/TYM(29043): convertView is NOT Null
03-30 10:27:29.503: D/TYM(29043): Position = 2
03-30 10:27:29.503: D/TYM(29043): convertView is NOT Null
03-30 10:27:29.503: D/TYM(29043): Position = 3
03-30 10:27:29.503: D/TYM(29043): convertView is NOT Null
03-30 10:27:29.503: D/TYM(29043): Position = 4
03-30 10:27:29.503: D/TYM(29043): convertView is NOT Null
03-30 10:27:29.503: D/TYM(29043): Position = 5
03-30 10:27:29.503: D/TYM(29043): convertView is NOT Null
03-30 10:27:29.503: D/TYM(29043): Position = 6
03-30 10:27:29.503: D/TYM(29043): convertView is NOT Null
03-30 10:27:29.503: D/TYM(29043): Position = 7
03-30 10:27:29.503: D/TYM(29043): convertView is NOT Null
03-30 10:27:29.503: D/TYM(29043): Position = 8
03-30 10:27:29.503: D/TYM(29043): convertView is NOT Null

截图:

根据我对 convertView 的了解,当项目第一次加载到 ListView 中时,内存只分配给那些加载到屏幕中的视图,并为缓冲区分配了一些额外的内存。当我们开始滚动时,离开屏幕的视图被回收到现有视图中。

因此,当屏幕第一次加载时,convertView 将为 NULL,因此分配了内存。仅当我们开始滚动并且视图可用于回收时,convertView 才不会为 Null。是这样吗?

上面的 Logcat 显示了我第一次加载屏幕时的日志。

我的问题:

  1. 为什么所有视图都有 3 次调用。刚刚第一次加载屏幕....
  2. 为什么 convertView NOT NULL 在第二个位置?
  3. 为什么convertView在第二个周期又是NULL?

【问题讨论】:

  • 把它们当作帮手,不要以为它们必须在你想要的任何时候出现:如果 convertView 不为 null 那就太好了,如果不是也很好

标签: android android-listview custom-adapter


【解决方案1】:

为了更好地理解发生了什么,在getView() 中放置一个调试器断点并检查调用堆栈跟踪以了解调用的原因。

您的ListView 似乎处于需要多次测量/布局通道的复杂布局中。例如。 LinearLayout 带有权重或 RelativeLayout 带有子级之间的依赖关系。

要测量ListView,必须测量可见的孩子。这解释了对getView() 的一次这样的调用,对于两遍布局,两个。此外,出于测量目的,视图可以立即回收,因为它们不需要显示在屏幕上。

道德:避免在多通道布局中放置适配器视图。尽可能避免多通道布局。

【讨论】:

    猜你喜欢
    • 2014-05-12
    • 2020-10-09
    • 1970-01-01
    • 1970-01-01
    • 2019-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-11
    相关资源
    最近更新 更多