CollapsingToolbarLayout 与 FloatingActionButton 错误

CollapsingToolbarLayout with FloatingActionButton bugs

您好,我正在 Android 应用程序中创建一个简单的滚动 activity。滚动活动有一个具有视差效果的 CollapsingToolbarLayout 和一个固定到 AppbarLayout 的浮动操作按钮。

当我向下滚动文本时,fab 消失,当我向上滚动文本时,浮动操作按钮再次出现。

错误是

1) 当 floatingActionBar 重新出现时,它位于页面的底部,一秒钟后它向上移动到正确的位置 (appbarlayout)。 2) 工具栏和页面内容之间有一个我无法弄清楚的奇怪边距。

因为这很难看所以我想知道:

1.How我能解决这个问题吗? 2.How 我可以保持 fab 可见并锚定到折叠的工具栏吗?

这是错误的演示:http://i.giphy.com/LPMqLSl46Yvu0.gif

这是我的布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:fitsSystemWindows="true"
    tools:context="toxotes.mystikon.EventActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar_event"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:fitsSystemWindows="true"
        android:theme="@style/AppTheme.AppBarOverlay">

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

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="200dp"
                app:layout_collapseMode="parallax"
                android:src="@drawable/test"
                android:scaleType="centerCrop"/>
            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar_event"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/AppTheme.PopupOverlay" />

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

    <include layout="@layout/content_event" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab_event"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_media_play"
        app:layout_anchor="@id/app_bar_event"
        app:layout_anchorGravity="bottom|end" />

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

这是 content_event 布局:

<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="toxotes.mystikon.EventActivity"
    tools:showIn="@layout/activity_event">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/text_margin"
        android:text="@string/large_text" />

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

这里是 java:

public class EventActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_event);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_event);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab_event);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }
}

就在今天,我遇到了完全相同的问题。您必须将支持库更新到版本 24.2.1。在你的构建中 gradle。这为我修好了。

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

但后来我遇到了矢量绘图的问题:

java.lang.IllegalStateException: This app has been built with an incorrect configuration. Please configure your build for VectorDrawableCompat.

如果您 运行 遇到同样的问题,这里是解决方案:http://android-developers.blogspot.de/2016/02/android-support-library-232.html