Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hiding footer in ListView

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?

like image 883
Andrew Avatar asked Sep 27 '11 21:09

Andrew


2 Answers

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
like image 133
dmon Avatar answered Oct 11 '22 11:10

dmon


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>
like image 24
luizlouro Avatar answered Oct 11 '22 10:10

luizlouro