具有自定义行为的CoordinatorLayout不被调用

Zar*_*wan 2 android android-coordinatorlayout

我正在尝试使用RecyclerView和TextView实现一个CoordinatorLayout,其中TextView会根据您滚动RecyclerView的方式进行动画处理。但是onDependentViewChanged,尽管我滚动了RecyclerView,但在我的自定义行为中,仅在视图第一次膨胀时才被调用几次,此后才被调用。

我的行为:

public class Behavior extends CoordinatorLayout.Behavior<TextView> {

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

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

    @Override
    public boolean onDependentViewChanged(CoordinatorLayout parent, TextView child, View dependency) {
        return true;
    }
}
Run Code Online (Sandbox Code Playgroud)

我的XML:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    </android.support.v7.widget.RecyclerView>

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="blahhhhhhhhhhh"
        app:layout_behavior="com.mypackage.Behavior"/>

</android.support.design.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)

Zar*_*wan 5

答案是,您需要在自定义行为类中覆盖以下方法以返回true:

@Override
public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, TextView child, View directTargetChild, View target, int nestedScrollAxes) {
    return true;
}
Run Code Online (Sandbox Code Playgroud)

这将通过发送滚动事件,你会得到调用layoutDependsOnonDependentViewChanged适当的。