AppBarLayout与FrameLayout容器作为滚动内容不起作用。

47

我正在尝试使用最新的设计库使我的工具栏在滚动时隐藏/显示。我的问题是我要滚动的内容在片段中,我只是将其注入到FrameLayout容器中,但它不起作用。这是我的活动:

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

<android.support.design.widget.CoordinatorLayout
    android:id="@+id/main_content"
    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">

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

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

    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

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

<FrameLayout
    android:id="@+id/navigation_drawer_container"
    android:layout_width="@dimen/navigation_drawer_width"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    tools:layout="@layout/fragment_nav_drawer_anon" />

我的片段:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<android.support.v4.widget.SwipeRefreshLayout
    android:id="@+id/pull_to_refresh"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</android.support.v4.widget.SwipeRefreshLayout>

<TextView
    android:id="@android:id/empty"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:textSize="18sp"
    android:fontFamily="sans-serif"
    android:color="@color/dark_grey"
    android:padding="60dp"/>

和工具栏:

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
app:layout_scrollFlags="scroll|enterAlways"
style="@style/Widget.MyApp.ActionBar" />

我正按照官方文档和这个演示,但仍然无法弄清如何使其工作。


请问您能否提供更多信息?比如您想要实现什么功能,以及您希望视图/片段的结构是怎样的。 - user3426273
10个回答

26

将你的FrameLayout替换为android.support.v4.widget.NestedScrollView

  

NestedScrollView与ScrollView非常相似,但它支持在新旧Android版本上作为嵌套滚动的父级和子级。嵌套滚动默认启用。

文档链接


7
只有当您希望整个片段垂直滚动时,才能使用该方法。如果您的片段包含需要与CoordinatorLayout的其他子项协调的ViewPager,则该方法无效。似乎如果FragmentContainer不是NestedScrollView,则CoordinatorLayout不能很好地处理后来通过FragmentTransactions添加的子项。遗憾的是,如果要使片段的整体不垂直滚动但仍然对滑动手势做出反应,则无法使用NestedScrollView。 - Marcus Wolschon
目前使用的是 androidx.core.widget.NestedScrollView。请将其添加到 FrameLayout 上方或替换 FrameLayout - CoolMind

15

在CoordinatorLayout的子元素中使用FrameLayout效果很好,标题栏会像预期的那样折叠。起初我遇到了问题,当时我使用了过时的库。

以下是我当前使用的Gradle依赖项:

compile 'com.android.support:appcompat-v7:22.2.1'
compile 'com.android.support:cardview-v7:22.2.0'
compile 'com.android.support:recyclerview-v7:22.2.0'
compile 'com.android.support:design:22.2.0'

我在活动的布局中使用FrameLayout作为CoordinatorLayout的子级,并设置属性app:layout_behavior="@string/appbar_scrolling_view_behavior"。这个FrameLayout用作片段的容器,我的片段布局的根元素要么是RecyclerView,要么是NestedScrollView

以下是活动的布局:

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

    <FrameLayout
            android:id="@+id/..."
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
            />

    <android.support.design.widget.AppBarLayout
            android:layout_height="192dp"
            android:layout_width="match_parent"
            >
        <android.support.design.widget.CollapsingToolbarLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:layout_scrollFlags="scroll|exitUntilCollapsed"
                >
            <ImageView
                    android:id="@+id/.."
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:fitsSystemWindows="true"
                    android:scaleType="centerCrop"
                    android:src="@drawable/..."
                    app:layout_collapseMode="parallax"/>
            <android.support.v7.widget.Toolbar
                    android:id="@+id/toolbar_sessions"
                    android:layout_height="?attr/actionBarSize"
                    android:layout_width="match_parent"
                    app:layout_collapseMode="pin"
                    />
        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>

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

我的第一个片段的布局如下所示:

<android.support.v7.widget.RecyclerView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:scrollbars="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="..."
    />

我的第二个片段的布局如下所示:

<android.support.v4.widget.NestedScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="..."
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="..."
    >
...
</android.support.v4.widget.NestedScrollView>

12
那种行为的原因是Framelayout没有指定一个Behaviour。CoordinatorLayout依赖于子视图来处理Behaviour。
你可以在这里底部阅读。

http://android-developers.blogspot.in/2015/05/android-design-support-library.html

它表明:

CoordinatorLayout 和自定义视图

需要注意的一件事是,CoordinatorLayout 对 FloatingActionButton 或 AppBarLayout 的工作没有任何内在理解 - 它只提供了一个额外的 API,即 Coordinator.Behavior,允许子视图更好地控制触摸事件和手势,并声明彼此之间的依赖关系并通过 onDependentViewChanged() 回调接收。

视图可以通过使用 CoordinatorLayout.DefaultBehavior(YourView.Behavior.class) 注释声明默认行为,或者在布局文件中使用 app:layout_behavior="com.example.app.YourView$Behavior" 属性设置。这个框架使得任何视图都能与 CoordinatorLayout 集成。

编辑:虽然 FrameLayout 不是自定义视图,但它没有指定 CoordinateLayout 寻求的行为。


1
那种行为的原因是Framelayout没有指定一个Behavior。谢谢。这真的帮了我很多。 - Jared Burrows

4

