简体   繁体   English

滚动时工具栏不会隐藏

[英]Toolbar doesn't hide when scrolling

I have 3 Fragments in the MainActivity where they are all showing, in the MainActivity layout I have a Banner Ad below a progress bar (both placed at the bottom), In the top I have also toolbar,tablayout and a listview and I want the toolbar to hide when scrolling down , this is the full activity_main layout : 我在MainActivity中有3个片段,它们都在其中显示,在MainActivity布局中,进度栏下方有一个横幅广告(均位于底部),顶部还有工具栏,布局和列表视图,我想要向下滚动时隐藏的工具栏,这是完整的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" />

And this the full layout of the fragment where the listview is defined : 这是定义listview的片段的完整布局:

    <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>

is there any solution for this problem , Thanks for your help :) 有没有解决这个问题的方法,谢谢您的帮助:)

I think you should use collapsingToolbarLayout. 我认为您应该使用collapsingToolbarLayout。

1.Add app:layout_scrollFlags to the 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 thing put the 2.接下来的事情

3.In your NestedScrollview add android:fillViewport="true" sometimes it gives the desired output. 3.在您的NestedScrollview中添加android:fillViewport =“ true”有时它会提供所需的输出。

1.you didn't add app:layout_scrollFlags to the 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 thing put the 2.接下来的事情

3.In your NestedScrollview add android:fillViewport="true" sometimes it gives the desired output. 3.在您的NestedScrollview中添加android:fillViewport =“ true”有时它会提供所需的输出。

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

EDIT 编辑

you can also try for CollapsingToolbarLayout 您也可以尝试使用CollapsingToolbarLayout

app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed"

and for Toolbar 和工具栏

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

it will pin the toolbar during scrolling.. 它将在滚动过程中固定在工具栏上。

you can also try for CollapsingToolbarLayout 您也可以尝试使用CollapsingToolbarLayout

app:layout_scrollFlags="scroll|enterAlways|enterAlwaysCollapsed"

and for Toolbar 和工具栏

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

it will pin the toolbar during scrolling. 它将在滚动过程中固定在工具栏上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM