7

我写了一个CordinatorLayout.Behaviour类并将它分配给一个 CordinatorLayout 的孩子,一个 LinearLayout 使用

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

但只有 layoutDependsOn、onStartNestedScroll、onInterceptTouchEvent 回调被调用,rest 不被调用。我究竟做错了什么

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

2 回答 2

17

根据onStartNestedScroll() Javadoc

只有从此方法返回 true 的行为才会接收后续的嵌套滚动事件。

默认行为始终是返回false,这是您在调用时返回的内容return super.onStartNestedScroll()true相反,您应该返回nestedScrollAxes您希望接收滚动事件的:

@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;
}
于 2015-08-04T16:48:11.530 回答
0

刚遇到同样的问题。意外的是,我在谷歌中发现了一个叫做“NestedScrollView”的东西。是的。“NestedScrollView”就是答案。使用它 insted 的 ScrollView,并执行您在上面的回答中看到的操作。工作正常 !

于 2015-09-13T21:06:02.583 回答