Horiziontal's directorial review on DrawerLayout

This is my NavigationView layout.

  <android.support.design.widget.NavigationView android:id="@+id/nav_view" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="start" app:headerLayout="@layout/header" app:menu="@menu/meny" /> 

headerLayout has a horizontal RecyclerView that has some elements that the user can scroll on it.

My problem is, whenever I want to scroll in RecyclerView , drawerLayout will close.

Is there a way to support horizontal RecyclerView on Drawerlayout ?

+4
source share
1 answer

You must disable touch event capture on DrawerLayout when the user scrolls to RecyclerView . Therefore, create a custom DrawerLayout as follows:

 public class DrawerLayoutHorizontalSupport extends DrawerLayout { private RecyclerView mRecyclerView; private NavigationView mNavigationView; public DrawerLayoutHorizontalSupport(Context context) { super(context); } public DrawerLayoutHorizontalSupport(Context context, AttributeSet attrs) { super(context, attrs); } public DrawerLayoutHorizontalSupport(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override public boolean onInterceptTouchEvent(MotionEvent ev) { if (isInside(ev) && isDrawerOpen(mNavigationView)) return false; return super.onInterceptTouchEvent(ev); } private boolean isInside(MotionEvent ev) { //check whether user touch recylerView or not return ev.getX() >= mRecyclerView.getLeft() && ev.getX() <= mRecyclerView.getRight() && ev.getY() >= mRecyclerView.getTop() && ev.getY() <= mRecyclerView.getBottom(); } public void set(NavigationView navigationView, RecyclerView recyclerView) { mRecyclerView = recyclerView; mNavigationView = navigationView; } } 

And after inflating your layout, just call set and pass in your NavigationView and RecyclerView .

In onInterceptTouchEvent I check if the box is open and the user touched inside the RecyclerView , then I return false, so DrawerLayout does nothing

+7
source

All Articles