我已将 Android 支持库从 23.1.1
升级到 23.2.1
并且 AppBarLayout
setExpanded
方法是不再像以前那样工作。
我有 CollapsingToolbarLayout
占据整个屏幕,在它下面有 NestedScrollView
持有其他 View 。上下滑动可完全折叠/展开工具栏布局,从而显示或隐藏带有内容的 ScrollView 。
手动滑动工作正常,但我也有触发 AppBarLayout
setExpanded
方法的按钮,带有 true/false
参数以自动折叠/展开工具栏.对于版本 23.1.1
,此方法也可以正常工作,但对于 23.2.1
,只有工具栏的第一次折叠会在下面的 ScrollView 中显示内容,所有后续折叠都不会。当工具栏折叠时触发 setExpanded(true)
方法将显示我的 ScrollView 的内容,并且展开动画将按预期工作。
问题可以在 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'
}
其余项目文件与具有空 Activity 的基础 Android Studio 项目相同。
- 正确行为的屏幕快照 - 在使用
SCROLL
按钮触发的折叠/展开动画期间,文本应始终可见。只有当工具栏(蓝色区域)完全覆盖屏幕时,它才应该可见。
- 损坏行为的屏幕快照 - 在
SCROLL
按钮触发的工具栏折叠动画期间,文本不可见。
上方屏幕快照中的红色箭头显示应观察的屏幕区域。
最佳答案
有关 fitsSystemWindows 的一些更改。
从 AppBarLayout 和 CollapsingToolbarLayout 中删除该属性。
仅在 CoordinatorLayout 中保留。
关于java - Android 支持库问题 23.2.1 AppBarLayout setExpanded,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36420317/