簡體   English   中英

在嵌套片段中滾動RecyclerView時隱藏工具欄

[英]Hiding Toolbar when Scrolling a RecyclerView in nested Fragment

編輯2; 這部分代碼還可以。 檢查我的答案,您可能有類似的問題,希望這可以解決您的問題。


我一直在檢查很多類似問題的帖子。 他們讓我離我很近,但我做對了。 我有一個MainActivity ,它承載在FrameLayout加載的Fragmentid: fragment_container )。 我想在滾動時隱藏Toolbar ,但這僅在滾動NavigationDrawer時發生。 當我滾動RecyclerViewToolbar不會隱藏。

Fragment包含一個ViewPager ,每個ViewPagerFragment都有一個RecyclerView

我嘗試了很多替代方法,而這是最接近我預期的方法。

我想做的事:

  • 滾動回收者視圖時隱藏工具欄

現在發生了什么:

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

我當前的main_activity布局:

<?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" />

編輯; 由於某些原因,CoordinatorLayout沒有顯示在我剛剛發布的代碼中。 一切都被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">

以前的代碼在這里。

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

我的問題在Fragment 我有這行殺死了效果:

recyclerView.setNestedScrollingEnabled(false);

我刪除了它,現在效果很好。 我已經處理了這么長時間,希望早點得到它。 希望這可以幫助某人。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM