简体   繁体   English

工具栏不响应CoordinatorLayout中的滚动

[英]Toolbar not responding to scroll in CoordinatorLayout

My view hierchy is straightforward: 我的观点很简单:

<?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_layout"
    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:layout_width="match_parent"
            android:layout_height="wrap_content">

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

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

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

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

    <!-- another view -->

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

A fragment is added to the FrameLayout with the view: 片段随视图一起添加到FrameLayout中:

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

Scrolling the RecyclerView in the fragment has no effect on the toolbar. 在片段中滚动RecyclerView对工具栏没有影响。 I was hoping it would have the same effect as http://imgur.com/HqR8Nx5.png 我希望它与http://imgur.com/HqR8Nx5.png具有相同的效果

Things I've tried: 我尝试过的事情:

1) Wrapping the FrameLayout in a NestedScrollView and adding the app:layout_behavior="@string/appbar_scrolling_view_behavior" to it 1)将FrameLayout包装在NestedScrollView中,然后添加app:layout_behavior="@string/appbar_scrolling_view_behavior"

2) Replacing the FrameLayout with a NestedScrollView 2)用NestedScrollView替换FrameLayout

3) Wrapping the toolbar in a CollapsingToolbarLayout 3)将工具栏包装在CollapsingToolbarLayout中

4) Changing/adding scroll flags 4)更改/添加滚动标志

I think the issue is that your RecyclerView does not have the layout_behavior attribute. 我认为问题在于您的RecyclerView不具有layout_behavior属性。 It should look like: 它应该看起来像:

 <android.support.v7.widget.RecyclerView 
     xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:app="http://schemas.android.com/apk/res-auto"
     android:id="@+id/list_content"
     app:layout_behavior="@string/appbar_scrolling_view_behavior"
     android:layout_height="match_parent"
     android:layout_width="match_parent"/>

I just recently implemented the collapsing toolbar as well. 我刚刚也实现了折叠工具栏。 And based my approach on this blog post . 并根据我在这篇博客文章中的方法

<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:background="@android:color/background_light"
android:fitsSystemWindows="true">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/main.appbar"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:fitsSystemWindows="true"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/main.collapsing"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleMarginEnd="64dp"
            app:expandedTitleMarginStart="48dp"
            app:layout_scrollFlags="scroll|enterAlways">


            <android.support.v7.widget.Toolbar
                android:id="@+id/main.toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:lineSpacingExtra="8dp"
            android:padding="@dimen/activity_horizontal_margin"
            android:text="@string/lorem"
            android:textSize="20sp"/>
    </android.support.v4.widget.NestedScrollView>

    <android.support.design.widget.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/activity_horizontal_margin"
        android:src="@drawable/ic_comment_24dp"
        app:layout_anchor="@id/main.appbar"
        app:layout_anchorGravity="bottom|right|end"/>
</android.support.design.widget.CoordinatorLayout>

you can replace NestedScrollView by your RecyclerView. 您可以用RecyclerView替换NestedScrollView。

    Toolbar toolbar = (Toolbar) findViewById(R.id.main_toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayShowHomeEnabled(true);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    CollapsingToolbarLayout collapsingToolbarLayout =
        (CollapsingToolbarLayout) findViewById(R.id.main_collapsing);

    collapsingToolbarLayout.setTitle(getString(R.string.app_name));

This a image of GIF: 这是GIF图片:

在此处输入图片说明

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

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