当软键盘可见时,CollapsingToolbarLayout不会折叠

6
我有一个用于工具栏的布局;
<?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>

我在我的布局中使用以下代码:

<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"/>

在我的大多数布局中,下面是一个嵌套的scrollview。我的问题是在内容默认情况下不应该滚动的布局上,打开软键盘允许使用adjustResize滚动内容,但我的工具栏没有像应该折叠一样做出反应。
我的布局的完整代码如下:
<?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>

整个页面在大屏幕设备上不会滚动,但在较小的设备或未来的版本中可能会出现滚动,因此我认为这可能是我的问题所在,但我尝试了很多不同的方法,没有得到任何结果。 我还尝试了使用编程方式展开和折叠工具栏,如下:

mAppBarLayout.setExpanded(expand, true);

但这并没有使布局动起来,我猜是因为它不在滚动布局中,可能是因为没有内容需要上移?

查看_ExpandedToolbar_会很有帮助。你能发布它吗? - Cheticamp
@Cheticamp 扩展工具栏是顶部片段。 - Daniel Sims
也许我漏掉了什么,但是_ExpandedToolbar_不是一个自定义视图吗?我正在尝试理解顶部片段实际上是如何添加到布局中的。我认为发布_ExpandedToolbar_的代码会有所帮助。 - Cheticamp
是的,顶部的代码在一个名为ExpandedToolbar的自定义视图中。 - Daniel Sims
你能发布那个自定义视图吗?是_ExpandedToolbar.java_还是_ExpandedToolbar.kt_? - Cheticamp
这是Java,但不幸的是我没有它在我的笔记本电脑上,它在我的工作笔记本电脑上,直到周一我才能拿到。我会在周一尝试下面建议的答案,因为他似乎已经解决了这个问题 :)。 - Daniel Sims
3个回答

2
“AppBarLayout”必须作为“CoordinatorLayout”的直接子项,才能实现预期的滚动和折叠布局效果。(请参阅AppBarLayout文档。)

该视图在很大程度上依赖于其作为CoordinatorLayout中的直接子项使用。如果您在不同的ViewGroup中使用AppBarLayout,则其大部分功能将无法正常工作。

以下是当前编码的布局示例(来自“布局检查器”)。

enter image description here

正如您所看到的,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>

如您所见,我通过注释掉标签来移除了它。现在,当我们运行应用程序时,可以看到以下层次结构:

enter image description here

在这里,您可以看到ExpandedToolbar实际上是一个AppBarLayout,它是CoordinatorLayout的直接子项。这很有效。下面是一个可视化示例。我没有实现整个自定义布局 - 只是为了演示目的而足够。

enter image description here

这是更新后的主要布局:

activity_main.xml

<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>

作为附注,我从NestedScrollView中删除了与锚点相关的标签和android:fillViewport="true",因为它们并不是真正需要的,并且阻止了布局检查器的工作。
你总可以选择不使用自定义视图,但我想你可能是出于方便而使用它。
这是我用于演示目的的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);
    }
}

2

我在xml文件中使用了以下代码行,这样无论软键盘是否可见,它都可以正常工作。

原始答案:Original Answer

<?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"
                xmlns:tools="http://schemas.android.com/tools"
                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">

        <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>

        <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/appbar_layout"
                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>

你可以使用以下监听器检查活动中的AppBarLayout折叠/展开过程:

也可以使用此监听器检查活动中AppBarLayout的折叠/展开过程:

appbar_layout.addOnOffsetChangedListener(object : AppBarLayout.OnOffsetChangedListener {
            override fun onOffsetChanged(p0: AppBarLayout?, p1: Int) {
                if (Math.abs(p1) - appbar_layout.totalScrollRange == 0) {
                    Log.d("tag", "Collapsed")
                } else {
                    Log.d("tag", "Expanded")
                }
            }
        })

我认为这是因为您在自定义xml中使用了AppBarLayout。"最初的回答"

0
尝试移除 android:fitsSystemWindows="true" 并仅使用 adjustResize。应该可以正常工作。

不行,这个方法不起作用,我已经尝试了很多删除和添加的组合,但都没有成功。我的活动也只是adjustResize。 - Daniel Sims

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