在Fragment中通过滚动RecyclerView来隐藏活动中的工具栏?

7

我的活动XML文件如下:

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

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

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

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="@color/colorPrimary"
                android:title="@string/app_name"
                app:layout_scrollFlags="scroll|enterAlways"/>
        </android.support.design.widget.AppBarLayout>

        <FrameLayout
            android:id="@+id/fragmentContainer"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            />

    </LinearLayout>


    <android.support.design.widget.NavigationView
        android:id="@+id/drawerNavigationView"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:itemTextColor="@color/navview_text_color"
        app:menu="@menu/home_drawer_items_menu"/>
</android.support.v4.widget.DrawerLayout>

我的Fragment XML代码如下:

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

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

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_scrollFlags="scroll|enterAlways"/>

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

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

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/fab_margin"
        android:clickable="true"
        android:src="@drawable/ic_plus"
        android:tint="@android:color/white"
        app:elevation="5dp"
        app:layout_anchor="@id/viewPager"
        app:layout_anchorGravity="bottom|right|end"/>
</android.support.design.widget.CoordinatorLayout>

我的fragmentViewPager中使用tabsnested fragments,而我的activity使用navigation drawer,所以我无法将tabs移到activity中,因为这些标签仅针对特定的fragment需要。其他片段只需要一个toolbar即可。同时,所有片段都需要访问navigation drawer

现在我想要做的是,以某种方式将app:layout_scrollFlags="scroll|enterAlways"与我的活动工具栏相关联。这样,每当我在fragment内滚动我的RecyclerView时,活动中的toolbar就会隐藏起来。

如果您能指点我方向或帮助我弄清楚如何实现这一点,那将是很棒的。


1
这个库可能会对你有所帮助:https://github.com/ksoichiro/Android-ObservableScrollView - Ben-J
@Ben-J 谢谢您的库,它似乎是一个很好的解决方法,但仍然缺少使用 app:layout_scrollFlags="scroll|enterAlways" 时发生的动画。 - Sheraz Ahmad Khilji
3个回答

0

如果我正确理解了你的问题,你可以做以下几点:

1)在MainActivity中包含工具栏和NavigationDrawer,这样所有片段都可以访问。

2)仅在包含其他片段的ViewPagerParentFragment中包含选项卡。

使用此方法,您的工具栏和NavigationDrawer将在所有片段中可见,而选项卡仅在ParentFragment中可见。

希望有所帮助。


你所说的我已经做了。请再次阅读问题。我想在滚动Fragment中的RecyclerView时隐藏工具栏。 - Sheraz Ahmad Khilji
为什么不使用onScrollListener - Atiq
我现在正在使用它,但仍然无法像 app:layout_scrollFlags="scroll|enterAlways" 一样给我流畅的动画效果。 - Sheraz Ahmad Khilji
你尝试过内置动画 android:animateLayoutChanges="true" 吗?我用它来隐藏和显示 AppbarLayout,效果很不错。 - Atiq
是的,我尝试过那样做,但界面会出现抖动。不像正常情况下那么流畅。 - Sheraz Ahmad Khilji

0

我已经解决了这个问题,尽管仍有些微小的改进空间,例如每当我滚动Fragment内部的RecyclerView时,FAB会向移动。

为了使toolbar响应app:layout_scrollFlags="scroll|enterAlways",我所做的是:

我更改了我的 Activity XML 为:

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

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

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="@color/colorPrimary"
                android:title="@string/app_name"
                app:layout_scrollFlags="scroll|enterAlways"/>
        </android.support.design.widget.AppBarLayout>

        <FrameLayout
            android:id="@+id/fragmentContainer"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            />
    </android.support.design.widget.CoordinatorLayout>


    <android.support.design.widget.NavigationView
        android:id="@+id/drawerNavigationView"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:itemTextColor="@color/navview_text_color"
        app:menu="@menu/home_drawer_items_menu"/>
</android.support.v4.widget.DrawerLayout>

我也将我的Fragment XML更改为:

<LinearLayout
    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"
    android:orientation="vertical">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0">

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

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

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1">

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

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_marginBottom="60dp"
            android:layout_marginEnd="20dp"
            android:layout_marginRight="20dp"
            android:clickable="true"
            android:src="@drawable/ic_plus"
            android:tint="@android:color/white"
            app:elevation="5dp"/>
    </RelativeLayout>
</LinearLayout>

将app:layout_scrollFlags="scroll|enterAlways"添加到容器布局中,会使容器向下滑动,因此任何位于片段内底部的视图将不再可见 ;( - Sjd

-2

我也遇到了这个问题。但是找到了解决方案。使用xml重构的解决方案行不通,所以需要通过编程来实现。我们只需要Inflater。是的,就是这么简单。动态将xml转换为可滚动的工作即可。 Inflate / AddView / RemoveView - 这就是你需要的全部!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接