Button被RecyclerView覆盖导致无法点击的问题

如题,做项目的时候碰到这个奇葩问题,看布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/refreshLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
  <FrameLayout
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      >

    <Button
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="按钮"
        android:layout_gravity="bottom"
        android:gravity="center"
        />

    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="50dp"
        android:clipToPadding="false"
        />

  </FrameLayout>

</android.support.v4.widget.SwipeRefreshLayout>

布局不复杂,需要说明的是,RecyclerView和Button都在FrameLayout中,且RecyclerView盖在Button上面,Button放在FrameLayout的底部,RecyclerView留出与Button等高的内间距,使得产生RecyclerView滑动到底部时,刚好Button就可见了。

给Button设置点击事件,运行程序,RecyclerView数据足够充满屏幕,滑动到底部,Button可见后,点击Button,无反应。

一开始以为是Button的某些属性不对或遗漏,尝试添加focusable、focusableInTouchMode、clickable等属
性,没用。之后自定义一个MyFrameLayout替换原来布局中的FrameLayout,重写dispatchTouchEvent、
onInterceptTouchEvent、onTouchEvent方法查看事件传递是不是有问题,调试之后得出MyFrameLayout事件传递正常,并没有哪里有异常的事件拦截处理,然而,事件就是传不到Button上。

以前也开发过类似的效果,并没有碰到过这种问题,如果RecyclerView的子项视图中没有响应点击事件的控件,点击是可以"穿透"RecyclerView到达后面的Button的,但这次不知道是哪里的问题。

最后,问题还是要解决的,方法参考自:https://stackoverflow.com/questions/30396075/click-through-recyclerview-empty-space

给RecyclerView设置TouchListener,在onTouch中,手动调用一下Button的dispatchTouchEvent方法,代码:

recyclerView.setOnTouchListener(new View.OnTouchListener() {
    @Override public boolean onTouch(View v, MotionEvent event) {
        button.dispatchTouchEvent(event);
        return false;
    }
});

感觉这个方法不太"优雅",但是目前也没找到更好的方法,有知道问题原因或有更好方法的朋友还请分享分享,谢谢!

可以使用以下两种方法解决这个问题: 1. 在布局中使用RelativeLayout,将Button放在RecyclerView之上,确保Button的z-index比RecyclerView高。 示例代码: ``` <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <androidx.recyclerview.widget.RecyclerView android:id="@+id/recycler_view" android:layout_width="match_parent" android:layout_height="match_parent"/> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:text="Button"/> </RelativeLayout> ``` 2. 使用CoordinatorLayout,将RecyclerViewButton放在不同的子视图中,并使用app:layout_anchor属性将Button锚定在RecyclerView下方。 示例代码: ``` <androidx.coordinatorlayout.widget.CoordinatorLayout android:layout_width="match_parent" android:layout_height="match_parent"> <androidx.recyclerview.widget.RecyclerView android:id="@+id/recycler_view" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior"/> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|center_horizontal" android:text="Button" app:layout_anchor="@id/recycler_view" app:layout_anchorGravity="bottom"/> </androidx.coordinatorlayout.widget.CoordinatorLayout> ``` 这两种方法都可以让ButtonRecyclerView之上,并且不会被RecyclerView覆盖。选择哪种方法取决于你的具体需求和布局结构。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值