将footerview添加到列表视图

时间:2013-03-20 05:13:16

标签: android android-layout android-listview

友。将footerview添加到列表视图时出现问题。我使用以下代码添加了footerview。

TextView totalIncome = new TextView(this);
TextView totalExpense = new TextView(this);
totalIncome.setGravity(Gravity.CENTER);
totalExpense.setGravity(Gravity.CENTER);
totalIncome.setPadding(0, 5, 0, 5);
totalExpense.setPadding(0, 5, 0, 5);
totalIncome.setText("Total Income is : " + tIncome + "");
totalExpense.setText("Total Expense is : " + tExpense + "");
final ListView lv1 = (ListView) findViewById(R.id.ListView);
lv1.addFooterView(totalIncome);
lv1.addFooterView(totalExpense);
lv1.setAdapter(adapter);

但我的问题是在动态更改列表内容时,footerview会在某些步骤中停止运行。我不知道为什么会这样。请帮帮我的朋友。

对于参考:

enter image description here

更改内容后

enter image description here

看到列表与footerview朋友之间的差距。我不想要这个空间。我试图在更改内容时删除footerview。但我不能。我通过互联网搜索了很多。但我没有找到任何解决方案的朋友。请帮忙。提前谢谢。

2 个答案:

答案 0 :(得分:2)

我猜您可以在更改列表视图的内容时尝试删除并重新添加页脚视图

删除:

lv1.removeFooterView(totalIncome);
lv1.removeFooterView(totalExpense);

阅读

lv1.addFooterView(totalIncome);
lv1.addFooterView(totalExpense);

答案 1 :(得分:1)

<强> 1。删除页脚视图

有一种ListView删除FooterView的方法,

lv.removeFooterView(v);

您只需要注意您传递已添加的相同视图,这意味着保存totalIncometotalExpense textview对象,使它们成为{{1并且及时删除传递相同的内容。

<强> 2。删除空格

不确定,仍在努力。

我想,这种情况正好发生,因为一旦你将页眉/页脚设置为listview然后将适配器设置为listview,页眉/页脚视图就会被放置在他们的位置,并且在修改列表内容时这些视图将不会被更改,建议:当您为适配器调用static方法时,请尝试再次删除和添加视图。