How to check if footer is added to list?

I have a ListView to which I have added a footer. What call can I make in the list view to determine if this footer is added to the list?

listView.findViewById(errorFooterid) ? 

there seems to be no method

  view.containsView(viewReference) 

Checking that an existing view is contained differently seems like a basic operation, but I don't know if there is a reliable way to check this in android.

Why doesn't the SDK support a clear and easy way to test this? Even findByView (R.id.viewId) and null checking are not a very elegant way to do boolean checking.

+4
source share
3 answers

If you had only one footer and you want to know that it is added or not, you will try to use

 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. 

Therefore, if it returns 0, your listview does not have a footer

 yourListView.getFooterViewsCount(); 
+18
source

You can find out the number of footer views using the following code

 listView.getFooterViewsCount(); 

If it returns 0, there are no footers.

+7
source

If it's in an activity class, just use findViewById (int id) . If the method returns null, you can assume that the footer has not been added.

If this is a fragment class, you can save the view that you return to your onCreateView () method as a data member of your fragment class. Then it works the same, except that you call findViewById () in the view you saved.

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

Source: https://habr.com/ru/post/1436710/


All Articles