Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove header from listView

Tags:

I'm having some problems when trying to remove the header from a listView. At first I use addHeaderView() to add it, but when I change to another layout I want it to disappear but removeHeaderView() doesn't work...

I also tried setting visibility to GONE and it doesn't disappear...

What can I do?

Thanks in advance

like image 792
hasiertxo Avatar asked Nov 28 '12 11:11

hasiertxo


2 Answers

Try the approach mentioned below..

Android ListView#addHeaderView and ListView#addFooterView methods are strange: you have to add the header and footer Views before you set the ListView's adapter so the ListView can take the headers and footers into consideration -- you get an exception otherwise. Here we add a ProgressBar (spinner) as the headerView:

// spinner is a ProgressBar

listView.addHeaderView(spinner); 

We'd like to be able to show and hide that spinner at will, but removing it outright is dangerous because we'd never be able to add it again without destroying the ListView -- remember, we can't addHeaderView after we've it's adapter:

listView.removeHeaderView(spinner); //dangerous! 

So let's hide it! Turns out that's hard, too. Just hiding the spinner view itself results in an empty, but still visible, header area.

Now try to hide the spinner:

spinner.setVisibility(View.GONE); 

Result: header area still visible with an ugly space:

The solution is to put the progress bar in a LinearLayout that wraps it's content, and hiding the content. That way the wrapping LinearLayout will collapse when its content is hidden, resulting in a headerView that is technically still present, but 0dip high:

<LinearLayout        xmlns:a="http://schemas.android.com/apk/res/android"       android:orientation="vertical"       android:layout_width="fill_parent"       android:layout_height="wrap_content">     <!-- simplified -->       <ProgressBar         android:id="@+id/spinner"         android:layout_width="wrap_content"         android:layout_height="wrap_content"/>    </LinearLayout> 

Then, set the layout as the header:

spinnerLayout = getLayoutInflater().inflate(R.layout.header_view_spinner, null); listView.addHeaderView(spinnerLayout); 

And when we need to hide it, hide the layout's content, not the layout:

spinnerLayout.findViewById(R.id.spinner).setVisibility(View.GONE); 

Now the header disappears from view. No more ugly space at the top!

like image 198
Praful Bhatnagar Avatar answered Sep 21 '22 09:09

Praful Bhatnagar


Most people don't like to use AddHeaderView, however I sometimes found it very convenient, to avoid modifying complex adapters, or if the headers are not very related to them.

With this easy trick you will be able to seamlessly remove/add headers:

I add an empty LinearLayout with orientation vertical, and height wrap_content, as the only Header View (let mListView be the target listView):

 LinearLayout mCustomHeaders=new LinearLayout(context);  mCustomHeaders.setOrientation(LinearLayout.VERTICAL);   mListView.addHeaderView(mCustomHeaders);  mListView.setAdapter (.......) 

Thenafter, I can add random stuff, anywhere, to the list as header, even when the list is full:

 mCustomHeaders.add(myHeaderView1);  mCustomHeaders.add(myHeaderView2);  mCustomHeaders.add(myHeaderView3); 

You can also delete all headers, anytime:

 mCustomHeaders.removeAllViews(); // will erase all headers 

You get the idea .... Hope it helps !

like image 34
rupps Avatar answered Sep 21 '22 09:09

rupps