【问题标题】:Toolbar doesn't hide when scrolling滚动时工具栏不隐藏
【发布时间】:2019-01-11 16:26:51
【问题描述】:

我在 MainActivity 中有 3 个片段,它们都在显示,在 MainActivity 布局中,我在进度条下方有一个横幅广告(都放在底部),在顶部我还有工具栏、tablayout 和一个列表视图和我希望工具栏在向下滚动时隐藏, 这是完整的 activity_main 布局:

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/primary_color"
android:fitsSystemWindows="true"
android:orientation="vertical"
tools:context="com.example.android.miwok.MainActivity"
tools:openDrawer="start">



    <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"

            >

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        >

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/primary_color"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:layout_scrollFlags="scroll|enterAlways|snap"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light">

            <Button
                android:id="@+id/more_toolbar"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_gravity="end"
                android:layout_marginEnd="8dp"
                android:background="@drawable/thumbsuo" />
        </android.support.v7.widget.Toolbar>

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            style="@style/CategoryTab"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>


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


        <android.support.v4.view.ViewPager
            android:id="@+id/viewpager"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"

            />


            <ProgressBar
                android:id="@+id/progressBarParent"
                style="?android:attr/progressBarStyleHorizontal"
                android:layout_width="match_parent"
                android:layout_height="10dp"
                android:layout_marginBottom="2dp"

                />

            <com.google.android.gms.ads.AdView xmlns:ads="http://schemas.android.com/apk/res-auto"
                android:id="@+id/adView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                ads:adSize="SMART_BANNER"
                ads:adUnitId="ca-app-pub-3940256099942544/6300978111">

            </com.google.android.gms.ads.AdView>
        </LinearLayout>


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


<android.support.design.widget.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="@color/primary_color"
    app:headerLayout="@layout/nav_header"
    app:itemIconTint="@color/text_drawer_color"
    app:itemTextColor="@color/text_drawer_color"
    app:menu="@menu/drawer_menu" />

这是定义列表视图的片段的完整布局:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@color/tan_background"
>



<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    android:layout_margin="4dp"

    >

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentTop="true"
    android:background="@color/tan_background"
    android:divider="@color/tan_background"
    android:dividerHeight="8dp"
    android:drawSelectorOnTop="true"
    android:focusable="true"
    android:orientation="vertical"
    android:nestedScrollingEnabled="true"

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

这个问题有解决办法吗,谢谢你的帮助:)

【问题讨论】:

    标签: android android-studio listview fragment toolbar


    【解决方案1】:

    我认为你应该使用 collapsingToolbarLayout。

    1.将 app:layout_scrollFlags 添加到 CollapsingToolbarLayout

    app:layout_scrollFlags="scroll|enterAlways|snap"  //add this
    app:contentScrim="?attr/colorPrimary"
    app:expandedTitleMarginStart="48dp"
    app:expandedTitleMarginEnd="64dp"
    

    2.Next 事情把

    3.在您的 NestedScrollview 添加 android:fillViewport="true" 有时它会提供所需的输出。

    1.你没有将 app:layout_scrollFlags 添加到 CollapsingToolbarLayout

    app:layout_scrollFlags="scroll|enterAlways|snap"  //add this
    app:contentScrim="?attr/colorPrimary"
    app:expandedTitleMarginStart="48dp"
    app:expandedTitleMarginEnd="64dp"
    

    2.Next 事情把

    3.在您的 NestedScrollview 添加 android:fillViewport="true" 有时它会提供所需的输出。

    <android.support.v4.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fillViewport="true"
    

    编辑

    你也可以试试 CollapsingToolbarLayout

    app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed"
    

    对于工具栏

    app:layout_collapseMode="pin"
    app:layout_scrollFlags="scroll|exitUntilCollapsed"
    

    它会在滚动过程中固定工具栏..

    你也可以试试 CollapsingToolbarLayout

    app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed"
    

    对于工具栏

    app:layout_collapseMode="pin"
    app:layout_scrollFlags="scroll|exitUntilCollapsed"
    

    它会在滚动过程中固定工具栏。

    【讨论】:

    • 感谢您的回答,但我希望工具栏在滚动不固定时隐藏
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-25
    • 2016-05-13
    • 2023-03-17
    相关资源
    最近更新 更多