Horiziontal recyclerview on DrawerLayout

落花浮王杯 提交于 2019-12-21 05:29:47

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/32134373/horiziontal-recyclerview-on-drawerlayout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!