简体   繁体   中英

Android - Using CoordinatorLayout in Activity and in Fragment

So I am using CoordinatorLayout in the Activity like..

<CoordinatorLayout>
    <AppBarLayout>
        <Toolbar/>
    </AppBarLayout>
    <FrameLayout> // For Fragments
</CoordinatorLayout>

The Toolbar uses scroll|enterAlways as Scrollflags .

In one specific Fragment, I want to achieve a Parallax effect on a layout and I use CoordinatorLayout again, but this causes the Toolbar to not behaving normally.

Here's how I use it in the Fragment 's layout :

<CoordinatorLayout>
    <AppBarLayout>
        <CollapsingToolbarLayout>
            <Toolbar/>
        </CollapsingToolbarLayout>
    </AppbarLayout>

    <RelativeLayout/> // Contains the Fragment's content
</CoordinatorLayout>

How to achieve both effects? Is it not possible because Fragment 's CoordinatorLayout intercepts the event?

粗糙的布局

So the toolbar is hidden on scroll, and I need the Search Functionalities to be hidden using parallax effect on scroll

I didnt try it, but this gist seems teorically a right approach, as it adds to a CoordinatorLayout a scrollingchild ability to dispatch scrolling to up too.

Update:

Tried removing the method offsetChildToAnchor , as it is not visible outside of the package.

Then found a forked gist that fixes an issue on scrolling management, and sets NestedScrollingEnabled as true.

Added app:layout_behavior="@string/appbar_scrolling_view_behavior" on the nested CoordinatorLayout.

Tested nesting two CoordinatorLayouts on an activity without fragment. It works. And should work with a fragment too.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM