单击右上角的浮动操作按钮使 RecyclerView 项目被单击

Clicking Floating ActionButton on right corner makes RecyclerView item get clicked

我想在 recyclerview 上添加浮动操作按钮,但问题是当我点击浮动操作按钮时 Recyclerview 项目被点击如何解决这个问题

见下方代码

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".screens.ShowSubjectsFrag"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">
    <android.support.v7.widget.RecyclerView
        android:id="@+id/MainList"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center">
    </android.support.v7.widget.RecyclerView>
    <android.support.design.widget.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@android:drawable/ic_input_add"
        app:layout_anchor="@id/MainList"
        android:layout_margin="@dimen/fab_margin"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />
</RelativeLayout>

请尝试在您的 xml 中使用以下代码:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/MainList"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center">
    </android.support.v7.widget.RecyclerView>

</RelativeLayout>

<android.support.design.widget.FloatingActionButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@android:drawable/ic_input_add"
    app:layout_anchor="@id/MainList"
    android:layout_margin="@dimen/fab_margin"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

将两个视图放在不同的布局中,以便它们可以与不同的 z 级别相关。

有关详细信息,请查看设计实践 here

谢谢..!!

您正在 RelativeLayout 中使用 RecyclerView,并且您已将其 widthheight 都设置为 match_parent!基本上你的 RecyclerView 隐藏了你的 FloatingActionButton.

如果我是你,我会做以下事情:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".screens.ShowSubjectsFrag"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">
    <android.support.v7.widget.RecyclerView
        android:id="@+id/MainList"
        android:layout_width="match_parent"
        android:layout_height="300dp" <!-- Use a fixed height over here -->
        android:layout_alignParentTop="true">
    </android.support.v7.widget.RecyclerView>
    <android.support.design.widget.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@android:drawable/ic_input_add"
        app:layout_anchor="@id/MainList" 
        android:layout_margin="@dimen/fab_margin"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />
</RelativeLayout>

我还怀疑 layout_anchor 不是必需的。也尝试将其删除。无论如何,在我看来主要问题是您的 RecyclerView 隐藏了您的 FloatingActionButton。您可以在 设计区域 (如果您使用的是 Android Studio)中进行检查。

如果有帮助请告诉我,否则我将深入探讨!

在您的 Activity/Fragment 中为 FloatingActionButton 写下 View#onClickListener 因为目前您的 FloatingActionButton 没有注册任何 event。 有同样的问题 干杯