【问题标题】:Anchor a FloatingActionButton with a ListView用 ListView 锚定 FloatingActionButton
【发布时间】:2016-04-16 02:25:51
【问题描述】:

我正在尝试使用 FAB 创建一个列表,以便在滚动列表时具有 FAB 效果(显示/隐藏)。

问题是我的 ListView 和 FAB 在不同的文件中。

带有 FAB 按钮的布局:

    <FrameLayout
        android:id="@+id/frameLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <include layout="@layout/item_list" />
    </FrameLayout>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/addForwardFilter"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        app:layout_anchor="@id/custom_list"
        app:layout_anchorGravity="bottom|right|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@drawable/ic_add" />

</android.support.design.widget.CoordinatorLayout>

布局 item_list.xml

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/item_list"
    android:name="mypackage.test.fragment.FItemListFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ItemListActivity"
    tools:layout="@layout/custom_list_content" />

布局 custom_list_content.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:id="@+id/custom_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:drawSelectorOnTop="false" />

</FrameLayout>

当我尝试锚定时,我得到了以下信息:

java.lang.IllegalStateException: Could not find CoordinatorLayout descendant view with id mypackage.test:id/custom_list to anchor view android.support.design.widget.FloatingActionButton

这个例子有效:

<android.support.design.widget.CoordinatorLayout
    android:id="@+id/main_content"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

          <ListView
              android:id="@+id/lvToDoList"
              android:layout_width="match_parent"
              android:layout_height="match_parent"></ListView>

          <android.support.design.widget.FloatingActionButton
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_gravity="bottom|right"
              android:layout_margin="16dp"
              android:src="@drawable/ic_done"
              app:layout_anchor="@id/lvToDoList"
              app:layout_anchorGravity="bottom|right|end" />

</android.support.design.widget.CoordinatorLayout>

有什么建议吗?

【问题讨论】:

    标签: android listview floating-action-button


    【解决方案1】:

    错误非常明确:FloatingActionButton 找不到它应该锚定到的 id。这是因为 id 位于 custom_list_content.xml 中,仅在 tools:layout 属性中引用。 tools 命名空间仅用于设计时(在我们的例子中,IDE 可以使用片段中的正确内容进行预览)并且不用于运行时。基本上,在运行时,布局最终是这样的:

    <FrameLayout
            android:id="@+id/frameLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">
    
            <fragment xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/item_list"
                android:name="mypackage.test.fragment.FItemListFragment"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
        </FrameLayout>
    
        <android.support.design.widget.FloatingActionButton
            android:id="@+id/addForwardFilter"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|end"
            app:layout_anchor="@id/custom_list"
            app:layout_anchorGravity="bottom|right|end"
            android:layout_margin="@dimen/fab_margin"
            android:src="@drawable/ic_add" />
    
    </android.support.design.widget.CoordinatorLayout>
    

    因此,FloatingActionButton 无法找到 custom_list。在固定示例中,所有内容都放在同一个文件中,包括锚 id,所以没有问题。

    【讨论】:

      【解决方案2】:

      我认为CoordinatorLayout 不适用于 ListViews。您必须改用RecyclerView

      【讨论】:

      • 我编辑了我的帖子。我举了一个适用于 CoordinatorLayout 的示例
      猜你喜欢
      • 2017-01-28
      • 1970-01-01
      • 2020-09-18
      • 2017-01-29
      • 2015-08-14
      • 2021-06-17
      • 2016-04-26
      • 1970-01-01
      • 2019-04-16
      相关资源
      最近更新 更多