NestedScrollingParent和NestedScrollingChild

前言:

NestedScrollingParent和NestedScrollingChild是輔助解決事件沖突出現(xiàn)的.在之前的事件攔截中,就算子View的ACTION_MOVE的事件返回false,父布局也是獲取不了該事件的。

NestedScrollingChild的接口定義如下:

  public interface NestedScrollingChild {
    // 參數(shù)enabled:true表示view使用嵌套滾動,false表示禁用.
    public void setNestedScrollingEnabled(boolean enabled);

    public boolean isNestedScrollingEnabled();

    // 參數(shù)axes:表示滾動的方向如:ViewCompat.SCROLL_AXIS_VERTICAL(垂直方向滾動)和
    // ViewCompat.SCROLL_AXIS_HORIZONTAL(水平方向滾動)
    // 返回值:true表示本次滾動支持嵌套滾動,false不支持
    public boolean startNestedScroll(int axes);

    public void stopNestedScroll();

    public boolean hasNestedScrollingParent();

    // 參數(shù)dxConsumed: 表示view消費了x方向的距離長度
    // 參數(shù)dyConsumed: 表示view消費了y方向的距離長度
    // 參數(shù)dxUnconsumed: 表示滾動產(chǎn)生的x滾動距離還剩下多少沒有消費
    // 參數(shù)dyUnconsumed: 表示滾動產(chǎn)生的y滾動距離還剩下多少沒有消費
    // 參數(shù)offsetInWindow: 表示剩下的距離dxUnconsumed和dyUnconsumed使得view在父布局中的位置偏移了多少
    public boolean dispatchNestedScroll(int dxConsumed, int dyConsumed,
            int dxUnconsumed, int dyUnconsumed, int[] offsetInWindow);

    // 參數(shù)dx: 表示view本次x方向的滾動的總距離長度
    // 參數(shù)dy: 表示view本次y方向的滾動的總距離長度
    // 參數(shù)consumed: 表示父布局消費的距離,consumed[0]表示x方向,consumed[1]表示y方向
    // 參數(shù)offsetInWindow: 表示剩下的距離dxUnconsumed和dyUnconsumed使得view在父布局中的位置偏移了多少
    public boolean dispatchNestedPreScroll(int dx, int dy, int[] consumed, int[] offsetInWindow);

    // 這個是滑動的就不詳細分析了
    public boolean dispatchNestedFling(float velocityX, float velocityY, boolean consumed);

    public boolean dispatchNestedPreFling(float velocityX, float velocityY);
}
  • setNestedScrollingEnabled 實現(xiàn)該結(jié)構(gòu)的View要調(diào)用setNestedScrollingEnabled(true)才可以使用嵌套滾動.

  • isNestedScrollingEnabled判斷當(dāng)前view能否使用嵌套滾動.

  • startNestedScroll和stopNestedScroll.是配對使用的.startNestedScroll表示view開始滾動了,一般是在ACTION_DOWN中調(diào)用,如果返回true則表示父布局支持嵌套滾動.在事件結(jié)束比如ACTION_UP或者ACTION_CANCLE中調(diào)用stopNestedScroll,告訴父布局滾動結(jié)束.

  • dispatchNestedScroll,把view消費滾動距離之后,把剩下的滑動距離再次傳給父布局.

  • dispatchNestedPreScroll,在view消費滾動距離之前把總得滑動距離傳給父布局.

  • dispatchNestedFling和dispatchNestedPreFling就是view傳遞滑動的信息給父布局的.

NestedScrollingParent接口的定義如下:

public interface NestedScrollingParent {
    /**
     * 有嵌套滑動到來了,問下該父View是否接受嵌套滑動
     * @param child 嵌套滑動對應(yīng)的父類的子類(因為嵌套滑動對于的父View不一定是一級就能找到的,可能挑了兩級父View的父View,child的輩分>=target)
     * @param target 具體嵌套滑動的那個子類
     * @param nestedScrollAxes 支持嵌套滾動軸。水平方向,垂直方向,或者不指定
     * @return 是否接受該嵌套滑動
     */
    public boolean onStartNestedScroll(View child, View target, int nestedScrollAxes);

    /**
     * 該父View接受了嵌套滑動的請求該函數(shù)調(diào)用。onStartNestedScroll返回true該函數(shù)會被調(diào)用。
     * 參數(shù)和onStartNestedScroll一樣
     */
    public void onNestedScrollAccepted(View child, View target, int nestedScrollAxes);

    /**
     * 停止嵌套滑動
     * @param target 具體嵌套滑動的那個子類
     */
    public void onStopNestedScroll(View target);

    /**
     * 嵌套滑動的子View在滑動之后報告過來的滑動情況
     *
     * @param target 具體嵌套滑動的那個子類
     * @param dxConsumed 水平方向嵌套滑動的子View滑動的距離(消耗的距離)
     * @param dyConsumed 垂直方向嵌套滑動的子View滑動的距離(消耗的距離)
     * @param dxUnconsumed 水平方向嵌套滑動的子View未滑動的距離(未消耗的距離)
     * @param dyUnconsumed 垂直方向嵌套滑動的子View未滑動的距離(未消耗的距離)
     */
    public void onNestedScroll(View target, int dxConsumed, int dyConsumed,
                               int dxUnconsumed, int dyUnconsumed);

    /**
     * 在嵌套滑動的子View未滑動之前告訴過來的準(zhǔn)備滑動的情況
     * @param target 具體嵌套滑動的那個子類
     * @param dx 水平方向嵌套滑動的子View想要變化的距離
     * @param dy 垂直方向嵌套滑動的子View想要變化的距離
     * @param consumed 這個參數(shù)要我們在實現(xiàn)這個函數(shù)的時候指定,回頭告訴子View當(dāng)前父View消耗的距離 
     *                    consumed[0] 水平消耗的距離,consumed[1] 垂直消耗的距離 好讓子view做出相應(yīng)的調(diào)整
     */
    public void onNestedPreScroll(View target, int dx, int dy, int[] consumed);

    /**
     * 嵌套滑動的子View在fling之后報告過來的fling情況
     * @param target 具體嵌套滑動的那個子類
     * @param velocityX 水平方向速度
     * @param velocityY 垂直方向速度
     * @param consumed 子view是否fling了
     * @return true 父View是否消耗了fling
     */
    public boolean onNestedFling(View target, float velocityX, float velocityY, boolean consumed);

    /**
     * 在嵌套滑動的子View未fling之前告訴過來的準(zhǔn)備fling的情況
     * @param target 具體嵌套滑動的那個子類
     * @param velocityX 水平方向速度
     * @param velocityY 垂直方向速度
     * @return true 父View是否消耗了fling
     */
    public boolean onNestedPreFling(View target, float velocityX, float velocityY);

    /**
     * 獲取嵌套滑動的軸
     * @see ViewCompat#SCROLL_AXIS_HORIZONTAL 垂直
     * @see ViewCompat#SCROLL_AXIS_VERTICAL 水平
     * @see ViewCompat#SCROLL_AXIS_NONE 都支持
     */
    public int getNestedScrollAxes();
}
  • onStartNestedScroll.當(dāng)子view的調(diào)用NestedScrollingChild的方法startNestedScroll時,會調(diào)用該方法.

  • onNestedScrollAccepted.如果onStartNestedScroll方法返回的是true的話,那么緊接著就會調(diào)用該方法.它是讓嵌套滾動在開始滾動之前,讓布局容器(viewGroup)或者它的父類執(zhí)行一些配置的初始化的.下面是原文:
    (It offers an opportunity for the view and its superclasses to perform initial configuration for the nested scroll.)

  • onStopNestedScroll停止?jié)L動了,當(dāng)子view調(diào)用stopNestedScroll時會調(diào)用該方法.

  • onNestedScroll,當(dāng)子view調(diào)用dispatchNestedScroll方法時,會調(diào)用該方法.

  • onNestedPreScroll,當(dāng)子view調(diào)用dispatchNestedPreScroll方法是,會調(diào)用該方法.

  • dispatchNestedFling和dispatchNestedPreFling對應(yīng)的就是滑動了.

