Show title always in Android ListView

I need to always show the ListView title, even if the ListView has no items.

Can I do it? Or is it better to add a title to the ListView as the first item?

+5
source share
5 answers

Regardless of whether your elements have ListViewelements or not, the title will always be displayed. If there are elements in the ListView, the title will scroll along with them, it will always be on top.

To add a title to the ListView, you can link to other answers provided by users.

-3
source

If there are no items for the list and you are not adding an adapter, just add this:

mListView.setAdapter(null);

and a heading will appear.

+3
source

onCreate , :

View headerView = ((LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.header_view, null, false);
        getListView().addHeaderView(headerView);

, xml.

+1

As an example, you can use http://viewpagerindicator.com/ and set getListView().setEmptyView(emptyView);// when there are no elements in the listView element

0
source

My solution was to manually switch the blank view.

/**
 * Custom empty view handling because we don't want the
 * list to be hidden when the empty view is displayed,
 * since the list must always display the header.
 */
private void toggleEmptyView(Adapter adapter)
{
    final View emptyView = findViewById(R.id.empty_view);
    adapter.registerDataSetObserver(new DataSetObserver() {
        @Override
        public void onChanged() {
            emptyView.setVisibility(adapter.getCount() == 0 ? View.VISIBLE : View.GONE);
        }
    });
}
0
source

All Articles