2017-08-07 81 views
0

基本上我有一个ListView(LV),显示了一堆物品,如果LV为空,则显示EmptyView,但它不显示任何代码:空视图不显示任何内容

ListView l = (ListView) view.findViewById(R.id.ArrayList); 
    l.setAdapter(adapter); 
    srl = (SwipeRefreshLayout) view.findViewById(R.id.refreshView); 
    srl.setOnRefreshListener(srfListener()); 
    View Empty = inflater.inflate(R.layout.empty,null); 
    l.setEmptyView(Empty); 

XML文件:

<?xml version="1.0" encoding="utf-8"?> 
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:text="Nothing Available " 
    android:textSize="30sp" 
    android:textColor="@color/colorPrimary" 
    android:layout_height="match_parent" 
    > 
</TextView> 

任何想法解决此问题?顺便说一句,它不是一个列表活动。

+0

能否请您发表您的列表视图的XML? – CzarMatt

+0

https://stackoverflow.com/a/12483610/5923606 – uguboz

回答

1

按照你的代码,你所创建的视图,并在列表中添加作为emptyView,

View Empty = inflater.inflate(R.layout.empty,null); 
l.setEmptyView(Empty); 

但是你可以看到低于其只是让可见光和消失了,它不添加到您的布局实际代码。

private void updateEmptyStatus(boolean empty) { 
     if (isInFilterMode()) { 
      empty = false; 
     } 

     if (empty) { 
      if (mEmptyView != null) { 
       mEmptyView.setVisibility(View.VISIBLE); 
       setVisibility(View.GONE); 
      } else { 
       // If the caller just removed our empty view, make sure the list view is visible 
       setVisibility(View.VISIBLE); 
      } 

      // We are now GONE, so pending layouts will not be dispatched. 
      // Force one here to make sure that the state of the list matches 
      // the state of the adapter. 
      if (mDataChanged) {   
       this.onLayout(false, mLeft, mTop, mRight, mBottom); 
      } 
     } else { 
      if (mEmptyView != null) mEmptyView.setVisibility(View.GONE); 
      setVisibility(View.VISIBLE); 
     } 
    } 

因此,添加它的布局,然后将该视图给setEmptyView

您可以添加这样,

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:swipe="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_gravity="top" 
    android:background="@color/dark_bg_color" 
    android:gravity="top" 
    android:orientation="vertical" > 
    <ListView 
     android:id="@+id/recentcall_listView" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:cacheColorHint="@color/transparent" 
     android:divider="#cecece" 
     android:dividerHeight="1dp" 
     android:listSelector="@color/transparent" 
     /> 

    <ViewStub 
     android:id="@android:id/empty" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_gravity="center" 
     android:layout_margin="20dp" 
     android:layout="@layout/[your empty layout]" /> 

</LinearLayout> 

而且在Java代码中,你可以这样调用,

ViewStub emptyViewStub = (ViewStub) view.findViewById(android.R.id.empty); 
l.setEmptyView(emptyViewStub);