Disable vertical scroll in CollapsingToolbarLayout / AppBarLayout

前端 未结 3 713
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-05 17:55

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

3条回答
  •  一向
    一向 (楼主)
    2021-02-05 18:19

    Ok, i found out the solution for this.

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

    Solution from here: How to disable scrolling of AppBarLayout in CoordinatorLayout?

    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());
    

提交回复
热议问题