为了清楚起见,我重写了这个问题:
我有一个 ListView 和一个 FooterView,页脚内有一个刷新按钮。
我在活动级别保留对所有这些的引用
ListView myListView;
View footerView;
Button refreshBtn;
在
onCreate(Bundle)
{
footerView = inflate footer view etc;
refreshBtn = footerView.findViewById(R.id.refresh_btn);
refreshBtn.setOnClickListener(this);
myListView.addFooter(footerView);
}
onClick()
{
// refresh clicked. Don't show FooterView anymore unless its needed again
myListView.removeView(footerView) //
}
onRefresthResult()
{
// looks like we need to try refresh again. So lets add the refresh footer back in.
// THIS DOES NOT WORK. CLICKING refreshBtn does NOTHING.
myListView.addFooter(footerView);
resetListAdapter();
// Now refresh button does NOTHING. unless we do THIS below instead.
// THIS DOES WORK, BUT WHY?
footerView = inflate footer view. (Why does this need to be done again. footerView was not null both it and the refreshBtn seemed to have valid values).
refreshBtn = footerView.findViewById(R.id.refresh_btn);
refreshBtn.setOnClickListener(this);
// Note footerView reference was always valid and resetting onClick
without reinflating did not work.
}
我唯一的理论是,当您删除视图时,它会以某种方式被标记,因此必须重新添加页脚视图的新实例?