Adding and removing headers and footers dynamically

I need to be able to add and remove headers and footers from my ListView dynamically.

So, I initialize my activity with my headers and footers, then at some point I want to hide them, and then I need to add the previous headers and footers and keep the same Adapter .

So, I found this solution, but it is ugly, and I really hope that there is another way.
Basically, I have to install a null adapter in order to be able to add a header view, and then install an empty adapter to add a footer view. To finish, I installed my real adapter.

Edit: I have to add that using the visibility attribute (GONE and VISIBLE) is not the solution here, because the header and footer views should not be in the adapter during my intermediate procedure.

  public class TestAdapterHeader extends ListActivity implements OnClickListener { private static String[] items = { "test 1", "test 2", "test 3", "test 4", "test 5", "test 6", "test 7", "test 8", "test 9", "test 10", "test 11", "test 12", "test 13", "test 14", "test 15", "test 16", "test 17", "test 18", "test 19", "test 20" }; private ArrayAdapter mAdapter; private LinearLayout mParentView; private TextView mHeaderView, mFooterView; private boolean mViewsHidden = false; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); initViews(); mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items); setListAdapter(mAdapter); } private void initViews() { // The main layout mParentView = new LinearLayout(this); mParentView.setOrientation(LinearLayout.VERTICAL); mParentView.setBackgroundColor(Color.BLACK); // The button to hide the views Button hideViewsButton = new Button(this); hideViewsButton.setText("Add/Remove views"); hideViewsButton.setOnClickListener(this); // The listview ListView listView = new ListView(this); listView.setId(android.R.id.list); listView.setCacheColorHint(Color.TRANSPARENT); mParentView.addView(hideViewsButton); mParentView.addView(listView); // Set the content view setContentView(mParentView); AbsListView.LayoutParams lp = new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, 150); mHeaderView = new TextView(this); mHeaderView.setTextColor(Color.WHITE); mHeaderView.setBackgroundColor(Color.BLUE); mHeaderView.setGravity(Gravity.CENTER); mHeaderView.setLayoutParams(lp); mHeaderView.setText("Header"); mFooterView = new TextView(this); mFooterView.setTextColor(Color.WHITE); mFooterView.setBackgroundColor(Color.BLUE); mFooterView.setGravity(Gravity.CENTER); mFooterView.setLayoutParams(lp); mFooterView.setText("Footer"); getListView().addHeaderView(mHeaderView); getListView().addFooterView(mFooterView); } @Override public void onClick(View v) { mViewsHidden = !mViewsHidden; // Remove header & footer views if (mViewsHidden) { getListView().removeHeaderView(mHeaderView); getListView().removeFooterView(mFooterView); } else { // Remove the ListAdapter to be able to add our headerView setListAdapter(null); getListView().addHeaderView(mHeaderView); // Set an empty ListAdapter to be able to add our footerView setListAdapter(new ArrayAdapter<String>(TestAdapterHeader.this, -1)); getListView().addFooterView(mFooterView); // Re set our Adapter setListAdapter(mAdapter); } mParentView.requestLayout(); } } 
+8
android listview
source share
11 answers

Do not try to add / remove header / footer view.

To be able to add / remove headers and footers dynamically, simply add RelativeLayout to the header and footer before installing the adapter. After that, you can add / remove whatever you want in RelativeLayouts.

Set the RelativeLayouts size for WRAP_CONTENT.

Harry

+8
source share

You can use the code below to do the same

 // to show the footer view footerView.setVisibility(View.VISIBLE); // to hide the footer view footerView.setVisibility(View.GONE); 
+6
source share

You should use view.setVisibility(int visibility) View.GONE, View.VISIBLE or View.INVISIBLE

  .... if (mViewsHidden) { mHeaderView.setVisibility(View.GONE); mFooterView.setVisibility(View.GONE); } .... else { mHeaderView.setVisibility(View.VISIBLE); mFooterView.setVisibility(View.VISIBLE); } .... 
+2
source share

I had this problem and I realized what I did. Add a tag to the view and find and remove it by tag.

  final Button btnAddMore = new Button(this); btnAddMore.setTag("footer"); if(myList.getFooterViewsCount() >0) { View v = myList.findViewWithTag("footer"); if(v != null) { myList.removeFooterView(v); } } 
+2
source share

you need to add headers and footers to listview before setAdapter, after which you can manipulate with setVisibility

good luck;)

+1
source share

This seems to be one of the few solutions. I just tried something very similar, and it worked, but I'm in the same boat. For me, it looks like a hack.

There are several other options, such as manually adding / removing / hiding the header / footer in an off-list view. However, you cannot get a good scroll effect, which is pretty much the listview's title point.

I just tried setting the view to View.GONE, and instead of the faded view, a space at the top of the ListView.

0
source share

In this situation, I found a job, although most may not like it. Just add a line layout above and below the list. Now you can dynamically add and remove views with the ability to use visibility parameters. If you set the WRAP_CONTENT parameters, then the layout will not take up space if there are no children.

0
source share

I had a similar problem - I had to add or remove the header dynamically, and I did it like this:
- I deleted the elements from the adapter and set adapter=null;
- To delete: r emoveHeaderView(mHeaderView);
- To add a title: addHeaderView(mHeaderView);
- re-create the adapter and install the content

It works, but without animation it looks ugly

0
source share
 its work fine for me! private void addHeaderList(){ mListView.setAdapter(null); mListView.addHeaderView(headerList); } private void removeHeaderList(){ mListView.removeHeaderView(headerList); } just called call function before setadapter again. addHeaderList(); mListView.setAdapter(mAdapter); 
0
source share

It is very easy!

1) Specify and id in the footer layout container.

Ex. footer.xml

  <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:id="@+id/parent_footer_search_list" android:layout_height="fill_parent" android:background="#fff" android:orientation="vertical"> <TextView android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center|left" android:lineSpacingExtra="5dp" android:text="Create new list" android:textColor="#666666" android:textSize="16.8sp" android:textStyle="bold" /> </LinearLayout> 

In the above xml, parent_footer_search_list is your main container identifier. Now back to your snippet / activity where you puffed this footer. So, to remove it,

 LinearLayout parentContainerForFooter = (LinearLayout) footer.findViewById(R.id.parent_footer_search_list); **yourview**.removeFooterView(parentContainerForFooter); 

Done! So you are using the correct Android standards. Not using wrap_content, GONE visibility, etc. These are just hacks!

0
source share

Try using list.addFooterView (footerview) for the footer and list.addHeaderView (headerview) for the header.

-one
source share

All Articles