可见软键盘时,CollapsingToolbarLayout不崩溃

Dan*_*ims 6 java android android-collapsingtoolbarlayout

我有一个用于工具栏的布局;

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

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

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/expanded_collapsing_toolbar"
        android:layout_width="match_parent"
        android:layout_height="120dp"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">

        <android.support.v7.widget.Toolbar
            android:id="@+id/expanded_toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_collapseMode="pin"/>

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

</android.support.design.widget.AppBarLayout>
Run Code Online (Sandbox Code Playgroud)

我在布局中将其用作:

<com.dan.finance.ui.ExpandedToolbar
    android:id="@+id/expandable_toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:toolbarNavColor="?attr/NavigationIconColor"
    app:toolbarNavIcon="?attr/NavigationUpArrow"
    app:toolbarTitle="My Finances"
    app:toolbarTitleColor="?attr/NavigationTitleColor"/>
Run Code Online (Sandbox Code Playgroud)

And below this in most of my layouts I have a nested scrollview, my problem is on layouts where the content shouldn't scroll by default, opening the soft keyboard allows the content to scroll using adjustResize, but my toolbar doesn't react to this and collapse as it should.

The full code for my layout is;

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

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

            <com.dan.finance.ui.ExpandedToolbar
                android:id="@+id/expandable_toolbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:toolbarNavColor="?attr/NavigationIconColor"
                app:toolbarNavIcon="?attr/NavigationUpArrow"
                app:toolbarTitle="My Finances"
                app:toolbarTitleColor="?attr/NavigationTitleColor"/>

            <android.support.v4.widget.NestedScrollView
                android:id="@+id/nested_scrollview"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fillViewport="true"
                app:layout_anchor="@id/expandable_toolbar"
                app:layout_anchorGravity="bottom"
                app:layout_behavior="@string/appbar_scrolling_view_behavior">

                <android.support.constraint.ConstraintLayout
                    android:id="@+id/container"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                <android.support.v7.widget.AppCompatTextView
                    android:id="@+id/title"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:paddingStart="32dp"
                    android:paddingEnd="0dp"
                    android:text="Finances"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent"/>

                <android.support.v7.widget.AppCompatEditText
                    android:id="@+id/edit_text"                       
                    android:layout_width="0dp"
                    android:layout_height="56dp"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                   app:layout_constraintTop_toBottomOf="@id/title"/>

                <android.support.v7.widget.AppCompatTextView
                    android:id="@+id/details"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:text="DETAILS TODO"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@id/edit_text"/>

                <android.support.v7.widget.RecyclerView                                                      android:id="@+id/finances_list"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal"
                    app:layoutManager="android.support.v7.widget.LinearLayoutManager"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@id/details"/>

                <android.support.v7.widget.AppCompatButton
                    android:id="@+id/button_see_all"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="See All"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintTop_toBottomOf="@id/finances_list"
                    app:layout_constraintVertical_bias="1.0"/>

                </android.support.constraint.ConstraintLayout>

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

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

    </RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

This layout as a whole doesn't scroll on large devices, but may scroll on smaller devices/future releases so I believe this may be where my problem lies but I've tried quite a few different things and nothing has come up. I've also tried programatically expanding and collapsing the toolbar using

mAppBarLayout.setExpanded(expand, true);
Run Code Online (Sandbox Code Playgroud)

but this doesn't animate the layout I'm assuming because it's not in a scrolling layout because there's no content to bring up maybe?

Che*_*amp 2

AppBarLayout必须是CoordinatorLayout的直接子级,布局的滚动和折叠才能按您的预期工作。(请参阅AppBarLayout 文档。)

此视图在很大程度上取决于是否用作 CoordinatorLayout 中的直接子级。如果您在不同的 ViewGroup 中使用 AppBarLayout,它的大部分功能将无法工作。

这是当前编码的布局。(这是来自布局检查器。)

在此输入图像描述

正如您所看到的,AppBarLayout不是CoordinatorLayout的直接子级,而是ExpandedToolbar的子级,而 ExpandedToolbar 本身就是一个AppBarLayout

要解决此问题,您需要将Expanded_toolbar.xml更改为以下内容:

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

    <!--<android.support.design.widget.AppBarLayout-->
    <!--android:id="@+id/appbar_layout"-->
    <!--android:layout_width="match_parent"-->
    <!--android:layout_height="wrap_content"-->
    <!--android:fitsSystemWindows="true">-->

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/expanded_collapsing_toolbar"
        android:layout_width="match_parent"
        android:layout_height="120dp"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">

        <android.support.v7.widget.Toolbar
            android:id="@+id/expanded_toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_collapseMode="pin" />

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

    <!--</android.support.design.widget.AppBarLayout>-->
</merge>
Run Code Online (Sandbox Code Playgroud)

如您所见,我通过注释掉了AppBarLayout 。现在,当我们运行应用程序时,我们会看到以下层次结构:

在此输入图像描述

在这里,您可以看到ExpandedToolbar实际上是一个AppBarLayout ,它是CoordinatorLayout的直接子级。这有效。这是一个视觉效果。我没有实现整个自定义布局 - 足以用于演示目的。

在此输入图像描述

这是更新后的主要布局:

活动主文件

<RelativeLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

        <com.example.customviewtoolbar.ExpandedToolbar
            android:id="@+id/expandable_toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:toolbarNavColor="?attr/NavigationIconColor"
            app:toolbarNavIcon="?attr/NavigationUpArrow"
            app:toolbarTitle="My Finances"
            app:toolbarTitleColor="?attr/NavigationTitleColor" />

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

            <android.support.constraint.ConstraintLayout
                android:id="@+id/container"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <android.support.v7.widget.AppCompatTextView
                    android:id="@+id/title"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:paddingStart="32dp"
                    android:paddingEnd="0dp"
                    android:text="@string/finances"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent" />

                <android.support.v7.widget.AppCompatEditText
                    android:id="@+id/edit_text"
                    android:layout_width="0dp"
                    android:layout_height="56dp"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@id/title" />

                <android.support.v7.widget.AppCompatTextView
                    android:id="@+id/details"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:text="DETAILS TODO"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@id/edit_text" />

                <android.support.v7.widget.RecyclerView
                    android:id="@+id/finances_list"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal"
                    app:layoutManager="android.support.v7.widget.LinearLayoutManager"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@id/details" />

                <android.support.v7.widget.AppCompatButton
                    android:id="@+id/button_see_all"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="See All"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintTop_toBottomOf="@id/finances_list"
                    app:layout_constraintVertical_bias="1.0" />

            </android.support.constraint.ConstraintLayout>

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

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

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

作为旁注,我android:fillViewport="true"NestedScrollView中删除了与锚点相关的标签,因为它们并不是真正需要的,并且阻止了布局检查器的工作。

您始终可以不使用自定义视图,但我认为您需要它是为了方便。

ExpandedToolbar这是我用于演示目的的模型。

ExpandedToolbar.java

public class ExpandedToolbar extends AppBarLayout {
    public ExpandedToolbar(Context context) {
        super(context);
        init();
    }

    public ExpandedToolbar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
        TypedArray a = context.getTheme().obtainStyledAttributes(
                attrs,
                R.styleable.MyToolbar,
                0, 0);

        try {
            String title = a.getString(R.styleable.MyToolbar_toolbarTitle);
            ((Toolbar) findViewById(R.id.expanded_toolbar)).setTitle(title);
        } finally {
            a.recycle();
        }
    }

    private void init() {
        inflate(getContext(), R.layout.expanded_toolbar, this);
    }
}
Run Code Online (Sandbox Code Playgroud)