如何检查页脚视图中是否已添加页脚?

时间:2012-09-27 22:54:31

标签: android

我有一个ListView,我添加了一个页脚。我可以在列表视图上调用什么来确定给定的页脚当前是否已添加到列表视图中?

  listView.findViewById(errorFooterid) ?

似乎没有方法

 view.containsView(viewReference)

检查现有视图是否包含在另一个视图中似乎是一个基本操作,但我不知道是否有一种可靠的方法可以在android中检查它。

为什么SDK不支持一种清晰简单的方法来检查?甚至findByView(R.id.viewId)和null检查也不是很优雅的做布尔检查的方法。

3 个答案:

答案 0 :(得分:18)

如果你只有一个页脚并想知道它是否添加,你会尝试使用

public int getFooterViewsCount ()
Since: API Level 1

Returns the number of footer views in the list. Footer views are special views at the bottom of the list that should not be recycled during a layout.
Returns

    The number of footer views, 0 in the default implementation. 

因此,如果它返回0,则listview没有页脚

yourListView.getFooterViewsCount();

答案 1 :(得分:8)

您可以使用以下代码

找出页脚视图的数量
listView.getFooterViewsCount();

如果返回0,则不存在页脚视图。

答案 2 :(得分:0)

如果这是在活动类中,那么只需使用findViewById(int id)。如果该方法返回null,那么您可以假设尚未添加页脚。

如果这是一个片段类,那么您可以将在onCreateView()方法中返回的视图存储为片段类的数据成员。然后它以相同的方式工作,除了你将在你保存的视图上调用findViewById()。

LinearLayout footer = (LinearLayout) findViewById(footerId);
if (footer == null)
{
    //add the footer
}
else
{
    //Update the footer
}