5

这是我NavigationView的布局

 <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有一个水平的RecyclerView,其中有一些用户可以在其上滚动的项目。

我的问题是每当我想滚动时RecyclerView,drawerLayout 就会关闭。

有什么方法可以支持水平RecyclerViewDrawerlayout吗?

4

1 回答 1

10

DrawerLayout您应该在用户滚动时禁用拦截触摸事件RecyclerView,因此创建一个这样的自定义DrawerLayout

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;
    }


}

在膨胀你的布局之后,只需调用set并传递你的NavigationViewand RecyclerView

onInterceptTouchEvent我检查抽屉是否打开并且用户在里面触摸RecyclerView然后我返回 false 所以DrawerLayout什么都不做

于 2015-08-21T07:14:07.387 回答