使用DrawerLayout可以实现侧滑形式的菜单。在点击侧滑菜单的空白区域时,容易出现点击事件的穿透问题。
我是将侧滑菜单的内容单独做成了一个页面,主页面include进来。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/white">
<include layout="@layout/toolbar"></include>
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawerlayout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<!--主布局-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/base_swipe_refresh_lay"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:scrollbarStyle="insideOverlay"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<com.yanzhenjie.recyclerview.swipe.SwipeMenuRecyclerView
android:id="@+id/base_rv_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"/>
</android.support.v4.widget.SwipeRefreshLayout>
</LinearLayout>
<!--侧滑菜单-->
<include layout="@layout/search_content_eventlist_drawer"></include>
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
主页面是显示一个list列表,侧滑菜单进行条件筛选。列表的每个item都是可点击的,可查看详细信息。问题就是侧滑菜单显示时,点击空白区域,就会被下面的item拦截到,跳转到item的明细界面。
由于侧滑菜单的layout没有处理点击事件,而下层的layout中恰好有控件处理该位置的点击事件,所以点击事件向下传递了。
解决办法很简单,在侧滑菜单的layout中添加点击事件的处理,clickable=true。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/searchWhite"
android:layout_gravity="end"
android:clickable="true">
<!--菜单内容-->
</LinearLayout>