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 later I need to add the previous headers and footers, and keep the same Adapter
.
So I found this solution, but it's ugly and I really hope that there is an other way.
Basically, I have to set a null adapter to be able to add the header view, and then set an empty adapter to add the footer view. To finish I set my real adapter.
Edit: I must add that using the visibility attribute (GONE & VISIBLE) is not a solution here, because the headers & footers views must 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();
}
}
Its very simple!
1) Provide and id to the layout container of you footer.
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 id. Now let's come back to your fragment/activity where you've inflated this footer. So, in order to remove it,
LinearLayout parentContainerForFooter = (LinearLayout) footer.findViewById(R.id.parent_footer_search_list);
**yourview**.removeFooterView(parentContainerForFooter);
Done! This way, you are using correct android standards. Don't using wrap_content, visibility GONE, etc. They are just hacks !
I just had this problem and heres what i did. Add a tag to the view and find and remove it by the 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);
}
}
Do not try to add/remove the header/footer view.
To be able to add/remove header and footer view dynamically, simply add before setting the adapter a RelativeLayout to header and footer. After this you can add/remove what ever you want to the RelativeLayouts.
Set the size of the RelativeLayouts to WRAP_CONTENT.
Harry
You can use below code to do the same
// to show the footer view
footerView.setVisibility(View.VISIBLE);
// to hide the footer view
footerView.setVisibility(View.GONE);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With