Android 带有 header 的 ListView 设置空视图将导致 header 被隐藏

Android ListView with header Setting empty view will cause the header to be hidden

目前我有这个带有 header 的 listView。

View headerView = getLayoutInflater().inflate(R.layout.headerview, null);
View emptyView = getLayoutInflater().inflate(R.layout.emptyView, null);
mlistView.addHeaderView(headerView);
mListView.setEmptyView(emptyView);

这是我的空视图

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/empty_layout_holder"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/empty_textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="No items"
        android:textSize="24dp" />


</RelativeLayout>

Header Xml

  <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white"
        android:orientation="vertical">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/fullname"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>


    </LinearLayout>

请注意,我将 emptyView 放在一个单独的 XML 中,因为我要更改它的颜色。目前我无法继续,因为设置空视图仅显示没有 header 甚至文本 "No items" 的白色布局。所以我想它不起作用。我在这里错过了什么吗?

必须将视图添加到您的 activity 布局中:

final int mp = ViewGroup.LayoutParams.MATCH_PARENT;
addContentView(emptyView, new RelativeLayout.LayoutParams(mp, mp));

ListView 仅设置 EmptyView 的可见性(如果存在)。在您的情况下,可见性已设置,但由于未添加,因此不会显示。