Namolem
Namolem

Reputation: 63

Is it possible to collapse multiple views in AppBarLayout?

I have an AppBarLayout with CollapsingToolbarLayout, two views below and ViewPager at the rest of the screen

<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>

With this layout it collapsed toolbar as expected, but both views are being pinned to the top

As i see in AppBarLayout class, when it calculates possible scroll, it iterates through child views until meet flag SCROLL_FLAG_EXIT_UNTIL_COLLAPSED=2, then breaks the loop

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

Is it possible somehow to solve it? Target design

Upvotes: 5

Views: 775

Answers (1)

Srikanth P
Srikanth P

Reputation: 1516

Based on Documentation on using "SCROLL_FLAG_SCROLL", "If any sibling views before this one do not have this flag, then this value has no effect."

Hence the same flag has to be used with other siblings.

Upvotes: 0

Related Questions