在我的应用中,它仅适用于RecyclerView。也许你应该使用RecyclerView而不是ListView。


你的Activity或Fragment中有RecyclerView吗? - wwang
@wwang 刚刚测试了一下,ListView 不起作用。在我的应用程序中,我有带有 RecyclerView 的片段,它可以无缝地工作,为了测试我添加了一个带有 ListView 的片段,但是它无法正常工作。 - razzledazzle
好的,知道了。谢谢。 - wwang
那么你的意思是只有RecyclerView可以用来引起应用栏的显示/隐藏?而ListView不行? - android developer
@wwang,您是否使用RecyclerView解决了问题?我需要在用户开始滚动RecyclerView或ListView时显示/隐藏顶部布局,然后再向下滚动时它会再次出现...我该如何解决?这里是问题的链接:http://stackoverflow.com/questions/32716906/issues-with-coordinator-layout-in-android - Erum

3
您可以在CoordinatorLayout中使用Framelayout实现滚动效果。为此,您需要在frameLayout和希望应用这种折叠效果的滚动视图中都使用app:layout_behavior="@string/appbar_scrolling_view_behavior"
例如,如果您在FrameLayout中填充RecyclerView,则还需要在recyclerrView中使用app:layout_behavior="@string/appbar_scrolling_view_behavior"
<?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:layout_width="match_parent"
    android:layout_height="match_parent"

    android:orientation="vertical">
<android.support.v7.widget.RecyclerView
    android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
    />
</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:layout_width="match_parent"
    android:layout_height="match_parent"

    android:orientation="vertical">
 <android.support.v4.widget.NestedScrollView
           android:layout_width="match_parent"
           android:layout_height="fill_parent"
        android:layout_above="@+id/bNext"
        android:fillViewport="true"
           android:scrollbars="none"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">
<!--  your content -->
</android.support.v4.widget.NestedScrollView>
</Relativelayout>

因此,使用此过程,您可以在frameLayout中实现滚动,无论它是否包含recycler或nestedScrollview。

我知道回答有点晚了,但是我是这样解决我的问题的。 - Anupriya

2

这是一个好问题,我也曾经遇到过。

你的容器FrameLayout已经定义了,问题出在Fragment上。Fragment应该使用RecyclerView而不是现在已经过时的ListView

示例:

<android.support.v7.widget.RecyclerView
    android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clipToPadding="false"
    android:paddingTop="@dimen/activity_vertical_margin"/>

我在我的应用程序中使用相同的结构,它运行得非常完美。

1

ListView没有实现NestedScrollingChild,因此无法工作。

RecyclerView实现了NestedScrollingChild,因此可以将滚动传播到NestedScrollingParent(即CoordinatorLayout)。


ListView能否实现这个接口并使其正常工作? - android developer
我认为可以。看一下RecyclerView的代码,尝试在自定义的ListView类中实现它。我还没有尝试过,所以如果你成功了,请及时更新我们! - Rafi Panoyan
我已经尝试使用它一段时间了,但在给定时间内没有成功。如果你成功了,请告诉我。我也尝试过使用这个库来实现这个功能:https://github.com/ksoichiro/Android-ObservableScrollView,但我没能让它们很好地协同工作。 - android developer

1
app:layout_scrollFlags="scroll|enterAlways" 从工具栏移动到 Framelayout 中。对于迟到,我感到抱歉。

但它可能会影响列表滚动。 - Zahra.HY

1
您只需要将

替换为:

FrameLayout

即可。

android.support.v4.widget.NestedScrollView

以这种方式:

<android.support.design.widget.CoordinatorLayout
    android:id="@+id/root_coordinator"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

        <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"
            app:layout_scrollFlags="scroll|enterAlways">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="192dp"
                android:scaleType="centerCrop"
                android:src="@drawable/header"
                app:layout_collapseMode="parallax" />

            <android.support.v7.widget.Toolbar
                android:id="@+id/app_bar"
                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>

    <!-- This was before FrameLayout -->
    <android.support.v4.widget.NestedScrollView
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">
    </android.support.v4.widget.NestedScrollView>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|right"
        android:layout_margin="16dp"
        android:src="@drawable/ic_drawer_alertas"
        app:borderWidth="0dp"
        app:fabSize="mini" />
</android.support.design.widget.CoordinatorLayout>


<android.support.design.widget.NavigationView
    android:id="@+id/navigation_drawer"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:headerLayout="@layout/drawer_header"
    app:itemIconTint="@color/colorAccent"
    app:itemTextColor="@color/colorSecondaryText"
    app:menu="@menu/menu_main" />


0

你必须为滚动视图添加行为:

<android.support.v4.widget.SwipeRefreshLayout
    android:layout_marginTop="5dp"
    android:id="@+id/swipe_main"
    android:enabled="false"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">
<com.marshalchen.ultimaterecyclerview.UltimateRecyclerView
    android:id="@+id/rvUserProfile"
    app:recyclerviewEmptyView ="@layout/ev_home"
    app:recyclerviewClipToPadding="false"
    android:layout_width="match_parent"
    android:layout_height="match_parent"></com.marshalchen.ultimaterecyclerview.UltimateRecyclerView>
</android.support.v4.widget.SwipeRefreshLayout>

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