简体   繁体   English

在Android中滚动时隐藏工具栏的问题

[英]Issue to hide Toolbar when scrolling in android

I've an Activity with an NavigationDrawer and a Toolbar. 我有一个带有NavigationDrawer和工具栏的活动。 I'm facing a problem when i want disappear a toobar when one of child fragment view is scrolled. 我想在滚动子片段视图之一时消失太棒时遇到问题。 Everything works fine except that there is some view coming up from the bottom which has the exact same size as the toolbar that is disappearing. 一切正常,除了底部有一些视图,该视图的大小与消失的工具栏完全相同。

I made an GIF Animation that shows the problem. 我制作了一个GIF动画来显示问题。 Due to my reputation i can not post the image directly but this gif and this gif shows the problem 由于我的声誉,我无法直接发布图像,但是此gif此gif显示了问题

I tried to figure out where this is coming from. 我试图弄清楚这是从哪里来的。 It seems that its from my container FrameLayout where my fragment views are placed during runtime. 看来这是从运行时放置片段视图的容器FrameLayout中获得的。 I changed its background to green so i can identify it. 我将其背景更改为绿色,以便可以识别它。

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

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <include
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_scrollFlags="scroll|enterAlways"
        layout="@layout/toolbar" />

    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/darkGreen"
        tools:context="org.cddevlib.breathe.MainActivity" />
</LinearLayout>

<fragment
    android:id="@+id/navigation_drawer"
    android:name="org.cddevlib.breathe.NavigationDrawerFragment"
    android:layout_width="@dimen/navigation_drawer_width"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    tools:layout="@layout/fragment_navigation_drawer" /> </android.support.v4.widget.DrawerLayout>

This is the Toolbars Layout that is included 这是包含的工具栏布局

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
  android:id="@+id/toolbar"
  android:layout_width="match_parent"
  android:layout_height="?attr/actionBarSize"
  app:layout_collapseMode="pin"
  app:layout_scrollFlags="scroll|enterAlways"
  android:fitsSystemWindows="true"
  app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
</android.support.v7.widget.Toolbar>

And finally a snippet from the fragments view that is loaded: 最后是从片段视图加载的片段:

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

   <include
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_scrollFlags="scroll|enterAlways"
    layout="@layout/toolbar" />

    <android.support.v4.widget.NestedScrollView
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/scrollView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:background="@color/white"
        android:fillViewport="true" >

        (...)

Note that i'm including the Toolbar again in that layout because i want different Toolbars for each fragment in my application by hiding / adding the toolbar. 请注意,我再次在该布局中包含工具栏,因为我希望通过隐藏/添加工具栏为应用程序中的每个片段使用不同的工具栏。

    toolbar = (Toolbar) ((View) vw).findViewById(R.id.toolbar);
            if (toolbar != null) {
                // // for crate home button
                activity = (AppCompatActivity) getActivity();
                activity.getSupportActionBar().hide();
                toolbar.setBackgroundDrawable(new ColorDrawable((ColorUtils.getColorDark(DataModule.getInstance()
                        .getMainActivity()))));
                activity.setSupportActionBar(toolbar);
                activity.getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            }

SOLUTION

So thanks to sanat shukla answer is firgured out that my problem was that i used an AppBarLayout too much in my fragments view. 因此,感谢sanat shukla,我的问题是我的问题是我在片段视图中使用了过多的AppBarLayout。 In my fragments view, AppBarLayout was the main layout for all of my components, but it should not! 在我的片段视图中,AppBarLayout是我所有组件的主要布局,但事实并非如此! Its intended to hold the toolbar content! 它旨在容纳工具栏内容! Thanks 谢谢

It's not an issue. 这不是问题。 It is the cool animation and support provided by android. 这是android提供的很棒的动画和支持。 When you use co-ordinator layout with Toolbar then it shows cool animation when scrolling the list. 当您将协调器布局与工具栏一起使用时,滚动列表时它将显示很酷的动画。

The Design library takes this to the next level: using an AppBarLayout allows your Toolbar and other views (such as tabs provided by TabLayout) to react to scroll events in a sibling view marked with a ScrollingViewBehavior Design库将其带入了一个新的高度:使用AppBarLayout可以使您的工具栏和其他视图(例如TabLayout提供的选项卡)对标有ScrollingViewBehavior的同级视图中的滚动事件做出反应。

Read from here : http://android-developers.blogspot.in/2015/05/android-design-support-library.html 从这里阅读: http : //android-developers.blogspot.in/2015/05/android-design-support-library.html

If you don't want to hide your toolbar then don't use co-ordinator layout and appbar with toolbar. 如果您不想隐藏工具栏,则不要使用协调器布局和带有工具栏的应用栏。

Try this for fragment : 试试这个片段:

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

     <! -- Your Scrollable View -->
    <android.support.v4.widget.NestedScrollView
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/scrollView1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            android:background="@color/white"
            android:fillViewport="true" >
    <android.support.v4.widget.NestedScrollView/>
    <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    <include
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_scrollFlags="scroll|enterAlways"
            layout="@layout/toolbar" />
     </android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>

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

相关问题 工具栏下的GridView - 滚动时隐藏工具栏 - GridView under Toolbar - Hide ToolBar when Scrolling Android:向上滚动时显示工具栏(向上拖动),向下滚动时隐藏(向下拖动) - Android: Show toolbar when scrolling UP(Drag UP) and hide when scrolling down(Drag down) Android工具栏+标签布局+抽屉,滚动时隐藏工具栏并将TabLayout置于顶部 - Android Toolbar + Tab Layout + Drawer, Hide toolbar when scrolling and take TabLayout to the top 滚动片段工具栏应隐藏在android中 - While Scrolling Fragment toolbar should hide in android 滚动时Android工具栏高度 - Android toolbar elevation when scrolling 在 Fragment 上滚动时如何隐藏工具栏 - How to hide toolbar when scrolling on Fragment 滚动时工具栏不会隐藏 - Toolbar doesn't hide when scrolling WebView滚动时无法隐藏工具栏 - can`t hide toolbar when webview scrolling 当您停止在android中滚动listview / recyclerview时,如何隐藏或显示部分隐藏的工具栏/操作栏 - How to hide or show the partially hidden toolbar/actionbar when you stop scrolling the listview/recyclerview in android 滚动时隐藏工具栏 - hide Toolbar while scrolling
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM