当CollapsingToolbarLayout的子项时,不会调用Android布局行为

时间:2015-06-11 16:08:46

标签: android androiddesignsupport android-collapsingtoolbarlayout android-coordinatorlayout

我正在尝试在layout_behavior的子元素上设置CollapsingToolbarLayout,但永远不会在iv_header视图上调用该行为。它在外部锚定时非常有效,例如使用tv_follow视图。

文档没有具体说明layout_behavior无法在AppBarLayoutCollapsingToolbarLayout中应用,所以我不知道为什么它不是{<}}。工作。

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/header"
        android:layout_width="match_parent"
        android:layout_height="@dimen/full_header_height"
        android:focusable="true"
        android:focusableInTouchMode="true">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <ImageView
                android:id="@+id/iv_header"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="centerCrop"
                android:contentDescription="@null"
                app:layout_behavior="com.package.view.HidingBehavior"
                app:layout_collapseMode="parallax"
                android:src="@drawable/profile_background"/>

            <android.support.v7.widget.Toolbar
                android:id="@+id/header_toolbar"
                android:layout_height="@dimen/action_bar_height"
                android:layout_width="match_parent"
                android:background="@drawable/toolbar_dark_gradient_half"
                android:gravity="top"
                app:layout_collapseMode="pin"/>

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

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

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

    <TextView
        android:id="@+id/tv_follow"
        android:textSize="20sp"
        android:textColor="@android:color/white"
        android:text="@string/follow"
        android:drawableLeft="@drawable/comm_follow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_anchor="@id/header"
        app:layout_anchorGravity="center"
        app:layout_behavior="com.package.view.HidingBehavior"
        android:drawablePadding="8dp"
        android:gravity="center"
        android:visibility="gone"
        android:fitsSystemWindows="true"/>

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

行为已从设计支持库中的FloatingActionButton代码中拉出。

public class HidingBehavior extends CoordinatorLayout.Behavior<View>{
    private Rect tmpRect;
    private boolean isAnimatingOut;
    private FastOutSlowInInterpolator fastOutSlowInInterpolator = new FastOutSlowInInterpolator();

    public HidingBehavior() {
    }

    public HidingBehavior(Context context, AttributeSet attrs) {
        super();
    }

    @Override
    public boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency) {
        return dependency instanceof AppBarLayout;
    }

    @Override
    public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) {
        if (dependency instanceof AppBarLayout) {
            AppBarLayout appBarLayout = (AppBarLayout) dependency;
            if (this.tmpRect == null) {
                this.tmpRect = new Rect();
            }

            Rect rect = this.tmpRect;
            ViewGroupUtils.getDescendantRect(parent, dependency, rect);
            if (rect.bottom <= getMinimumHeightForContext(appBarLayout)) {
                if(!this.isAnimatingOut && child.getVisibility() == View.VISIBLE) {
                    this.animateOut(child);
                }
            } else if(child.getVisibility() != View.VISIBLE) {
                this.animateIn(child);
            }
        }

        return false;
    }

    private int getMinimumHeightForContext(AppBarLayout appBarLayout) {
        int minHeight = ViewCompat.getMinimumHeight(appBarLayout);
        if(minHeight != 0) {
            return minHeight*2;
        } else {
            int childCount = appBarLayout.getChildCount();
            return childCount >= 1?ViewCompat.getMinimumHeight(appBarLayout.getChildAt(childCount - 1))*2:0;
        }
    }

    private void animateIn(View view) {
        view.setVisibility(View.VISIBLE);
        ViewCompat.animate(view)
                .scaleX(1.0F)
                .scaleY(1.0F)
                .alpha(1.0F)
                .setInterpolator(fastOutSlowInInterpolator)
                .withLayer()
                .setListener((ViewPropertyAnimatorListener)null).start();
    }

    private void animateOut(final View view) {
        ViewCompat.animate(view)
                .scaleX(0.0F)
                .scaleY(0.0F)
                .alpha(0.0F)
                .setInterpolator(fastOutSlowInInterpolator)
                .withLayer()
                .setListener(new ViewPropertyAnimatorListener() {
            public void onAnimationStart(View view) {
                HidingBehavior.this.isAnimatingOut = true;
            }

            public void onAnimationCancel(View view) {
                HidingBehavior.this.isAnimatingOut = false;
            }

            public void onAnimationEnd(View view) {
                HidingBehavior.this.isAnimatingOut = false;
                view.setVisibility(View.GONE);
            }
        }).start();
    }
}

1 个答案:

答案 0 :(得分:11)

如果我没有记错的话,这条线在您目前拥有的地方是不必要的......

app:layout_behavior="com.package.view.HidingBehavior"

layout_behavior应该应用于AppBarLayout的兄弟,而不是(嵌套)的孩子。这是因为它告诉兄弟姐妹,在CoordinatorLayout内,他们需要如何协调他们的行为以回应他们正在做的事情。

换句话说,只要你拥有它,就不会协调任何其他行为的行为。