在DrawerLayout Horiziontal recyclerview [英] Horiziontal recyclerview on DrawerLayout
问题描述
这是我的 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屋!