code :

father:
@SuppressLint("NewApi") public class NestScrollingLayout extends FrameLayout implements NestedScrollingParent{
    
    private static final String TAG = "NestScrollingLayout";
    
    private  NestedScrollingParentHelper mParentHelper;
    
    public NestScrollingLayout(Context context, AttributeSet attrs,
            int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

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

    public NestScrollingLayout(Context context) {
        super(context);
        init();
        
    }
    
    
    @SuppressLint("NewApi") private void init() {
         mParentHelper = new NestedScrollingParentHelper(this);
    }
    @Override
    public boolean onStartNestedScroll(View child, View target, int nestedScrollAxes) {
        
        Log.d(TAG, "child==target:" + (child == target));
        
        Log.d(TAG, "----父布局onStartNestedScroll----------------target:" + target + ",----+++this:" + this);
             
         return (nestedScrollAxes & ViewCompat.SCROLL_AXIS_VERTICAL) != 0;
    }
    @SuppressLint("NewApi") @Override
    public void onNestedScrollAccepted(View child, View target, int nestedScrollAxes) {
        
        Log.d(TAG, "----父布局onNestedScrollAccepted---------------");
        
        mParentHelper.onNestedScrollAccepted(child, target, nestedScrollAxes);
    }
    
    @Override
    public void onStopNestedScroll(View target) {       
        Log.d(TAG, "----父布局onStopNestedScroll----------------");
        mParentHelper.onStopNestedScroll(target);
    }
    // 剩余沒有消費的
    @Override
    public void onNestedScroll(View target, int dxConsumed, int dyConsumed,
            int dxUnconsumed, int dyUnconsumed) {
        Log.d(TAG, "----父布局onNestedScroll----------------");
    }
    
    @Override
    public void onNestedPreScroll(View target, int dx, int dy, int[] consumed) {
        // 這里要消費的
        
        scrollBy(0, -dy);
        
        consumed[0] = 0;
        
        consumed[1] = 10;
        Log.d(TAG, "----父布局onNestedPreScroll----------------");
    }
    
    @Override
    public boolean onNestedFling(View target, float velocityX, float velocityY, boolean consumed) {
        Log.d(TAG, "----父布局onNestedFling----------------");
        return true;
    }
    @Override
    public boolean onNestedPreFling(View target, float velocityX, float velocityY)  {
        Log.d(TAG, "----父布局onNestedPreFling----------------");
        return true;
    }
    @Override
    public int getNestedScrollAxes() {
        Log.d(TAG, "----父布局getNestedScrollAxes----------------");
         return mParentHelper.getNestedScrollAxes();
    }
    
}
children:
public class NestScrollingView extends View implements NestedScrollingChild{

    private static final String TAG = "NestScrollingView";
    
    private NestedScrollingChildHelper mChildHelper;
    
    private int[] mConsumed = new int[2];
    
    private int[] mOffset = new int[2];
    
    public NestScrollingView(Context context, AttributeSet attrs,
            int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
        
    }
    
    public NestScrollingView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public NestScrollingView(Context context) {
        super(context);
        init();
        

    }
    
    private void init() {
        mChildHelper = new NestedScrollingChildHelper(this);
        setNestedScrollingEnabled(true);
    }
    
