CoordinatorLayout.Behavior callbacks don't trigger
V

2

7

I wrote a CordinatorLayout.Behaviour class and assigned it in a CordinatorLayout's child, a LinearLayout using

app:layout_behavior="com.mob2.zd2duta.infodrawer.components. FloatingHeaderBehaviour"

but only layoutDependsOn, onStartNestedScroll, onInterceptTouchEvent callbacks are called, rest don't get called. What am I doing wrong

public class FloatingHeaderBehaviour extends CoordinatorLayout.Behavior<LinearLayout> {

    private String TAG = FloatingHeaderBehaviour.class.getSimpleName();
    private Context context;

    public FloatingHeaderBehaviour(Context context, AttributeSet attrs) {
        this.context = context;
    }

    @Override
    public boolean layoutDependsOn(CoordinatorLayout parent, LinearLayout child, View dependency) {
        boolean val = (dependency.getId() == R.id.nested_scrollview);
        return val;
    }

    @Override
    public boolean onDependentViewChanged(CoordinatorLayout parent, LinearLayout child, View dependency) {

        Utils.logD(this.getClass().getSimpleName(), "dependency changed");
        return true;
    }

    @Override
    public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, LinearLayout child, View directTargetChild, View target, int nestedScrollAxes) {

        Utils.logD(this.getClass().getSimpleName(), "scroll started");
        return super.onStartNestedScroll(coordinatorLayout,child, directTargetChild, target, nestedScrollAxes);
    }

    @Override
    public void onStopNestedScroll(CoordinatorLayout coordinatorLayout, LinearLayout child, View target) {
        Utils.logD(this.getClass().getSimpleName(), "scroll stopped");
        super.onStopNestedScroll(coordinatorLayout, child, target);
    }

    @Override
    public void onNestedScroll(CoordinatorLayout coordinatorLayout, LinearLayout child, View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) {
        Utils.logD(this.getClass().getSimpleName(), "scroll changed");
        super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed);
    }

    @Override
    public void onNestedPreScroll(CoordinatorLayout coordinatorLayout, LinearLayout child, View target, int dx, int dy, int[] consumed) {
        Utils.logD(this.getClass().getSimpleName(), "scroll pre");
        super.onNestedPreScroll(coordinatorLayout, child, target, dx, dy, consumed);
    }

    @Override
    public boolean onInterceptTouchEvent(CoordinatorLayout parent, LinearLayout child, MotionEvent ev) {
        Utils.logD(this.getClass().getSimpleName(), "onInterceptTouchEvent");
        return super.onInterceptTouchEvent(parent, child, ev);
    }

    @Override
    public boolean onTouchEvent(CoordinatorLayout parent, LinearLayout child, MotionEvent ev) {
        Utils.logD(this.getClass().getSimpleName(), "onTouchEvent");
        return super.onTouchEvent(parent, child, ev);
    }

    @Override
    public void onNestedScrollAccepted(CoordinatorLayout coordinatorLayout, LinearLayout child, View directTargetChild, View target, int nestedScrollAxes) {
        Utils.logD(this.getClass().getSimpleName(), "onNestedScrollAccepted");
        super.onNestedScrollAccepted(coordinatorLayout, child, directTargetChild, target, nestedScrollAxes);
    }

    @Override
    public boolean onNestedFling(CoordinatorLayout coordinatorLayout, LinearLayout child, View target, float velocityX, float velocityY, boolean consumed) {
        Utils.logD(this.getClass().getSimpleName(), "onNestedFling");
        return super.onNestedFling(coordinatorLayout, child, target, velocityX, velocityY, consumed);
    }

    @Override
    public boolean onNestedPreFling(CoordinatorLayout coordinatorLayout, LinearLayout child, View target, float velocityX, float velocityY) {
        Utils.logD(this.getClass().getSimpleName(), "onNestedPreFling");
        return super.onNestedPreFling(coordinatorLayout, child, target, velocityX, velocityY);
    }
}
Villeinage answered 4/8, 2015 at 16:37 Comment(0)
S
17

Per the onStartNestedScroll() Javadoc:

Only Behaviors that return true from this method will receive subsequent nested scroll events.

The default behavior is always to return false, which is what you are returning when you call return super.onStartNestedScroll(). You should instead return true for the nestedScrollAxes you wish to receive scroll events for:

@Override
public boolean onStartNestedScroll(final CoordinatorLayout coordinatorLayout,
        final FloatingActionButton child, final View directTargetChild,
        final View target, final int nestedScrollAxes) {
    return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL;
}
Siphon answered 4/8, 2015 at 16:48 Comment(0)
E
0

Just had the same problem. Accidentlly, I've found in google something called "NestedScrollView". And yes. "NestedScrollView" is the answer. Use it insted of ScrollView, and also do what you can see in the anaswer above. Works fine !

Egeria answered 13/9, 2015 at 21:6 Comment(1)
Could you provide a full answer? The answer 'above' might not always be above.Riposte

© 2022 - 2024 — McMap. All rights reserved.