简体   繁体   中英

Hiding Toolbar when Scrolling a RecyclerView in nested Fragment

EDIT 2; This part of the code was OK. Check my answer for this, you might have a similar issue and I hope this solves your issue.


I've been checking a lot of posts with similar issues; they got me close, but I'm not able to make it right. I have a MainActivity , that hosts a Fragment loaded in the FrameLayout ( id: fragment_container ). I want to hide the Toolbar when scrolling, but this only happens when I scroll the NavigationDrawer . When I scroll the RecyclerView , the Toolbar doesn't hide.

The Fragment contains a ViewPager , and each ViewPagerFragment has a RecyclerView .

I tried a lot of alternatives and this is the one that is closest to what I intend.

What I want to do:

  • hide toolbar when scrolling recycler view

What's going on right now:

http://gifmaker.cc/PlayGIFAnimation.php?folder=2016080211dGOklkBglH6HPrbVKq2rLE&file=output_lp5KmP.gif

My current main_activity layout:

<?xml version="1.0" encoding="utf-8"?>

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

    <android.support.v7.widget.Toolbar
        android:id="@+id/my_toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:elevation="16dp"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:layout_scrollFlags="scroll|enterAlways">

        <Button
            android:id="@+id/login_button"
            android:layout_width="24dp"
            android:layout_height="24dp"
            android:layout_gravity="end"
            android:layout_marginEnd="16dp"
            android:layout_marginRight="16dp"
            android:background="@drawable/ic_account_circle_white_24dp" />

        <Button
            android:id="@+id/favourites_button"
            android:layout_width="24dp"
            android:layout_height="24dp"
            android:layout_gravity="end"
            android:layout_marginEnd="16dp"
            android:layout_marginRight="16dp"
            android:background="@drawable/favourite_button" />

        <Button
            android:id="@+id/bookmarked_button"
            android:layout_width="24dp"
            android:layout_height="24dp"
            android:layout_gravity="end"
            android:layout_marginEnd="16dp"
            android:layout_marginRight="16dp"
            android:background="@drawable/bookmarked_button" />

        <Button
            android:id="@+id/history_button"
            android:layout_width="24dp"
            android:layout_height="24dp"
            android:layout_gravity="end"
            android:layout_marginEnd="16dp"
            android:layout_marginRight="16dp"
            android:background="@drawable/ic_history_white_24dp" />
    </android.support.v7.widget.Toolbar>

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


<android.support.v4.widget.DrawerLayout
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <android.support.design.widget.NavigationView
        android:id="@+id/navigationView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="start"
        app:menu="@menu/menu" />
</android.support.v4.widget.DrawerLayout>


<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab_pressed"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="16dp"
    android:src="@drawable/favourite_button"
    app:fabSize="mini"
    app:layout_anchor="@id/fragment_container"
    app:layout_anchorGravity="bottom|right|end" />

EDIT; for some reason the CoordinatorLayout is not being displayed in the code I just posted. Everything is surrounded with a CoordinatorLayout!

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">

Previous Code goes here.

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

My issue was inside the Fragment . I had this line that was killing the effect:

recyclerView.setNestedScrollingEnabled(false);

I removed it and now it works great. I've been dealing with this for so long, wish I got it earlier. Hope this helps someone out there.

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