    @Override
    public void setNestedScrollingEnabled(boolean enabled) {
        mChildHelper.setNestedScrollingEnabled(enabled);
    }
    @Override
    public boolean isNestedScrollingEnabled() {
        return mChildHelper.isNestedScrollingEnabled();
    }
    @Override
    public boolean startNestedScroll(int axes) {
        Log.d(TAG, "-----------startNestedScroll 子View開始滾動---------------");
        return mChildHelper.startNestedScroll(axes);
    }
    @Override
    public void stopNestedScroll() {
        Log.d(TAG, "-----------stopNestedScroll 子View停止?jié)L動---------------");
        mChildHelper.stopNestedScroll();
    }
    @Override
    public boolean hasNestedScrollingParent() {
        return mChildHelper.hasNestedScrollingParent();
    }
    @Override
     public boolean dispatchNestedScroll(int dxConsumed, int dyConsumed,
                int dxUnconsumed, int dyUnconsumed, int[] offsetInWindow) {
        Log.d(TAG, "-----------dispatchNestedScroll 子View把剩余的滾動距離傳給父布局---------------");
        
        return mChildHelper.dispatchNestedScroll(dxConsumed,dyConsumed,
                dxUnconsumed,dyUnconsumed,offsetInWindow);
    }
    @Override
     public boolean dispatchNestedPreScroll(int dx, int dy, int[] consumed, int[] offsetInWindow) {
        Log.d(TAG, "-----------dispatchNestedPreScroll 子View把總的滾動距離傳給父布局---------------");
        return mChildHelper.dispatchNestedPreScroll(dx,dy,
                consumed,offsetInWindow);
        
        
    }
    
