在嵌套视图/片段上滚动时,不会调用AppBarLayout.Behaviour

时间:2017-05-07 18:01:36

标签: android android-layout android-fragments android-coordinatorlayout coordinator-layout

我有一个带有AppBarLayout折叠工具栏的CoordinatorLayout。 我的布局文件层次结构看起来像(省略了layout_width):

<CoordinatorLayout>
   <AppBarLayout>
      <CollapsingToolbarLayout 
       app:layout_scrollFlags="scroll|exitUntilCollapsed">
         <LinearLayout id="nested_fragment_host"/>
         <Toolbar/>
      </CollapsingToolbarLayout>
   <AppBarLayout>
   <NestedScrollView/>
</CoordinatorLayout>

AppBarLayout中的扩展区域是片段,我动态添加:

transaction.replace(R.id.nested_fragment_host, new MyFragment(), FRAGMENT_TAG);

总的来说,它工作正常,但如果用户在展开/折叠中间停止滚动,我需要自动展开/折叠AppBarLayout(所以当用户从屏幕移开手指时,AppBarLayout应该展开或折叠,否则中间国家允许)。 对于此任务,我决定使用自定义行为:

public class Behavior extends AppBarLayout.Behavior {
        private int cumulativeDy = 0;

        @Override
        public boolean onStartNestedScroll(@NonNull CoordinatorLayout parent, @NonNull AppBarLayout child, @NonNull View directTargetChild, @NonNull View target, int nestedScrollAxes) {
            boolean result = nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL;
            return result;
        }

        @Override
        public void onNestedScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull AppBarLayout child, @NonNull View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) {
            super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed);
            cumulativeDy += dyUnconsumed;
        }

        @Override
        public void onStopNestedScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull AppBarLayout abl, @NonNull View target) {
            super.onStopNestedScroll(coordinatorLayout, abl, target);

            if (cumulativeDy < 0) {
                abl.setExpanded(true, true);
            } else {
                abl.setExpanded(false, true);
            }
            cumulativeDy = 0;
        }
    }

这就是我的问题:当AppBarLayout崩溃并且用户开始扩展它时,它完全正常。但是在相反的情况下,当AppBarLayout被扩展时 - 如果用户在NestedScrollView上开始滚动手势 - 我的Behavior类被调用并且没有问题。但是,如果用户在AppBarLayout内容上开始滚动手势 - 未调用Behavior,但AppBarLayout开始对滚动手势做出反应。在这种情况下,如果用户在转换过程中停止滚动手势,我就无法检测到它。有人知道这种行为的原因吗?是否可以调用Behavior类?如果不是,如何确定用户是否在嵌套视图上滚动?

0 个答案:

没有答案