开发者

List activity with header, footer and empty all visible

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 the empty view to appear and the header and footer to disappear. However my header has filtering UI and开发者_如何学Python should therefore always be visible.

The only way I can make it happen at the moment is if I take the header and footer out of the listview and implement them as static views outside in the activity layout. However then these are always visible and only the data scrolls.

I would prefer for both to just be on top and bottom of the scrolling list. Wrapping it all in a scroll view does not work since then there are two nested scroll views( the list view outside and the wrapping one).

Is there a way to do this nicely apart from a hack like adding a fake record?


Make your empty view include your header.

i.e.

 <ListView />
 <RelativeLayout
    android:id="@android:id/empty"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
       // Add whatever you want here including your header
 </RelativeLayout>


This answer is a bit late, but you can simply override BaseAdapter.isEmpty() to return false.

public boolean isEmpty() {
    return false;
}


You could use my MergeAdapter for this. Add the header view, add the data adapter, and add the footer view. They will all scroll in unison, and the header and footer will persist even if the adapter in the middle has no rows.


Ok, this workaround is a hack. But, without having to create a fake item.

For an ArrayAdapter you can add a null item... i.e.

myAdapter.add(null);

And, in the MyAdapter getView(..) you do something like:

myItem = getItem(position);
if (myItem == null) {
   return getLayoutInflater().inflate(R.layout.my_empty_row, null);
} else {
...
}


I found that header/footer will always be visible if you don't call ListView.setEmptyView.

Instead, manually show/hide your empty view:

emptyView.setVisibility(list.size() == 0 ? View.VISIBLE : View.GONE);


I meet the same issue, if ListView has no item, you could ListView.setAdapter(new SomeAdapter(new ArrayList())); and the header is also visible.


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.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