在DrawerLayout Horiziontal recyclerview [英] Horiziontal recyclerview on DrawerLayout

查看:390
本文介绍了在DrawerLayout Horiziontal recyclerview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的 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 具有水平<$​​ C $ C> RecyclerView 有用户可以在其上滚动的一些项目。

headerLayout has a horizontal RecyclerView which has some items that user can scroll on it.

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

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

有没有什么办法来支持水平 RecyclerView Drawerlayout

Is there any way to support horizontal RecyclerView on Drawerlayout?

推荐答案

你想要的是禁止对 DrawerLayout 当用户滚动在 RecyclerView ,因此,创建一个自定义的 DrawerLayout 是这样的:

What you want is 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;
    }


}

和膨胀您的布局后,只需拨打设置并通过您的 NavigationView RecyclerView

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

onInterceptTouchEvent 我检查是否是内部 RecyclerView 抽屉打开,用户触摸然后我返回false,那么 DrawerLayout 束手无策

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

这篇关于在DrawerLayout Horiziontal recyclerview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