在CollapsingToolbarLayout/AppBarLayout中禁用垂直滚动 [英] Disable vertical scroll in CollapsingToolbarLayout / AppBarLayout
问题描述
我只希望在nestedscrollview中发生滚动/触摸事件时,在CollapsingToolbarLayout/AppBarLayout上垂直滚动(这是有效的),但是如果用户尝试直接滚动CollapsingToolbarLayout/AppBarLayout,则它不起作用.我之所以需要这样做,是因为那里的滚动/触摸事件与我的recyclerview滚动事件搞混了.
I would like the vertical scroll on the CollapsingToolbarLayout / AppBarLayout ONLY when the scroll/touch event occurs in the nestedscrollview (this is working), but if the user tries to scroll the CollapsingToolbarLayout / AppBarLayout directly it should not work. I need this because the scroll/touch events there are messing with my recyclerview scroll events.
我有这样的结构:
CoordinatorLayout
-- AppBarLayout
---- CollapsingToolbarLayout (scrollflags: scroll, exitUntilCollapsed, snap)
------ Toolbar (CollapseMode: pin)
------ RecyclerView (Horizontal, CollapseMode: pin)
----- /CollapsingToolbarLayout
-- /AppBarLayout
-- NestedScrollView (Behaviour: appbar_scrolling_view_behavior)
-- /NestedScrollView
/ CoordinatorLayout
我尝试为CollapsingToolbarLayout/AppBarLayout覆盖onTouch()和onInterceptTouchEvent(),但是它一直拦截触摸事件/滚动.
I tried overriding onTouch() and onInterceptTouchEvent() for both CollapsingToolbarLayout / AppBarLayout but it keeps intercepting the touch event/scroll.
推荐答案
好,我找到了解决方案.
Ok, i found out the solution for this.
您只需要在AppBarLayout行为中覆盖onDrag()方法,并且如果触摸发生在AppBarLayout视图的顶部,则不会触发滚动.
You just have to override the onDrag() method in the AppBarLayout behaviour and the scroll wont be triggered if the touch occurs on top of the AppBarLayout view.
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) appBarLayout.getLayoutParams();
AppBarLayout.Behavior behavior = (AppBarLayout.Behavior) params.getBehavior();
behavior.setDragCallback(new AppBarLayout.Behavior.DragCallback() {
@Override
public boolean canDrag(@NonNull AppBarLayout appBarLayout) {
return false;
}
});
解决方案从这里: 如何在CoordinatorLayout中禁用AppBarLayout滚动?
如果由于行为为空而遇到NullPointerException,则应首先分配一个:
If you run into NullPointerException because of a null Behavior, you should assign one first:
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) mAppbar.getLayoutParams();
params.setBehavior(new AppBarLayout.Behavior());
这篇关于在CollapsingToolbarLayout/AppBarLayout中禁用垂直滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!