【问题标题】:How to set AppBarLayout overlaying the content?如何设置 AppBarLayout 覆盖内容?
【发布时间】:2016-06-06 14:43:36
【问题描述】:

这是我当前的 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>

现在“some_layout”在“recycler_view”上方并与它一起滚动。我想以这样的方式更改它,“some_layout”覆盖回收器视图,但滚动行为仍然存在(基本上,当没有滚动时,两个视图都应该是顶部对齐的,并且“some_layout”应该在一些滚动后消失)。可以用 CoordinatorLayout 做吗?

【问题讨论】:

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


    【解决方案1】:

    如果我正确理解了您的问题,您希望 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" />
    

    【讨论】:

      猜你喜欢
      • 2018-07-10
      • 2015-09-12
      • 2013-02-21
      • 1970-01-01
      • 2022-06-11
      • 1970-01-01
      • 2019-08-26
      • 1970-01-01
      • 2022-10-23
      相关资源
      最近更新 更多