【问题标题】:setEmptyView() doesn't seem to work for (Sherlock)ListFragmentsetEmptyView() 似乎不适用于 (Sherlock)ListFragment
【发布时间】:2013-07-04 13:54:04
【问题描述】:

我有一个ListFragment,我想设置它的空视图,所以我使用以下代码:

View emptyView = getLayoutInflater().inflate(R.layout.collection_empty_view, null);
detailFragment.getListView().setEmptyView(emptyView);

对于以下 xml 布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/empty"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/tv_empty"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:text="@string/empty_collection" />

    <Button
        android:id="@+id/btn_add_legislation"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="addText"
        android:text="@string/action_add_text" />

</LinearLayout>

但什么都没有出现!

我试过detailFragment.getListView().setEmptyText(),确实工作!我做错了吗?

【问题讨论】:

    标签: android android-listview actionbarsherlock android-listfragment


    【解决方案1】:

    空视图必须与列表视图位于同一视图层次结构中。那样膨胀是行不通的。

    通常我会按以下格式创建布局:

    <Layout>
        <ListView/>
        <View android:id="@+id/empty"/>
    </Layout>
    

    空视图是我想用作“空视图”的视图。然后在创建时我得到一个空视图的引用并使用该引用调用 setEmptyView()。

    这是来自 Android 文档的示例(查看 documentation,它应该适用于 SherlockListFragment):

     <?xml version="1.0" encoding="utf-8"?>
     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:orientation="vertical"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
             android:paddingLeft="8dp"
             android:paddingRight="8dp">
    
         <ListView android:id="@id/android:list"
                   android:layout_width="match_parent"
                   android:layout_height="match_parent"
                   android:background="#00FF00"
                   android:layout_weight="1"
                   android:drawSelectorOnTop="false"/>
    
         <TextView android:id="@id/android:empty"
                   android:layout_width="match_parent"
                   android:layout_height="match_parent"
                   android:background="#FF0000"
                   android:text="No data"/>
     </LinearLayout>
    

    【讨论】:

    • 效果很好!我唯一要补充的是,如果您仍想使用ListFragment,则列表ID 应为android.R.id.list。
    【解决方案2】:

    您可以在我的应用程序片段中尝试我正在做的事情。 我不知道这是否是一个好习惯,但它确实有效!

    View emptyView = getActivity().getLayoutInflater().inflate(R.layout.empty, null);
    getActivity().setContentView(emptyView);
    

    【讨论】:

      猜你喜欢
      • 2020-04-04
      • 2023-04-06
      • 2017-08-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多