Адр*_*иан 4 android drawerlayout android-recyclerview navigationview
这是我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即将关闭.
有没有什么办法来支持水平RecyclerView上Drawerlayout?
您应该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并通过您的NavigationView和RecyclerView.
在onInterceptTouchEvent我检查抽屉是否打开和用户触摸内部RecyclerView然后我返回假,所以DrawerLayout什么都不做
| 归档时间: | 
 | 
| 查看次数: | 1045 次 | 
| 最近记录: |