【问题标题】:ListView adds empty view to root/parent instead of self?ListView 将空视图添加到根/父级而不是自身?
【发布时间】:2015-05-27 22:11:21
【问题描述】:

我正在尝试向ListView 添加自定义空视图,如this Q&A 所示。但是ListView 似乎将空视图添加到其父级,或者可能添加到根ViewGroup,而不是添加到自身。我在这里做错了吗?

我创建了一个小布局文件来演示该问题。注意空视图是如何放置在活动的左上角的,完全在ListView之外:

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

    <TextView
        android:id="@+id/top_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Some text above the ListView" />

    <ListView
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_below="@id/top_text" />

    <TextView
        android:id="@android:id/empty"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ListView empty text" />

</RelativeLayout>

【问题讨论】:

  • “我在这里做错了吗?” -- 好吧,您可能希望将 TextView 居中。这由您应用的布局规则控制,现在,您将其设置在左上角。
  • @CommonsWare 同意。但最直接的问题是它在哪个视图的左上角。
  • 嗯,它必须在某个地方。它不能是ListView 的孩子。如果你不喜欢它的位置,把它移到别处。你还没有真正解释问题是什么。
  • @CommonsWare 列表项不是ListView 的子项吗?如果不是,那么问题是我假设它们是并且空视图也应该是。如果那是错的,那什么是对的?

标签: android android-listview android-5.0-lollipop


【解决方案1】:

列表项不是 ListView 的子项吗?

是的,当ListView 通过关联的ListAdapter 创建它们时。

空视图不是列表项。它不是ListView 的子代。这只是另一种观点。 AdapterView 切换自身的可见性和空视图,这取决于 AdapterView 是否为空。如果AdapterView 为空,则其自身的可见性设置为GONE,空视图设置为VISIBLE。如果AdapterView 不为空,则其自身的可见性设置为VISIBLE,而空视图的可见性设置为GONE。参见the source code for AdapterView 中的updateEmptyStatus()

鉴于可见性切换效果,典型的做法是让空视图大致占据与ListView 相同的空间。但是,这不是必需的。

【讨论】:

    猜你喜欢
    • 2019-05-04
    • 1970-01-01
    • 2022-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-03
    • 1970-01-01
    • 2015-08-14
    相关资源
    最近更新 更多