【问题标题】:RecyclerView Item Getting Clicked When Click on FloatingActionButton in Android在Android中单击FloatingActionButton时RecyclerView项目被点击
【发布时间】:2017-07-04 12:07:16
【问题描述】:

我的应用中有一个FloatingActionButton,而不是RecyclerViewButton 显示正常,但是当我单击它时,它下面的 RecyclerView 项目正在获取单击事件,而不是按钮。如果我将RecyclerView 设置为显示在FloatingActionButton 之上,Button 将按预期工作。

如何解决,让Button获取点击事件?

谢谢。

【问题讨论】:

  • 将 FAB 放在相对布局或 LinearLayout 中,并将布局属性设置为 android:clickable="true" android:focusableInTouchMode="true" 并完成。
  • Fab.bringToFront() 并在 xml 中添加 clickable:true
  • 如果您使用 android:clickable="false" android:enabled="false" android:focusableInTouchMode="false" 作为按钮,那么如果您在按钮上设置 OnClick 侦听器,它将需要单击。

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


【解决方案1】:

只有在 FloatingActionButton 上没有设置 onClick 监听器时才会发生这种情况。所以只要在FloatingActionButton上设置onClick Listener你的问题就解决了....

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //Your code to do something if you have
        }
});

如果需要采取行动,只需像上面那样实现一个空侦听器..

【讨论】:

  • 这应该是公认的答案。这是正确的。
【解决方案2】:

对屏幕使用 FrameLayout。你可以获得所有的点击事件、项目或浮动按钮。

<FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/white">
 <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </android.support.v7.widget.RecyclerView>
<android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_dialog_email" />
</FrameLayout>

【讨论】:

    猜你喜欢
    • 2021-12-28
    • 1970-01-01
    • 2020-07-03
    • 1970-01-01
    • 1970-01-01
    • 2019-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多