繁体   English   中英

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

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

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

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

My 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)在MainActivity包含工具栏和NavigationDrawer ,以便它也可以在所有片段中访问。

2)仅在ParentFragment中包含选项卡,其中包含用于其他片段的ViewPager

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

希望能帮助到你

我已经解决了这个问题,虽然还是有余地的一些小的改进,如FAB上下 移动 ,每当我滚动RecyclerView内部Fragment

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

也碰到了这个问题。 但是找到了解决方案。 使用xml返工的解决方案不可能,因此您需要以编程方式执行此操作。 我们需要的只是Inflater 是的,是的,那很简单。 在飞行中更改xml以工作所有滚动。 Inflate / AddView / RemoveView - 这就是你所需要的!

暂无
暂无

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

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