如何设置覆盖内容的AppBarLayout?

时间:2016-06-06 14:43:37

标签: android android-layout android-coordinatorlayout android-appbarlayout

这是我目前的XML:

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

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

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

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

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
</android.support.design.widget.CoordinatorLayout>

现在&#34; some_layout&#34;高于&#34; recycler_view&#34;并与它一起滚动。我想以这样的方式改变它&#34; some_layout&#34;覆盖回收者视图但滚动行为仍然存在(当没有滚动时,基本上两个视图都应该是顶部对齐的,并且&#34; some_layout&#34;在滚动之后应该消失)。是否可以使用CoordinatorLayout?

1 个答案:

答案 0 :(得分:0)

如果我理解您的问题,您希望Appbar覆盖您的内容(RecyclerView),对吧?

虽然我还没有验证此解决方案,但它基于this answer。如果有效,请告诉我。

通过新AppBarLayout.ScrollingViewBehavior覆盖MyAppBarScrollingViewBehavior并将onDependentViewChanged()修改为updateOffset()来扩展offset = 0

@Override
public boolean onDependentViewChanged(CoordinatorLayout parent, View child,
        View dependency) {
    updateOffset(parent, child, dependency);
    return false;
}

private boolean updateOffset(CoordinatorLayout parent, View child,
        View dependency) {
    final CoordinatorLayout.Behavior behavior = ((CoordinatorLayout.LayoutParams) dependency
            .getLayoutParams()).getBehavior();
    if (behavior instanceof Behavior) {
        // Offset the child so that it is below the app-bar (with any
        // overlap)
        final int offset = 0;   // CHANGED TO 0
        setTopAndBottomOffset(offset);
        return true;
    }
    return false;
}

在RecyclerView上设置行为

<android.support.v7.widget.RecyclerView
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    ...
    layout_behavior="MyAppBarScrollingViewBehavior" />