我正在尝试将空页脚 ( LinearLayout
) 添加到我ListView
的页脚,并在之后用我自己的视图填充此页脚:
private View footerButton;
private LinearLayout footer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.rlist);
footer=new LinearLayout(this);
footer.setLayoutParams(new ListView.LayoutParams(ListView.LayoutParams.MATCH_PARENT, ListView.LayoutParams.WRAP_CONTENT));
footerButton=((LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.footer_button, null, false);
ListView list=(ListView) findViewById(R.id.list);
list.addFooterView(footerButton,null,false);
final UsersListAdapter adapter = new UsersListAdapter(this);
list.setAdapter(adapter);
//...
new Thread() {
@Override
public void run() {
//Loading...
runOnUiThread(new Runnable() {
public void run() {
adapter.notifyDataSetChanged();
footer.removeAllViews();
footer.addView(footerButton);
}
});
}
}.start();
}
但我在“footer.addView(footerButton);”行中遇到异常:
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
为什么?如您所知,我会removeAllViews()
在添加任何视图之前致电。如何解决?