首页 > 解决方案 > 即使列表不为空,emptyView 也始终显示

问题描述

我希望有人可以看一下,让我知道出了什么问题。这是我在 activity_main 中的 emptyView 容器

<ListView
        android:id="@+id/display_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <RelativeLayout
        android:id="@+id/empty_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true">

        <TextView
            android:id="@+id/empty_shelter_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:fontFamily="sans-serif-medium"
            android:paddingTop="16dp"
            android:text="@string/empty_view_title_text"
            android:textAppearance="?android:textAppearanceMedium" />

        <TextView
            android:id="@+id/empty_subtitle_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/empty_shelter_title"
            android:layout_centerHorizontal="true"
            android:fontFamily="sans-serif"
            android:paddingTop="8dp"
            android:text="@string/empty_view_sub_text"
            android:textAppearance="?android:textAppearanceMedium"
            android:textColor="#A2AAB0" />

    </RelativeLayout>

我在每个片段中设置了 emptyView,即使项目显示在列表中,它仍然显示空视图字段。

//set empty activity
    ListView listView = rootView.findViewById(R.id.display_view);
    View emptyView = rootView.findViewById(R.id.empty_view);
    listView.setEmptyView(emptyView);
    //create a new cursor adapter
    mCursorAdapter = new PlatformCursorAdapter(getActivity(),null);
    listView.setAdapter(mCursorAdapter);

如果你能告诉我我做错了什么,我将不胜感激。

标签: javaandroidxmllistviewandroid-cursoradapter

解决方案


尝试在 xml 中隐藏 empty_view。

<RelativeLayout
        android:id="@+id/empty_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:visibility="gone"
        android:layout_centerInParent="true">

推荐阅读