【发布时间】: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 显示了我第一次加载屏幕时的日志。
我的问题:
- 为什么所有视图都有 3 次调用。刚刚第一次加载屏幕....
- 为什么 convertView NOT NULL 在第二个位置?
- 为什么convertView在第二个周期又是NULL?
【问题讨论】:
-
把它们当作帮手,不要以为它们必须在你想要的任何时候出现:如果 convertView 不为 null 那就太好了,如果不是也很好
标签: android android-listview custom-adapter