2012-09-27 72 views
4

我有一个ListView,我已经添加了一个页脚。我可以在列表视图上做什么调用来确定给定的页脚是否被添加到列表视图中?如何检查页脚是否已添加到列表视图?

listView.findViewById(errorFooterid) ? 

似乎有不是一个方法

view.containsView(viewReference) 

如果现有的观点得到了另一个包含似乎是一个基本操作检查,但我不知道是否有一个可靠的方法在android中检查这个。

为什么SDK不支持清晰简单的方法来检查此问题?即使findByView(R.id.viewId)和null检查也不是非常优雅的方式来执行布尔检查。

回答

0

如果这是一个活动类,那么只需使用findViewById(int id)。如果该方法返回null,那么你可以假设页脚没有被添加。

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

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

嗯,我不知道。如果活动中有两个ListView,该怎么办?有时在页脚布局中未提供id,但是在布局中引用或id,但对于每个列表,布局都是一次膨胀。 –

16

如果你只有一个脚注,想知道它的加入与否,你会尝试使用

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(); 
7

您可以使用以下代码找出页脚视图的数量

listView.getFooterViewsCount(); 

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

相关问题