我在我的活动中使用ListView与自定义数组适配器(它只是覆盖getView方法)进行一些聊天活动。当我打开软键盘listview大小调整(例如从5到2个可见行),但getView方法调用7次:
07-11 11:59:13.185: DEBUG/*: [ChatMessageArrayAdapter] generating view for pos:5 07-11 11:59:13.346: DEBUG/*: [ChatMessageArrayAdapter] generating view for pos:4 07-11 11:59:13.485: DEBUG/*: [ChatMessageArrayAdapter] generating view for pos:3 07-11 11:59:13.625: DEBUG/*: [ChatMessageArrayAdapter] generating view for pos:2 07-11 11:59:13.775: DEBUG/*: [ChatMessageArrayAdapter] generating view for pos:1 07-11 11:59:14.015: DEBUG/*: [ChatMessageArrayAdapter] generating view for pos:5 07-11 11:59:14.135: DEBUG/*: [ChatMessageArrayAdapter] generating view for pos:4
如何防止这种开销呢? 请求帮助。
答案 0 :(得分:0)
AFAIK这是getView();
的正常行为
您无法阻止对getView()
的调用,但可以最大限度地减少进程开销。
你会使用类似的东西:
public View getView(final int position, View convertView, ViewGroup parent) {
View row = null;
if(convertView == null)
{
row = LayoutInflater.from(mContext).inflate(R.layout.row, null);
//initialize and set values of components inside row for the first time.
}
else
{
row = convertView;
}
return row;
}