【问题标题】:Issue with Android Support Library 23.2.1 AppBarLayout setExpandedAndroid 支持库 23.2.1 AppBarLayout setExpanded 的问题
【发布时间】:2016-04-05 07:55:51
【问题描述】:

我已将 Android 支持库从 23.1.1 升级到 23.2.1AppBarLayout setExpanded 方法不再像以前那样工作。

我有CollapsingToolbarLayout 占据整个屏幕,在它下面有NestedScrollView 持有其他视图。上下滑动会完全折叠/展开工具栏布局,从而显示或隐藏带有内容的滚动视图。

手动滑动可以正常工作,但我也有按钮触发 AppBarLayout setExpanded 方法和 true/false 参数以自动折叠/展开工具栏。对于版本23.1.1,此方法也可以正常工作,但对于23.2.1,只有工具栏的第一次折叠会在下面的滚动视图中显示内容,所有后续折叠都不会。在工具栏折叠时触发setExpanded(true) 方法将显示我的滚动视图的内容,并且展开动画将按预期工作。

问题可以在 API 22 及更低版本的设备/模拟器上重现。

有什么想法可以解决23.2.1 库中的这种行为吗?


展示上述行为的基本示例:

MainActivity.java

package com.test.displaym;

import android.support.design.widget.AppBarLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity
{
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void btnClick(View v)
    {
        View iv = findViewById(R.id.image_view);
        AppBarLayout bar = (AppBarLayout) findViewById(R.id.app_bar);

        if (iv.getTop() == 0) bar.setExpanded(false);
        else bar.setExpanded(true);
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context="com.test.displaym.MainActivity">

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

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

            <android.support.design.widget.CollapsingToolbarLayout
                android:id="@+id/toolbar_layout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                app:contentScrim="@color/colorPrimary"
                app:layout_scrollFlags="scroll|exitUntilCollapsed">

                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:id="@+id/image_view"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true"
                    android:fitsSystemWindows="true"
                    android:scaleType="centerCrop"
                    app:layout_collapseMode="parallax"/>

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

        <android.support.v4.widget.NestedScrollView
            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:id="@+id/page_scroll"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">

            <TextView android:id="@+id/page"
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      android:text = "Some text\n12345\n"
                      android:orientation="vertical">
            </TextView>
        </android.support.v4.widget.NestedScrollView>

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

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Scroll"
        android:id="@+id/button"
        android:onClick="btnClick"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"/>

</RelativeLayout>

build.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.2"

    defaultConfig {
        applicationId "com.test.displaym"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.2.1'
    compile 'com.android.support:design:23.2.1'
    compile 'com.android.support:support-v4:23.2.1'
}

其余项目文件与基础 Android Studio 项目中的相同,但 Activity 为空。

  • 正确行为的屏幕快照 - 在使用 SCROLL 按钮触发的折叠/展开动画期间,文本应始终可见。只有当工具栏(蓝色区域)完全覆盖屏幕时,它才应该是可见的。

  • 损坏行为的屏幕快照 - 在由SCROLL 按钮触发的工具栏折叠动画期间,文本不可见。

以上屏幕快照中的红色箭头表示应观察的屏幕区域。

【问题讨论】:

  • 支持库 23.3.0 出现类似问题。报告为code.google.com/p/android/issues/detail?id=205960
  • 奇怪的是,禁用 setExpanded (bar.setExpanded(false, false)) 的动画似乎可以正常工作。所以问题/错误可能在支持库的动画代码中(?)。
  • @Sevle 那将是正确的观察。不过,这并没有多大帮助,因为我在这里追求的是动画效果:)
  • 问题已通过 23.4.0 库修复

标签: java android android-support-library android-support-design


【解决方案1】:

fitsSystemWindows 发生了一些变化。

从 AppBarLayout 和 CollapsingToolbarLayout 中移除该属性。

只保留在 CoordinatorLayout 中。

【讨论】:

    【解决方案2】:

    全部删除

    android:fitsSystemWindows="true"

    在子布局上,只有 Parent RelativeLayout 有它。

    并将 CollapsingToolbarLayout 编辑为:

               <android.support.design.widget.CollapsingToolbarLayout
                android:id="@+id/toolbar_layout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                app:contentScrim="@color/colorPrimary"
                app:layout_scrollFlags="scroll|snap|enterAlways">
    

    希望对您有所帮助!

    【讨论】:

    • 谢谢,但这并不能解决滚动内容问题。此外,snap|enterAlways 标志以不符合我的目的的方式彻底改变了滚动行为。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-03
    • 2016-07-04
    • 1970-01-01
    相关资源
    最近更新 更多