【问题标题】:Clicking Floating ActionButton on right corner makes RecyclerView item get clicked单击右上角的浮动操作按钮使 RecyclerView 项目被单击
【发布时间】:2015-12-25 05:24:46
【问题描述】:

我想在 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添加可点击属性

标签: android android-recyclerview floating-action-button


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    请尝试在您的 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

    谢谢..!!

    【讨论】:

    • 这行不通。布局文件中不能有两个根元素。
    • @Michael Peterson:很抱歉在这里造成了误解。给出的代码只是布局文件的一部分。你可以把它放在一个单独的根视图组中。
    【解决方案3】:

    您在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。您可以在 Design area 中检查相同的内容(如果您使用的是 Android Studio)。

    如果有帮助,请告诉我,否则我将进一步深入研究!

    【讨论】:

    • 使用固定高度通常不是解决这个问题的好方法。它只会导致其他 UI 问题,例如大型设备上的列表太短。
    猜你喜欢
    • 1970-01-01
    • 2017-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-07
    • 1970-01-01
    • 1970-01-01
    • 2017-08-02
    相关资源
    最近更新 更多