Android 如何在具有多个ListView的活动中为ListView设置空视图?

Android 如何在具有多个ListView的活动中为ListView设置空视图?,android,listview,user-interface,Android,Listview,User Interface,在一个活动中,我有3个列表堆叠在一起,我想为每个列表设置空视图。我曾尝试将空视图设置为动态创建的视图和布局中定义的视图,但无论哪种方式,当列表为空时,它似乎都会将其折叠为0高度,而不管我已将每个列表的最小高度设置为60px 这是空视图的xml定义 <TextView android:id="@+id/empty_view" android:layout_width="fill_parent" android:layout_height="60px"

在一个活动中,我有3个列表堆叠在一起,我想为每个列表设置空视图。我曾尝试将空视图设置为动态创建的视图和布局中定义的视图,但无论哪种方式,当列表为空时,它似乎都会将其折叠为0高度,而不管我已将每个列表的最小高度设置为60px

这是空视图的xml定义

<TextView android:id="@+id/empty_view"
        android:layout_width="fill_parent"
        android:layout_height="60px"
        android:text="@string/empty_text"
        />

mListView.setEmptyView(findViewById(R.id.empty_view));

糟糕的是,这对我来说是相当愚蠢的:p我有一个相当可笑的印象,如果我在xml布局中随机放置一个TextView,然后将其设置为列表的空视图,它会神奇地消失,然后在列表为空时重新出现在列表中。我想当列表的适配器为空时,列表会将其添加为列表中的一行。即使有人决定尝试一些类似的可笑的事情,并且想知道发生了什么,我也会发布我的原始布局的完整版本以及解决方案

我用来设置空视图的代码是标准的:

mListView.setEmptyView(findViewById(R.id.empty));
但是,我的原始布局如下所示:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="200sp"
    >
    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#FFFFFF"
        android:text="@string/drafts"
        />
    <ListView android:id="@+id/drafts"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:minHeight="60sp"
        />
    <TextView android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:background="#FFFFFF"
        android:text="@string/finished"
        />
    <ListView android:id="@+id/finished"
        android:layout_width="fill_parent"
        android:layout_height="60px"
        android:minHeight="60px"
        />
    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#FFFFFF"
        android:text="@string/uploaded"
        />
    <ListView android:id="@+id/uploaded"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:minHeight="60sp"
        />
    <TextView
        android:id="@+id/empty"
        android:text="DUDE WHERE'S MY CAR?"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
/>
</LinearLayout>

虽然我的列表视图的最小高度为60sp,但当列表为空时,高度似乎变为0。此外,我注意到文本视图在我的三个列表下面都是可见的,所以它似乎没有“消失”并在我的列表中重新显示为一行。真令人失望!在我的一个严重疏忽中,为了进行测试,我只将视图设置为第二个列表的emptyView,确保第一个和第二个列表为空,并填充第三个列表。我的第一个列表有它的最小高度,我的第二个列表缩小到0,我的第三个列表被正确填充,我的空视图出现在它下面。我没有尝试填充第二个列表,因此从未意识到空视图随后会消失

最后,在花了一个多小时的时间处理动态生成的空视图和诸如此类的内容之后,我离开了它一天,今晚回来测试填充第二个列表。瞧,空旷的景色消失了,灯泡继续亮着。我快速测试了将空视图移动到第二个列表下方和第三个列表上方,当然,它工作得非常好

以下是新的工作布局:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="200sp"
    >
    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#FFFFFF"
        android:text="@string/drafts"
        />
    <ListView android:id="@+id/drafts"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />
    <TextView android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:background="#FFFFFF"
        android:text="@string/finished"
        />
    <ListView android:id="@+id/finished"
        android:layout_width="fill_parent"
        android:layout_height="60px"
        android:minHeight="60px"
        />
    <TextView
        android:id="@+id/empty"
        android:text="DUDE WHERE'S MY CAR?"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
    />
    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#FFFFFF"
        android:text="@string/uploaded"
        />
    <ListView android:id="@+id/uploaded"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />

</LinearLayout>


答案很长,可能只是在和我自己说话,但不管怎样-问了问题,我就明白了,我似乎永远无法回答其他人的问题,所以我最好回答我自己的问题:)

我的错,这对我来说是相当愚蠢的:P我有一个相当可笑的印象,如果我在xml布局中随机放置一个TextView,然后将其设置为列表的空视图,它会神奇地消失,然后在列表为空时重新出现在列表中。我想当列表的适配器为空时,列表会将其添加为列表中的一行。即使有人决定尝试一些类似的可笑的事情,并且想知道发生了什么,我也会发布我的原始布局的完整版本以及解决方案

我用来设置空视图的代码是标准的:

mListView.setEmptyView(findViewById(R.id.empty));
但是,我的原始布局如下所示:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="200sp"
    >
    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#FFFFFF"
        android:text="@string/drafts"
        />
    <ListView android:id="@+id/drafts"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:minHeight="60sp"
        />
    <TextView android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:background="#FFFFFF"
        android:text="@string/finished"
        />
    <ListView android:id="@+id/finished"
        android:layout_width="fill_parent"
        android:layout_height="60px"
        android:minHeight="60px"
        />
    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#FFFFFF"
        android:text="@string/uploaded"
        />
    <ListView android:id="@+id/uploaded"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:minHeight="60sp"
        />
    <TextView
        android:id="@+id/empty"
        android:text="DUDE WHERE'S MY CAR?"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
/>
</LinearLayout>

虽然我的列表视图的最小高度为60sp,但当列表为空时,高度似乎变为0。此外,我注意到文本视图在我的三个列表下面都是可见的,所以它似乎没有“消失”并在我的列表中重新显示为一行。真令人失望!在我的一个严重疏忽中,为了进行测试,我只将视图设置为第二个列表的emptyView,确保第一个和第二个列表为空,并填充第三个列表。我的第一个列表有它的最小高度,我的第二个列表缩小到0,我的第三个列表被正确填充,我的空视图出现在它下面。我没有尝试填充第二个列表,因此从未意识到空视图随后会消失

最后,在花了一个多小时的时间处理动态生成的空视图和诸如此类的内容之后,我离开了它一天,今晚回来测试填充第二个列表。瞧,空旷的景色消失了,灯泡继续亮着。我快速测试了将空视图移动到第二个列表下方和第三个列表上方,当然,它工作得非常好

以下是新的工作布局:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="200sp"
    >
    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#FFFFFF"
        android:text="@string/drafts"
        />
    <ListView android:id="@+id/drafts"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />
    <TextView android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:background="#FFFFFF"
        android:text="@string/finished"
        />
    <ListView android:id="@+id/finished"
        android:layout_width="fill_parent"
        android:layout_height="60px"
        android:minHeight="60px"
        />
    <TextView
        android:id="@+id/empty"
        android:text="DUDE WHERE'S MY CAR?"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
    />
    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#FFFFFF"
        android:text="@string/uploaded"
        />
    <ListView android:id="@+id/uploaded"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />

</LinearLayout>

答案很长,可能只是在和自己说话,但不管怎样——问了这个问题,我就明白了,而且我似乎永远无法回答其他人的问题,所以我不妨回答我自己的:)