    @Override
    public boolean dispatchNestedFling(float velocityX, float velocityY, boolean consumed){
        return mChildHelper.dispatchNestedFling(velocityX,velocityY,
                consumed); 
    }
    @Override
    public boolean dispatchNestedPreFling(float velocityX, float velocityY){
        return mChildHelper.dispatchNestedPreFling(velocityX,velocityY); 
        
    }
    @Override
    public boolean onTouchEvent(MotionEvent event) { 
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            startNestedScroll(ViewCompat.SCROLL_AXIS_VERTICAL);
            break;
        case MotionEvent.ACTION_MOVE:
            
            dispatchNestedPreScroll(0,20,mConsumed,mOffset);

            Log.d(TAG, "offset--x:" + mOffset[0] + ",offset--y:" + mOffset[1]);
            dispatchNestedScroll(50,50,50,50,mOffset);
                    
            break;
        case MotionEvent.ACTION_UP:
            stopNestedScroll();
            break;
        default:
            break;
        }
        return true;
    }
    
}
log:
D/NestScrollingView(12088): -----------stopNestedScroll 子View停止?jié)L動---------------
D/NestScrollingView(12088): -----------startNestedScroll 子View開始滾動---------------
D/NestScrollingLayout(12088): child==target:true
D/NestScrollingLayout(12088): ----父布局onStartNestedScroll----------------target:com.yluo.testnestscrolling.NestScrollingView
D/NestScrollingLayout(12088): ----父布局onNestedScrollAccepted---------------
D/NestScrollingView(12088): -----------dispatchNestedPreScroll 子View把總的滾動距離傳給父布局---------------
D/NestScrollingLayout(12088): ----父布局onNestedPreScroll----------------
D/NestScrollingView(12088): offset--x:0,offset--y:20
D/NestScrollingView(12088): -----------dispatchNestedScroll 子View把剩余的滾動距離傳給父布局---------------
D/NestScrollingLayout(12088): ----父布局onNestedScroll----------------
D/NestScrollingView(12088): -----------dispatchNestedPreScroll 子View把總的滾動距離傳給父布局---------------
D/NestScrollingLayout(12088): ----父布局onNestedPreScroll----------------
D/NestScrollingView(12088): offset--x:0,offset--y:20
D/NestScrollingView(12088): -----------dispatchNestedScroll 子View把剩余的滾動距離傳給父布局---------------
D/NestScrollingLayout(12088): ----父布局onNestedScroll----------------
D/NestScrollingView(12088): -----------dispatchNestedPreScroll 子View把總的滾動距離傳給父布局---------------
D/NestScrollingLayout(12088): ----父布局onNestedPreScroll----------------
D/NestScrollingView(12088): offset--x:0,offset--y:20
D/NestScrollingView(12088): -----------dispatchNestedScroll 子View把剩余的滾動距離傳給父布局---------------
D/NestScrollingLayout(12088): ----父布局onNestedScroll----------------
D/NestScrollingView(12088): -----------dispatchNestedPreScroll 子View把總的滾動距離傳給父布局---------------
D/NestScrollingLayout(12088): ----父布局onNestedPreScroll----------------
D/NestScrollingView(12088): offset--x:0,offset--y:20
D/NestScrollingView(12088): -----------dispatchNestedScroll 子View把剩余的滾動距離傳給父布局---------------
D/NestScrollingLayout(12088): ----父布局onNestedScroll----------------
D/NestScrollingView(12088): -----------dispatchNestedPreScroll 子View把總的滾動距離傳給父布局---------------
D/NestScrollingLayout(12088): ----父布局onNestedPreScroll----------------
D/NestScrollingView(12088): offset--x:0,offset--y:20
D/NestScrollingView(12088): -----------dispatchNestedScroll 子View把剩余的滾動距離傳給父布局---------------
D/NestScrollingLayout(12088): ----父布局onNestedScroll----------------
D/NestScrollingView(12088): -----------dispatchNestedPreScroll 子View把總的滾動距離傳給父布局---------------
D/NestScrollingLayout(12088): ----父布局onNestedPreScroll----------------
D/NestScrollingView(12088): offset--x:0,offset--y:20
D/NestScrollingView(12088): -----------dispatchNestedScroll 子View把剩余的滾動距離傳給父布局---------------
D/NestScrollingLayout(12088): ----父布局onNestedScroll----------------
D/NestScrollingView(12088): -----------dispatchNestedPreScroll 子View把總的滾動距離傳給父布局---------------
D/NestScrollingLayout(12088): ----父布局onNestedPreScroll----------------
D/NestScrollingView(12088): offset--x:0,offset--y:20
D/NestScrollingView(12088): -----------dispatchNestedScroll 子View把剩余的滾動距離傳給父布局---------------
D/NestScrollingLayout(12088): ----父布局onNestedScroll----------------
D/NestScrollingView(12088): -----------dispatchNestedPreScroll 子View把總的滾動距離傳給父布局---------------
D/NestScrollingLayout(12088): ----父布局onNestedPreScroll----------------
D/NestScrollingView(12088): offset--x:0,offset--y:20
D/NestScrollingView(12088): -----------dispatchNestedScroll 子View把剩余的滾動距離傳給父布局---------------
D/NestScrollingLayout(12088): ----父布局onNestedScroll----------------
D/NestScrollingView(12088): -----------dispatchNestedPreScroll 子View把總的滾動距離傳給父布局---------------
D/NestScrollingLayout(12088): ----父布局onNestedPreScroll----------------
D/NestScrollingView(12088): offset--x:0,offset--y:20
D/NestScrollingView(12088): -----------dispatchNestedScroll 子View把剩余的滾動距離傳給父布局---------------
D/NestScrollingLayout(12088): ----父布局onNestedScroll----------------
D/NestScrollingView(12088): -----------dispatchNestedPreScroll 子View把總的滾動距離傳給父布局---------------
D/NestScrollingLayout(12088): ----父布局onNestedPreScroll----------------
D/NestScrollingView(12088): offset--x:0,offset--y:20
D/NestScrollingView(12088): -----------dispatchNestedScroll 子View把剩余的滾動距離傳給父布局---------------
D/NestScrollingLayout(12088): ----父布局onNestedScroll----------------
D/NestScrollingView(12088): -----------stopNestedScroll 子View停止?jié)L動---------------
D/NestScrollingLayout(12088): ----父布局onStopNestedScroll----------------
D/NestScrollingView(12088): -----------stopNestedScroll 子View停止?jié)L動---------------

result:
--------------子View開始滾動------------------
----父布局onStartNestedScroll----------------
----父布局onNestedScrollAccepted---------------
-----------子View把總的滾動距離傳給父布局--------
----父布局onNestedPreScroll----------------
---offset--x:0,offset--y:20
-----------子View把剩余的滾動距離傳給父布局-------
----父布局onNestedScroll----------------
-----------子View停止?jié)L動---------------
----父布局onStopNestedScroll----------------
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容