Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Horiziontal recyclerview on DrawerLayout

This is my NavigationView's 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 which has some items that user can scroll on it.

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

Is there any way to support horizontal RecyclerView on Drawerlayout?

like image 548
Адриан Avatar asked Aug 21 '15 07:08

Адриан


1 Answers

You should disable intercepting touch event on DrawerLayout when user is scrolling on RecyclerView, So create a custom DrawerLayout like this:

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 your NavigationView and RecyclerView.

In onInterceptTouchEvent i check whether drawer is open and user touch inside RecyclerView then I return false so DrawerLayout do nothing

like image 127
Saeed Masoumi Avatar answered Oct 26 '22 17:10

Saeed Masoumi