android - 是否可以在 AppBarLayout 中折叠多个 View ?

标签 android android-coordinatorlayout coordinator-layout

我有一个带有 CollapsingToolbarLayout 的 AppBarLayout,下面有两个 View ,屏幕其余部分有 ViewPager

<Coordinator>

    <AppBarLayout>
        // Toolbar stuff inside
        <CollapsingToolbarLayout app:layout_scrollFlags="scroll|exitUntilCollapsed"/>

        // Should collapse by design
        <FirstButtonView app:layout_scrollFlags="scroll" />

        <TabLayout app:layout_scrollFlags="scroll|exitUntilCollapsed">

    </AppBarLayout>

    <ViewPager app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

</Coordinator>

使用此布局,它按预期折叠了工具栏,但两个 View 都固定在顶部

正如我在 AppBarLayout 类中看到的那样,当它计算可能的滚动时,它遍历 subview 直到遇到标志 SCROLL_FLAG_EXIT_UNTIL_COLLAPSED=2,然后中断循环

public final int getTotalScrollRange() {
        if (this.totalScrollRange != -1) {
            return this.totalScrollRange;
        } else {
            int range = 0;
            int i = 0;

            for(int z = this.getChildCount(); i < z; ++i) {
                View child = this.getChildAt(i);
                AppBarLayout.LayoutParams lp = (AppBarLayout.LayoutParams)child.getLayoutParams();
                int childHeight = child.getMeasuredHeight();
                int flags = lp.scrollFlags;
                if ((flags & 1) == 0) {
                    break;
                }

                range += childHeight + lp.topMargin + lp.bottomMargin;
                if ((flags & 2) != 0) {
                    range -= ViewCompat.getMinimumHeight(child);
                    break;
                }
            }

            return this.totalScrollRange = Math.max(0, range - this.getTopInset());
        }
    }

是否有可能以某种方式解决它? Target design

最佳答案

基于关于使用“SCROLL_FLAG_SCROLL”的文档,“如果在此 View 之前的任何同级 View 没有此标志,则此值无效。

因此,相同的标志必须与其他 sibling 一起使用。

关于android - 是否可以在 AppBarLayout 中折叠多个 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58258371/

相关文章:

android - 后端与托管区别?

android - react-native 获取当前日期

android - Kotlin 协程使用 mockito 进行单元测试

android - 如何解决 LinearLayout.LayoutParams 与 CoordinatorLayout.LayoutParams 的 ClassCastException

java - AppBarLayout 不允许其他 View 进行 match_parent

android - float 操作按钮不会出现在 < 5 android 版本的设备上

java - 可以使用 xml 属性 onClick ="click"更改按钮文本,而无需创建按钮对象

android - 修复 CoordinatorLayout 中的底栏

android - CoordinatorLayout (CL) : NSV Not at Top When Loaded 中的 NestedScrollView (NSV)

java - 设计库 - 带有 GridView/listView 的 CoordinatorLayout/CollapsingToolbarLayout