简体   繁体   English

隐藏工具栏滚动与片段内的recyclerview

[英]Hiding Toolbar on scroll with recyclerview inside fragment

I'm trying to get the toolbar to collapse on scroll when a recyclerview inside a fragment is scrolled. 当滚动片段内的recyclerview时,我正试图让工具栏在滚动时折叠。 To start, heres my main layout: 首先,继承我的主要布局:

<DrawerLayout>

     <RelativeLayout
        android:id="@+id/mainRelativeLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >

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

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

                <Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    android:elevation="5dp"
                    app:layout_scrollFlags="scroll|enterAlways"
                    >

                </Toolbar>

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

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

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

    </RelativeLayout>

<!-- ignore -->
<drawercontents>
</DrawerLayout>

So as you can probably guess my fragments are being loaded into @id/container . 因此你可能猜到我的片段被加载到@id/container My first fragment contains the recyclerview, and I set app:layout_behavior="@string/appbar_scrolling_view_behavior" on that recyclerview. 我的第一个片段包含recyclerview,我在该recyclerview上设置app:layout_behavior="@string/appbar_scrolling_view_behavior" This does work, and the toolbar collapses on scroll. 这确实有效,工具栏在滚动时折叠。 The issue is the toolbar covers the top contents of the fragment when its not collapsed. 问题是工具栏覆盖了片段未折叠时的顶部内容。 Adding a top margin to the fragment container equal to the size of the toolbar just causes a blank space to be left when the toolbar collapses (obviously). 在片段容器中添加一个等于工具栏大小的上边距只会在工具栏崩溃时(显然)导致留下空白。

Whats missing here? 什么不见​​了? Any ideas? 有任何想法吗?

EDIT: As requested, here is the layout for the fragment containing the recyclerview: 编辑:根据要求,这是包含recyclerview的片段的布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView
    android:id="@+id/feed"
    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:background="#00000000"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
<?xml version="1.0" encoding="utf-8"?>
<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/tabanim_appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.v7.widget.Toolbar
            android:id="@+id/tabanim_toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways|snap"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

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

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

Haven't tested this yet, but this is what you must do: 尚未测试过,但这是你必须做的:

  • Move the app:layout_behavior="@string/appbar_scrolling_view_behavior" to the FrameLayout with id container . app:layout_behavior="@string/appbar_scrolling_view_behavior"到带有id containerFrameLayout
  • Add android:fitsSystemWindows = "true" to your AppBarLayout . android:fitsSystemWindows = "true"AppBarLayout

This is exactly what I'm doing in my app, and it works. 这正是我在我的应用程序中所做的,它的工作原理。 If this doesn't work for you, try cleaning (on Android Studio, Build -> Clean Project ) your project and running the app again. 如果这对您不起作用,请尝试清理(在Android Studio上, 构建 - >清理项目 )您的项目并再次运行该应用程序。

My friend i did this today and working perfect, i have 2 fragments both have RecyclerView and can scroll Toolbar . 我的朋友今天我做了这个并且工作完美,我有2个片段都有RecyclerView并且可以滚动Toolbar I looked chrisbanes's CheeseSquare : CheeseSquare 我看了chrisbanes's CheeseSquareCheeseSquare

If you take a look at MainActivity , there is 3 fragments in TabLayout and all of them can scroll Toolbar . 如果你看看MainActivityTabLayout有3个片段,所有片段都可以滚动Toolbar So i prepared this layout , maybe you will take a look at: 所以我准备了这个布局,也许你会看看:

my activity_home.xml : 我的activity_home.xml

<?xml version="1.0" encoding="utf-8"?>
<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">

    <include layout="@layout/content_home"/>

    <android.support.design.widget.NavigationView
        android:id="@+id/navigation_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        android:layout_gravity="start"
        app:menu="@menu/homepage_leftdrawer">


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

</android.support.v4.widget.DrawerLayout>

And content_home.xml : content_home.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/root"
    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:theme="@style/AppTheme3.AppBarOverlay">

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


<android.support.design.widget.TabLayout
    android:id="@+id/tabs"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:tabTextColor="@color/Transparent_few_white"
    app:tabIndicatorColor="@color/White"
    app:tabTextAppearance="@style/TabLayoutTextStyle"
    app:tabIndicatorHeight="3dp"
    app:tabSelectedTextColor="@color/White"
    android:background="@color/ColorPrimary"
    app:tabPaddingStart="0dp"
    app:tabPaddingEnd="0dp"/>

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

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

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

Where is the key here : 关键在哪里:

If you take a look at my ViewPager , it has app:layout_behavior="@string/appbar_scrolling_view_behavior" attribute Then thats it. 如果你看看我的ViewPager ,它有app:layout_behavior="@string/appbar_scrolling_view_behavior"属性然后就是这样。 Dont add any other behavior in fragment . 不要在片段中添加任何其他行为。 Just try like that and come with result please ! 试试这样,请结果!

