5

When my Activity loads, I inflate a layout file that I use for a footer. I attach it to the ListView (addFooterView) and then set its visibility to View.GONE. I maintain a reference to it, and when I want the user to see it, I set the visibility to View.VISIBLE.

For the most part, this works great. However, the footer seems to still take up space. If the user uses the scroll wheel/pad, the area the footer is taking up gets highlighted. I'd like to polish this more so the footer is completely gone; ideally without detaching it from the ListView.

Is this possible? Or am I going to have to set/unset the foot instead of simply toggling its visibility?

Andrew
  • 20,756
  • 32
  • 99
  • 177

4 Answers4

10

You can use listView.removeFooterView(view). The easiest way to do this is to create an instance variable to hold your inflated footer view (so you only inflate it in onCreate() ). Then just call listView.addFooterView(instanceFooter) and listView.removeFooterView(instanceFooter) as needed.

Edit: Here's what I'm doing to get this to work:

  1. inflate footer layout(s) in onCreate
  2. onResume: IF the adapter has not been instantiated, call addFooterView() THEN initialize your adapter (keep an instance reference to it) and call setAdapter(). This will leave the ListView "prepped"
  3. onResume: update the adapter with the data (I have my data in a separate class) and call notifyDatasetChanged()
  4. Call removeFooterView() (it will hide it if it's being displayed and do nothing otherwise)
  5. Call addFooterView() if the footer needs to be displayed
dmon
  • 30,048
  • 8
  • 87
  • 96
  • 3
    I don't think this works. If I remember correctly, you have to add the footer before setting the adapter for it to show up. – Andrew Sep 28 '11 at 20:53
  • I've tested it out just now and my memory seems to be accurate. – Andrew Sep 28 '11 at 20:55
  • This will work if I use mList.setAdapter(mAdapter); instead of mAdapter.notifyDataSetChanged();, but that seems kind of silly. It also messes up the ListView's scrolling. – Andrew Sep 28 '11 at 21:04
  • Err, this totally works for me. But you're right in the sense that the footer has to be added first, though that is the rule for adding a footer anyway (which he is already successfully doing). But yes, the trick is adding it a the beginning regardless of whether you want to show it or not, then calling `removeFooter()`/`addFooter()` on demand. I'll update the answer with the steps of how I got it working. – dmon Sep 29 '11 at 00:26
  • 1
    This does work if you call addFooter() once before setting the adapter. – Andrew Sep 29 '11 at 16:16
  • It's worked fine for me and my two footers. I can add/remove them without any issues. – Buffalo Oct 13 '12 at 09:13
  • I'm tried footer visible and gone but not achieve my goal but after see your answer I'm done it man. Nice answer with nice explanation @dmon – duggu Jan 20 '15 at 08:38
2

You can toggle the visibility. To do that, you need to wrap the content of your footer using a linearlayout, then you set the linearlayout visibility to GONE.

In the example bellow I set the visibility of LogoLinearLayout to GONE and it worked.

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <LinearLayout
            android:id="@+id/LogoLinearLayout"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <ImageView
                android:id="@+id/Logo"
                android:src="@drawable/Logo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/spacing3"
                android:layout_marginBottom="@dimen/spacing3"
                android:layout_gravity="center" />
        </LinearLayout>
    </LinearLayout>
luizlouro
  • 61
  • 1
  • 6
-1

Set isSelectable parameter to false when You call addFooterView to disable footer selection and highlighting

JustAnotherCoder
  • 621
  • 7
  • 13
-2

Try using View.INVISIBLE instead of View.GONE. (I have not tried this,but it might work)

Hiral Vadodaria
  • 19,158
  • 5
  • 39
  • 56
  • 1
    This doesn't work. It seemed like a longshot, but I tried it anyway to no avail. I am essentially getting the result of View.INVISIBLE when setting the visibility to View.GONE. – Andrew Sep 28 '11 at 21:07