简体   繁体   English

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

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

My Activity XML is 我的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 is : 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>

My fragment uses tabs and nested fragments inside the ViewPager and my activity uses navigation drawer so i can't move the tabs to activity as the tabs are only needed for this specific fragment . 我的fragmentViewPager使用tabsnested fragments ,我的activity使用navigation drawer所以我无法将tabs移动到activity因为只有这个特定fragment需要制表符。 And other fragments just need a toolbar . 而其他片段只需要一个toolbar At the same time all the fragments need to access the navigation drawer . 同时,所有片段都需要访问navigation drawer

Now what i want to do is somehow associate app:layout_scrollFlags="scroll|enterAlways" with my activity's toolbar. 现在我要做的是以某种方式关联app:layout_scrollFlags="scroll|enterAlways"和我的活动工具栏。 So that whenever i scroll my RecyclerView inside the fragment the toolbar in the activity gets hidden. 因此,每当我在fragment内滚动我的RecyclerView时,活动中的toolbar就会被隐藏。

It would be great if you guys can point me in the right direction or help me figure out how to do this ? 如果你们能指出我正确的方向或帮助我弄清楚如何做到这一点会很棒吗?

If I understand your question correctly what you can do is 如果我正确理解你的问题,你可以做的是

1) Include the Toolbar and NavigationDrawer in MainActivity so it will be accessible in all fragments too. 1)在MainActivity包含工具栏和NavigationDrawer ,以便它也可以在所有片段中访问。

2) Include tabs in in only ParentFragment which contains ViewPager for other fragments 2)仅在ParentFragment中包含选项卡,其中包含用于其他片段的ViewPager

with this approach your Toolbar and NavigationDrawer will be visible in all fragments while tabs only in ParentFragment 使用此方法,您的工具栏和NavigationDrawer将在所有片段中可见,而选项卡仅在ParentFragment

Hope it helps 希望能帮助到你

I have solved this issue, though there is still room for some minor improvements like the FAB is moving up and down whenever i scroll RecyclerView inside the Fragment 我已经解决了这个问题,虽然还是有余地的一些小的改进,如FAB上下 移动 ,每当我滚动RecyclerView内部Fragment

In order to make toolbar respond to app:layout_scrollFlags="scroll|enterAlways" this is what i did 为了使toolbar响应app:layout_scrollFlags="scroll|enterAlways"这就是我所做的

I changed my Activity XML to: 我将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>

Also i changed my Fragment XML to: 我还将我的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>

Also ran into this problem. 也碰到了这个问题。 But a solution was found. 但是找到了解决方案。 The solution with xml rework can not be, so you need to do it programmatically. 使用xml返工的解决方案不可能,因此您需要以编程方式执行此操作。 All we need is Inflater . 我们需要的只是Inflater Yes, yes, that's so simple. 是的,是的,那很简单。 On the fly change xml to work all scrolling. 在飞行中更改xml以工作所有滚动。 Inflate/AddView/RemoveView - that's all you need! Inflate / AddView / RemoveView - 这就是你所需要的!

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

相关问题 在片段内而不是活动内使用recyclerview时,在滚动条上隐藏工具栏 - Hide toolbar on scroll when the using a recyclerview inside a Fragment instead of an Activity 滚动“活动工具栏”,在屏幕外滚动片段中的Recyclerview - Scroll Activity Toolbar off screen scrolling Recyclerview in fragment 如果我向下滚动片段中的recyclerView,如何自动隐藏工具栏? - How to hide toolbar automatically if I scroll down the recyclerView inside a fragment? 使用CoordinatorLayout隐藏工具栏,但在片段上使用RecyclerView - Hide Toolbar with CoordinatorLayout, but RecyclerView on a fragment 在嵌套片段中滚动RecyclerView时隐藏工具栏 - Hiding Toolbar when Scrolling a RecyclerView in nested Fragment 片段内的水平recyclerview不滚动 - Horizontal recyclerview inside fragment not scrolling 工具栏不会隐藏在DrawerLayout中的Scrolling recyclerview上 - Toolbar doesn't hide on Scrolling recyclerview in DrawerLayout 隐藏带有协调器布局和RecyclerView的工具栏 - Hide Toolbar with Coordinator layout and RecyclerView in a fragment 如何使用固定的CardView和RecyclerView将工具栏隐藏在片段中 - How to hide the Toolbar in a fragment with a fixed CardView and a RecyclerView 无法在滚动片段布局上隐藏工具栏 - Not able to hide toolbar on scrolling Fragment layout
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM