Android 使用ListView锚定浮动操作按钮
我正在尝试使用FAB创建一个列表,以便在滚动列表时具有FAB效果(显示/隐藏)Android 使用ListView锚定浮动操作按钮,android,listview,floating-action-button,Android,Listview,Floating Action Button,我正在尝试使用FAB创建一个列表,以便在滚动列表时具有FAB效果(显示/隐藏) <FrameLayout android:id="@+id/frameLayout" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_beha
<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>
问题是我的ListView和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>
带有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>
此示例适用于:
<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>
<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>
有什么建议吗?我认为
CoordinatorLayout
不适用于ListView。您必须改用RecyclerView
。错误非常明确:FloatingAction按钮找不到它应该定位到的id。这是因为id位于custom_list_content.xml中,它仅在tools:layout属性中引用。工具名称空间仅用于设计时(在本例中,IDE可以使用片段中的正确内容进行预览),不用于运行时。基本上,在运行时,布局以如下方式结束:
<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>
<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无法找到自定义的列表。在固定示例中,所有内容都放在同一个文件中,包括锚id,因此没有问题。我编辑了我的帖子。我举了一个与CoordinatorLayout配合良好的例子
<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>