簡體   English   中英

工具欄不會隱藏在DrawerLayout中的Scrolling recyclerview上

[英]Toolbar doesn't hide on Scrolling recyclerview in DrawerLayout

你好,當我滾動片段中的recyclerview時,我試圖隱藏工具欄,我的布局也有一個drawerlayout和tablayout。 我正在嘗試僅隱藏工具欄並向上移動選項卡欄。

這是我的activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:fitsSystemWindows="true"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="#e9e7e7"
    tools:context=".MainActivity">



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

        <android.support.v4.view.ViewPager
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            android:id="@+id/viewPager"
            >

        </android.support.v4.view.ViewPager>



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

            <android.support.design.widget.CollapsingToolbarLayout
                android:id="@+id/ctb_dashboard"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                app:contentScrim="@color/white"
                app:layout_scrollFlags="scroll|exitUntilCollapsed">


                <android.support.v7.widget.Toolbar
                    xmlns:android="http://schemas.android.com/apk/res/android"
                    xmlns:app="http://schemas.android.com/apk/res-auto"
                    android:layout_width="match_parent"
                    android:background="@color/colorPrimary"
                    android:minHeight="?attr/actionBarSize"
                    android:id="@+id/toolBar"
                    app:layout_scrollFlags="scroll|enterAlways"
                    android:layout_height="wrap_content"
                    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                    app:theme="@style/ThemeOverlay.AppCompat.Dark"
                    app:layout_collapseMode="pin">

                </android.support.v7.widget.Toolbar>

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

            <android.support.design.widget.TabLayout
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:id="@+id/tabLayout"
                app:tabMode="fixed"
                app:tabSelectedTextColor="@color/white"
                app:tabTextColor="@color/colorTab"
                app:tabTextAppearance="@style/MyTabStyle"
                app:tabIndicatorColor="@android:color/white"
                app:tabIndicatorHeight="3dp"
                app:tabGravity="fill">
            </android.support.design.widget.TabLayout>


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


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



    <android.support.design.widget.NavigationView
        app:headerLayout="@layout/header"
        app:itemTextAppearance="@style/NavDrawerTextStyle"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:id="@+id/nav_view"
        android:background="#d2d2d2"
        app:itemTextColor="#6c6c6c"
        app:itemIconTint="#6c6c6c"
        app:menu="@menu/drawermenu"
        android:layout_gravity="start">
    </android.support.design.widget.NavigationView>




</android.support.v4.widget.DrawerLayout>

fragment_home.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".HomeFragment">

    <android.support.v7.widget.RecyclerView
        android:layout_marginTop="100dp"
        android:id="@+id/blog_listt2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

    </android.support.v7.widget.RecyclerView>

    <ProgressBar
        android:id="@+id/progressBar2"
        style="?android:attr/progressBarStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical|center_horizontal"
        android:indeterminateTint="@color/colorPrimaryDark"
        android:visibility="visible"/>

</FrameLayout>

我已經嘗試了在堆棧溢出中找到的大多數答案,但仍然無法完成。 難道我做錯了什么? 有人可以幫我解決這個問題,嘗試1周完成這項工作嗎?

您能否詳細說明出現了什么問題? 我在這里看到的最大問題是,在CollapsingToolbarLayout的layout_scrollFlags上設置了exitUntilCollapsed標志。 如果您無法使工具欄滾動到屏幕之外,那就是罪魁禍首。 您的AppBarLayout需要這樣設置:

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary">

    <android.support.design.widget.CollapsingToolbarLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:contentScrim="?attr/colorPrimary"
        app:titleEnabled="false"
        app:layout_scrollFlags="scroll">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_collapseMode="pin" />
    </android.support.design.widget.CollapsingToolbarLayout>

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

尚無法嵌入圖像。 單擊此處獲取gif。

另外,如果您只需要在屏幕外滾動工具欄,則不需要CollapsingToolbarLayout。 您可以使用帶有app:layout_scrollFlags="scroll"的工具欄,如下所示:

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary">
    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        app:layout_scrollFlags="scroll"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

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

這是我找到的用於解釋AppBarLayout和CollapsingToolbarLayout的所有不同滾動標記的最佳資源: http ://saulmm.github.io/mastering-coordinator

acitivity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:fitsSystemWindows="true"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="#e9e7e7"
    tools:context=".MainActivity">

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

        <android.support.v4.view.ViewPager
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            android:id="@+id/viewPager"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            >

        </android.support.v4.view.ViewPager>


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

                <android.support.v7.widget.Toolbar
                    xmlns:android="http://schemas.android.com/apk/res/android"
                    xmlns:app="http://schemas.android.com/apk/res-auto"
                    android:layout_width="match_parent"
                    android:background="@color/colorPrimary"
                    android:minHeight="?attr/actionBarSize"
                    android:id="@+id/toolBar"
                    app:layout_scrollFlags="scroll|enterAlways"
                    android:layout_height="wrap_content"
                    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                    app:theme="@style/ThemeOverlay.AppCompat.Dark"
                    app:layout_collapseMode="pin">

                </android.support.v7.widget.Toolbar>

            <android.support.design.widget.TabLayout
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:id="@+id/tabLayout"
                app:tabSelectedTextColor="@color/white"
                app:tabTextColor="@color/colorTab"
                app:tabTextAppearance="@style/MyTabStyle"
                app:tabIndicatorColor="@android:color/white"
                app:tabIndicatorHeight="3dp"
                app:tabMode="fixed"
                app:tabGravity="fill">
            </android.support.design.widget.TabLayout>


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

    <android.support.design.widget.NavigationView
        app:headerLayout="@layout/header"
        app:itemTextAppearance="@style/NavDrawerTextStyle"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:id="@+id/nav_view"
        android:background="#d2d2d2"
        app:itemTextColor="#6c6c6c"
        app:itemIconTint="#6c6c6c"
        app:menu="@menu/drawermenu"
        android:layout_gravity="start">
    </android.support.design.widget.NavigationView>


</android.support.v4.widget.DrawerLayout>

暫無
暫無

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

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