通过在 Fragment 中滚动回收视图来隐藏 activity 中的工具栏?

Hide toolbar in activity by scrolling a recyclerview inside the Fragment?

我的ActivityXML是

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

我的片段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 所以我不能移动tabsactivity 因为只有这个特定的 fragment 才需要标签。而其他片段只需要一个toolbar。同时所有的片段都需要访问navigation drawer.

现在我想做的是以某种方式关联 app:layout_scrollFlags="scroll|enterAlways" 使用我的 activity 的工具栏。因此,每当我在 fragment 中滚动我的 RecyclerView 时,activity 中的 toolbar 就会被隐藏。

如果你们能指出正确的方向或帮助我弄清楚如何做到这一点,那就太好了?

如果我正确理解你的问题,你可以做的是

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

2) 仅在 ParentFragment 中包含制表符,其中包含其他片段 ViewPager

通过这种方法,您的工具栏和 NavigationDrawer 将在所有片段中可见,而选项卡仅在 ParentFragment

中可见

希望对您有所帮助

我已经解决了这个问题,尽管还有一些小的改进空间,比如 FAB 正在移动 updown 每当我在 Fragment

中滚动 RecyclerView

为了让 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>

我也将我的片段 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>

也运行成这个问题。但是找到了解决办法。 xml返工的解决方案不能,所以你需要以编程方式进行。我们只需要 Inflater。是的,是的,就这么简单。即时更改 xml 以工作所有滚动。 Inflate/AddView/RemoveView - 这就是您所需要的!