android - 如何同步两个 CoordinatorLayout + AppBarLayout 的滚动

标签 android android-coordinatorlayout android-collapsingtoolbarlayout android-appbarlayout

我有一个 XML Activity 。

类似于:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    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/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/coordinator"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.design.widget.AppBarLayout
            android:id="@+id/appbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="@color/primary"
                android:theme="@style/ToolbarStyle"
                android:gravity="center_vertical"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                app:layout_scrollFlags="scroll|enterAlways|snap"
                style="@style/bold" />

            <android.support.design.widget.TabLayout
                android:id="@+id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:theme="@style/TabLayoutStyle"
                android:animateLayoutChanges="true"
                app:tabGravity="fill"
                app:tabMode="fixed"
                app:tabTextAppearance="@style/TabStyle"/>

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

        <com.wedmegood.planner.view.XViewPager
            android:id="@+id/main_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="fill_vertical"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />

        ...

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

ViewPagerFragment 有一个 CoordinatorLayoutAppBarLayout + CollapsingToolbarLayout .

fragment 的XML:

<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:id="@+id/root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/primaryDark"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapse_toolbar"
            android:layout_width="match_parent"
            android:layout_height="@dimen/my_wedding_banner_height"
            android:minHeight="48dp"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed|snap">

            <RelativeLayout
                android:id="@+id/banner"
                android:layout_width="match_parent"
                android:layout_marginTop="0dp"
                android:layout_height="match_parent"
                app:layout_collapseMode="none">


            </RelativeLayout>

            <android.support.design.widget.TabLayout
                android:id="@+id/tabs"
                android:layout_width="match_parent"
                android:layout_height="48dp"
                android:layout_gravity="bottom"
                android:animateLayoutChanges="true"
                app:tabMode="fixed"
                app:tabGravity="fill"
                app:tabTextColor="@color/primary"
                app:tabSelectedTextColor="@color/primary"
                app:tabIndicatorColor="@color/accent"
                app:tabIndicatorHeight="4dp"
                app:tabTextAppearance="@style/TabStyle"/>

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

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

        <com.wedmegood.planner.view.XViewPager
            android:id="@+id/viewpager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />

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

fragment 有一个横幅,RelativeLayout。我想同步这两个 AppBarLayouts 的滚动,以便外部 AppBar 在内部 AppBar 折叠之前/之后折叠(不管它发生在之前还是之后)?我尝试根据它们的 Offset 更改监听器设置和取消设置 2 个 AppBar 的滚动标志,它有点工作但没有提供平滑的滚动效果。只要横幅正常工作,我就可以完全更改 fragment 的 XML。

另外,有没有办法在 CollapsingToolbarLayout 折叠/展开时更改样式/调用 TabLayout 的 setTabTextColors?

最佳答案

几天前我遇到了同样的问题。最后,我从 SwipeRefreshLayout 中找到了解决方案。只需扩展 CoordinatorLayout 并实现 NestedScrollingChild 接口(interface)。有用!

这是我的代码:

public class NestedCoordinatorLayout extends CoordinatorLayout implements NestedScrollingChild {
private final NestedScrollingChildHelper mNestedScrollingChildHelper;

private final int[] mParentOffsetInWindow = new int[2];
private final int[] mParentScrollConsumed = new int[2];

public NestedCoordinatorLayout(Context context) {
    this(context, null, 0);
}

public NestedCoordinatorLayout(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
}

public NestedCoordinatorLayout(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    mNestedScrollingChildHelper = new NestedScrollingChildHelper(this);
    setNestedScrollingEnabled(true);
}

// NestedScrollingChild

@Override
public void setNestedScrollingEnabled(boolean enabled) {
    mNestedScrollingChildHelper.setNestedScrollingEnabled(enabled);
}

@Override
public boolean isNestedScrollingEnabled() {
    return mNestedScrollingChildHelper.isNestedScrollingEnabled();
}

@Override
public boolean startNestedScroll(int axes) {
    return mNestedScrollingChildHelper.startNestedScroll(axes);
}

@Override
public void stopNestedScroll() {
    mNestedScrollingChildHelper.stopNestedScroll();
}

@Override
public boolean hasNestedScrollingParent() {
    return mNestedScrollingChildHelper.hasNestedScrollingParent();
}

@Override
public boolean dispatchNestedScroll(int dxConsumed, int dyConsumed, int dxUnconsumed,
                                    int dyUnconsumed, int[] offsetInWindow) {
    return mNestedScrollingChildHelper.dispatchNestedScroll(dxConsumed, dyConsumed,
            dxUnconsumed, dyUnconsumed, offsetInWindow);
}

@Override
public boolean dispatchNestedPreScroll(int dx, int dy, int[] consumed, int[] offsetInWindow) {
    return mNestedScrollingChildHelper.dispatchNestedPreScroll(dx, dy, consumed, offsetInWindow);
}

@Override
public boolean dispatchNestedFling(float velocityX, float velocityY, boolean consumed) {
    return mNestedScrollingChildHelper.dispatchNestedFling(velocityX, velocityY, consumed);
}

@Override
public boolean dispatchNestedPreFling(float velocityX, float velocityY) {
    return mNestedScrollingChildHelper.dispatchNestedPreFling(velocityX, velocityY);
}

@Override
public void onNestedScrollAccepted(View child, View target, int axes) {
    super.onNestedScrollAccepted(child, target, axes);

    // Dispatch up to the nested parent
    startNestedScroll(axes & ViewCompat.SCROLL_AXIS_VERTICAL);
}

@Override
public void onStopNestedScroll(View target) {
    super.onStopNestedScroll(target);
    stopNestedScroll();
}

@Override
public void onNestedScroll(View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) {
    super.onNestedScroll(target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed);
    dispatchNestedScroll(dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed,
            mParentOffsetInWindow);
}

@Override
public void onNestedPreScroll(View target, int dx, int dy, int[] consumed) {
    super.onNestedPreScroll(target, dx, dy, consumed);

    final int[] parentConsumed = mParentScrollConsumed;
    if (dispatchNestedPreScroll(dx - consumed[0], dy - consumed[1], parentConsumed, null)) {
        consumed[0] += parentConsumed[0];
        consumed[1] += parentConsumed[1];
    }
}
}

在 fragment 的布局(内部协调器布局)中使用它。

关于android - 如何同步两个 CoordinatorLayout + AppBarLayout 的滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34934274/

相关文章:

android - 如何将 header 添加到 Google 的新 NavigationView 中?

android - Snackbar 的滑动关闭卡住应用程序

android - 约束布局与嵌套 ScrollView 中的父级不匹配

android - 折叠工具栏-状态栏下的工具栏

android - 谷歌地图标记不显示

Android ListFragment 令人困惑

android - 具有线性布局的 AppBarLayout

android - 将滚动 fragment 设置为滚动 Activity

android - 从 Intent 调用 SimpleBaseGameActivity

android - 带有底部导航栏的 fragment 中的折叠工具栏 |安卓工作室