List activity with header, footer and empty all visible

前端 未结 8 1393
失恋的感觉
失恋的感觉 2021-01-08 00:36

I would like to have the listview in a ListActivity be displayed with the header and footer visible all the time even if the list data is empty.

An empty list causes

8条回答
  •  不思量自难忘°
    2021-01-08 01:03

    In my case I had both header and footer views. I placed my mEmptyView in footer view.

    First, as user1034295 mentioned, I overrided adapter's isEmpty() method:

    @Override
    public boolean isEmpty() {
    // If not overridden, then this will return `true` if your list is empty
    // Thus, you won't see header/footer views
       return false;
    }
    

    ExpandableListView adapter's getGroupCount(). Override appropriate method for your adapter.

    @Override
    public int getGroupCount() {
        int size = null != mData ? mData.size() : 0;
        if (0 == size) mHostFragment.toggleEmptyView(true);
        else mHostFragment.toggleEmptyView(false);
        return size;
    }
    

    In you activity/fragment

    public void toggleEmptyView(boolean show) {
        if(show && mEmptyView.getVisibility() != View.VISIBLE)
            mEmptyView.setVisibility(View.VISIBLE);
        else if (!show && mEmptyView.getVisibility() != View.GONE)
            mEmptyView.setVisibility(View.GONE);
    }
    

    mEmptyView is a child in ListView's footer view. So, when your list becomes 0 sized you'll get your header/footer views remained + mEmptyView will become visible.

提交回复
热议问题