List activity with header, footer and empty all visible

前端 未结 8 1314
失恋的感觉
失恋的感觉 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.

    0 讨论(0)
  • 2021-01-08 01:08

    You can put the listView and the empty layout together in a FrameLayout, and put the empty layout margin in the size of the header, then, you can change the visibility of the empty view when an item is added to listview.

    layout will be as similar to this:

    <FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1">
    
        <ListView
            android:id="@+id/myListView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            tools:listheader="@layout/myHeader"/>
    
        <LinearLayout
            android:layout_marginTop="@dimen/headerHeight"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/white"
            android:id="@+id/emptyLayout">
    
           //insert your layout here
    
        </LinearLayout>
    
    </FrameLayout>
    
    0 讨论(0)
提交回复
热议问题