Zarwan
Zarwan

Reputation: 5777

CoordinatorLayout with custom behavior not called

I'm trying to implement a CoordinatorLayout with a RecyclerView and a TextView where the TextView will animate depending on how you scroll the RecyclerView. But onDependentViewChanged in my custom behaviour is only being called a few times when my view first inflates and isn't called after that, despite me scrolling the RecyclerView.

My behaviour:

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;
    }
}

My 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>

Upvotes: 3

Views: 2440

Answers (1)

Zarwan
Zarwan

Reputation: 5777

The answer to this is that you need to override the following method in your custom behaviour class to return true:

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

This will send scroll events through and you'll get calls to layoutDependsOn and onDependentViewChanged appropriately.

Upvotes: 6

Related Questions