You need something similar to the play store app. 你需要类似于Play商店应用的东西。 Pasting the layouts here. 在这里粘贴布局。 The code for this is easy and you can code it by yourself. 这个代码很简单,您可以自己编写代码。

  1. FrameLayout 的FrameLayout
    • ImageView ImageView的
    • ScrollView 滚动型
      1. LinearLayout 的LinearLayout
      2. HorizontalScrollView Horizo​​ntalScrollView
        • LinearLayout 的LinearLayout
    • ToolBar 工具栏

With the above layout hierarchy you can achieve something like this: 使用上面的布局层次结构,您可以实现以下目标:

在此输入图像描述

将一个布局名称设置为toolbar.xml并将其包含在您要使用的代码中,但提醒一件事请不要使用其他任何布局,如Relativelayout,Linearlayout和其他布局

You can fix this by adding a third flag - snap , to layout_scrollFlags property. 您可以通过向layout_scrollFlags属性添加第三个标志 - snap来解决此问题。

<Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:elevation="5dp"
    app:layout_scrollFlags="scroll|enterAlways|snap" />

Using this option will determine what to do when a view only has been partially reduced. 使用此选项将确定仅在部分减少视图时要执行的操作。 If scrolling ends and the view size has been reduced to less than 50% of its original, then this view to return to its original size. 如果滚动结束并且视图大小已减小到原始视图的小于50%,则此视图将恢复为其原始大小。 If the size is greater than 50% of its sized, it will disappear completely. 如果尺寸大于其尺寸的50%,它将完全消失。

check this link . 检查此链接

I used a SwipeRefreshLayout or just a LinearLayout or RelativeLayout as the root of the Fragment 's layout and it's working. 我使用SwipeRefreshLayout或只是一个LinearLayoutRelativeLayout作为Fragment布局的根,它正在工作。

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/swipe_refresh_layout_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/mycustomcolor"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</android.support.v4.widget.SwipeRefreshLayout>

With RelativeLayout : 使用RelativeLayout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/fragment2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view_android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true" 
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</RelativeLayout>

Then it should be good to go. 那应该是好的去。

You can use Collapsing layout like 您可以使用折叠布局

<?xml version="1.0" encoding="utf-8"?>
<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:fitsSystemWindows="true">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapse_toolbar"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:fitsSystemWindows="true"
            app:contentScrim="@color/zxing_transparent"
            android:paddingBottom="2dip"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:expandedTitleGravity="bottom|center_horizontal"
            app:expandedTitleMarginBottom="50dip"
            app:collapsedTitleGravity="top|center_vertical"
            app:collapsedTitleTextAppearance="@style/collapseTitle">

            <Toolbar
                    android:id="@+id/toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    android:elevation="5dp"
                    app:layout_scrollFlags="scroll|enterAlways"
                    >
                </Toolbar>



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

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

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


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

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsing_toolbar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:contentScrim="?attr/colorPrimary"
        >
        <android.support.v7.widget.Toolbar
            android:id="@+id/appBar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:layout_collapseMode="pin" />

    </android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:layout_below="@+id/app_bar_layout"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    android:weightSum="1">
    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorAccent"
        >
        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent" 
            android:text="Your recycler view should go here"/>
        </FrameLayout>
</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以工作所有滚动。

in your framelayout set 在你的framelayout集中

android:layout_below="@+id/appBarLayout"

if that not work then try this: 如果那不起作用,那试试这个:

android:layout_below="@+id/toolbar"

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

相关问题 工具栏没有隐藏在RecyclerView滚动上 - Toolbar not hiding on RecyclerView scroll 在RecyclerView Scroll上平稳隐藏工具栏? - Hiding Toolbar smoothly on RecyclerView Scroll? 如果我向下滚动片段中的recyclerView,如何自动隐藏工具栏? - How to hide toolbar automatically if I scroll down the recyclerView inside a fragment? 在片段内而不是活动内使用recyclerview时,在滚动条上隐藏工具栏 - Hide toolbar on scroll when the using a recyclerview inside a Fragment instead of an Activity 在嵌套片段中滚动RecyclerView时隐藏工具栏 - Hiding Toolbar when Scrolling a RecyclerView in nested Fragment 无法使用片段滚动带有 recyclerview 的折叠工具栏 - Unable to scroll collapsing toolbar with recyclerview using fragment 无法在片段内滚动RecyclerView? - Cannot scroll RecyclerView inside a Fragment? 通过在Fragment中滚动recyclerview来隐藏活动中的工具栏? - Hide toolbar in activity by scrolling a recyclerview inside the Fragment? 滚动“活动工具栏”,在屏幕外滚动片段中的Recyclerview - Scroll Activity Toolbar off screen scrolling Recyclerview in fragment android如何使用Fragment RecyclerView滚动工具栏中的Fragment Tablayout ViewPager滚动BottomNavigationView - android how to scroll BottomNavigationView with Fragment Tablayout ViewPager in side Fragment RecyclerView Scroll toolbar
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM