简体   繁体   中英

AppBarLayout hiding toolbar on scroll behavior, switching fragments

I've implemented a CoordinatorLayout and wrapped my Toolbar inside an AppBarLayout so that the toolbar hides itself when scrolling. Most of the app consists of Fragments that are swapped in and out of the @id/container FrameLayout as seen below. Some of the Fragments are just RecyclerViews , while others are other layouts. I've added app:layout_behavior="@string/appbar_scrolling_view_behavior" to the @id/container FrameLayout. Heres my main layout:

<DrawerLayout>

     <RelativeLayout
        android:id="@+id/mainRelativeLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >

        <android.support.design.widget.CoordinatorLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"  
            >

            <android.support.design.widget.AppBarLayout
                android:id="@+id/appBarLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                >

                <Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    android:elevation="5dp"
                    app:layout_scrollFlags="scroll|enterAlways"
                    >

                </Toolbar>

            </android.support.design.widget.AppBarLayout>

            <FrameLayout
                android:id="@+id/container"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:layout_behavior="@string/appbar_scrolling_view_behavior"
                />

        </android.support.design.widget.CoordinatorLayout>

    </RelativeLayout>

<!-- ignore -->
<drawercontents>
</DrawerLayout>

Now, the Toolbar hides itself when scrolling on Fragments with a RecyclerView .

The issue is, when switching to another fragment that doesnt have a RecyclerView , it will mess up that fragments layout. For example, if I scroll down on a fragment that containers a RecyclerView , the Toolbar will hide itself (like it should). Then, if I switch to the other fragment without the RecyclerView , the toolbar will still be hidden, and the layout will expand into that extra space. If I start back at a fragment with a RecyclerView and dont scroll (so the Toolbar stays visible), and then switch to another fragment, the content is now pushed down off the screen because of the Toolbar .

Heres a screenshot of the issue: http://prnt.sc/a7majo

I also tried disabling the scroll behavior in that fragment using the following code:

Toolbar mToolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);

AppBarLayout.LayoutParams toolbarLayoutParams = (AppBarLayout.LayoutParams) mToolbar.getLayoutParams();
toolbarLayoutParams.setScrollFlags(0);
mToolbar.setLayoutParams(toolbarLayoutParams);

AppBarLayout appBarLayout = (AppBarLayout) getActivity().findViewById(R.id.appBarLayout);

CoordinatorLayout.LayoutParams appBarLayoutParams = (CoordinatorLayout.LayoutParams) appBarLayout.getLayoutParams();
appBarLayoutParams.setBehavior(null);
appBarLayout.setLayoutParams(appBarLayoutParams);

This doesnt work either an produces the odd result pictured at the bottom here: http://prnt.sc/a7mapg

So whats the workaround for this? I've seen a few other questions that were similar to this but the answers given did not seem to work for my situation. Thanks in advance!

protected void setScrollingEnabled(boolean isEnabled) {
    AppBarLayout.LayoutParams params = (AppBarLayout.LayoutParams) toolbar.getLayoutParams();
    params.setScrollFlags(isEnabled ? (AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL | AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS) : 0);
}

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