Android嵌套滑动机制

Tags
notion image

概览

实现嵌套滑动有三种方案:
  1. 纯事件拦截与派发方案
  1. 基于NestingScroll机制的实现方案
  1. 基于CoordinatorLayout与Behavior的实现方案
第一种方案:灵活性最高,也最繁琐。因为事件的拦截是一锤子买卖,谁拦截了事件,当前手势接下来的事件都会交给拦截者来处理,除非等到下一次Down事件触发。这很不方便多个View对同一个事件进行处理。
第二种方案:其实就是对原始的事件拦截机制做了一层封装,通过子View实现NestedScrollingChild接口,父View实现NestedScrollingParent 接口,并且在子View和父View中都分别有一个NestedScrollingChildHelper、NestedScrollingParentHelper来代理了父子之间的联动,开发者不用关心具体是怎么联动的,这一点很方便。
第三种方案:其实就是对原始的NestedScrolling机制再次做了一层封装。CoordinatorLayout默认实现了NestedScrollingParent接口。第二种方案只能由子View通知父View,但有时候除了需要通知父View,还需要通知兄弟View,这个时候就该是Behavior出场了。

Touch事件分发流程

notion image
关于Touch事件分发流程的使用示例:欢迎查看我的博客:自定义下拉刷新和上拉加载框架

NestedScrolling简要流程

具体流程可以查看NestedScrollingChildHelper等类的源码
notion image

CoordinatorLayout+ Behavior的流程

notion image
关于Behavior的使用示例:欢迎查看我的博客:自定义Behavior,实现滑动卡片

NestedScrolling的一个简单示例

效果图

当子View滑动到父View边缘时,会带动父View一起滑动。
http://upload-images.jianshu.io/upload_images/1458573-c8f6892fad0b743f?imageMogr2/auto-orient/strip

实现代码

嵌套滑动的发起者:子View

主要思路:(用scroll来举例)
  1. 实现NestedScrollingChild接口。
  1. 定义NestedScrollingChildHelper变量。
  1. 在实现的NestedScrollingChild每个接口中调用。NestedScrollingChildHelper对应的函数。
  1. setNestedScrollingEnabled(true); 一般在初始化里面调用设置可以嵌套滑动。
  1. onTouchEvent 或者 dispatchTouchEvent 方法里面case ACTION_DOWN 调用startNestedScroll函数 告诉父View开始嵌套滑动。
  1. onTouchEvent 或者 dispatchTouchEvent 方法里面case ACTION_MOVE 调用dispatchNestedPreScroll或者dispatchNestedScroll 这个就视情况而定了告诉父View滑动的情况。
  1. onTouchEvent 或者 dispatchTouchEvent 方法里面case ACTION_UP 调用stopNestedScroll 告诉父View结束嵌套滑动。
  1. 重写onDetachedFromWindow方法,调用NestedScrollingChildHelper的onDetachedFromWindow方法
package com.soubu.sample.nestedscrolling;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v4.view.NestedScrollingChild;
import android.support.v4.view.NestedScrollingChildHelper;
import android.support.v4.view.ViewCompat;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import java.util.Arrays;

/**
 * 嵌套滑动的发起者:子View
 * <p>
 * 作者:余天然 on 2017/3/30 上午10:40
 */
public class NestedChildView extends View implements NestedScrollingChild {

    private final static String TAG = "NestedChildView";

    private float mLastX;//手指在屏幕上最后的x位置
    private float mLastY;//手指在屏幕上最后的y位置

    private float mDownX;//手指第一次落下时的x位置(忽略)
    private float mDownY;//手指第一次落下时的y位置

    private int[] consumed = new int[2];//消耗的距离
    private int[] offsetInWindow = new int[2];//窗口偏移

    private NestedScrollingChildHelper mScrollingChildHelper;

    public NestedChildView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        mScrollingChildHelper = new NestedScrollingChildHelper(this);
        setNestedScrollingEnabled(true);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        float x = ev.getX();
        float y = ev.getY();

        int action = ev.getAction();

        switch (action) {
            case MotionEvent.ACTION_DOWN: {

                mDownX = x;
                mDownY = y;
                mLastX = x;
                mLastY = y;
                //当开始滑动的时候,告诉父view
                startNestedScroll(ViewCompat.SCROLL_AXIS_HORIZONTAL | ViewCompat.SCROLL_AXIS_VERTICAL);
                break;
            }

            case MotionEvent.ACTION_MOVE: {
                /*
                mDownY:293.0
                mDownX:215.0
                 */

                int dy = (int) (y - mDownY);
                int dx = (int) (x - mDownX);

                //分发触屏事

嵌套滑动的处理者:父View

主要思路:
  1. 实现NestedScrollingParent接口。
  1. 定义NestedScrollingParentHelper变量。
  1. 在实现的NestedScrollingParent几个接口中(onNestedScrollAccepted, onStopNestedScroll, getNestedScrollAxes)调用NestedScrollingParentHelper对应的函数。
  1. 视情况而定onNestedScroll onNestedPreScroll onNestedFling onNestedPreFling 做相应的处理。
package com.soubu.sample.nestedscrolling;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.NestedScrollingParent;
import android.support.v4.view.NestedScrollingParentHelper;
import android.support.v4.view.ViewCompat;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;

import java.util.Arrays;

/**
 * 嵌套滑动的处理者:父View
 * <p>
 * 作者:余天然 on 2017/3/30 上午10:42
 */
public class NestedParentLayout extends FrameLayout implements NestedScrollingParent {

    private static final String TAG = "NestedParentLayout";

    private NestedScrollingParentHelper mScrollingParentHelper;

    public NestedParentLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        mScrollingParentHelper = new NestedScrollingParentHelper(this);
    }

    /**
     * 有嵌套滑动到来了,问下该父View是否接受嵌套滑动
     *
     * @param child            嵌套滑动对应的父类的子类(因为嵌套滑动对于的父View不一定是一级就能找到的,可能挑了两级父View的父View,child的辈分>=target)
     * @param target           具体嵌套滑动的那个子类
     * @param nestedScrollAxes 支持嵌套滚动轴。水平方向,垂直方向,或者不指定
     * @return 是否接受该嵌套滑动
     */
    @Override
    public boolean onStartNestedScroll(View child, View target, int nestedScrollAxes) {
        Log.d(TAG, "onStartNestedScroll() called with: " + "child = [" + child + "], target = [" + target + "], nestedScrollAxes = [" + nestedScrollAxes + "]");
        return true;
    }

    /**
     * 该父View接受了嵌套滑动的请求该函数调用。onStartNestedScroll返回true该函数会被调用。
     * 参数和onStartNestedScroll一样
     */
    @Override
    public void onNestedScrollAccepted(View child, View target, int nestedScrollAxes) {
        mScrollingParentHelper.onNestedScrollAccepted(child, target, nestedScrollAxes);
    }

    /**
     * 获取嵌套滑动的轴
     *
     * @see ViewCompat#SCROLL_AXIS_HORIZONTAL 垂直
     * @see ViewCompat#SCROLL_AXIS_VERTICAL 水
本文档由瓦雀创建

© fishyer 2022 - 2023