Android中NestedScrolling滑動(dòng)機(jī)制詳解
1,如今NestedScrolling運(yùn)用到很多地方了,要想好看一點(diǎn)的滑動(dòng)變換,基本上就是使用這個(gè)來(lái)完成的,讓我們來(lái)簡(jiǎn)單的了解一下。
2,NestedScrolling機(jī)制能夠讓父View和子View在滾動(dòng)式進(jìn)行配合,其基本流程如下:
- 當(dāng)子view開(kāi)始滾動(dòng)之前,可以通知父View,讓其先于自己進(jìn)行滾動(dòng);
- 子View自己進(jìn)行滾動(dòng);
- 子view滾動(dòng)之后,還可以通知父view繼續(xù)滾動(dòng)。
而要實(shí)現(xiàn)這樣的交互機(jī)制,首先父view要實(shí)現(xiàn)NestedScrollingParent接口,而子View需要實(shí)現(xiàn)N恩斯特大S從rollingChild接口,在這套機(jī)制中子View是發(fā)起者,父view是接受回調(diào)并做出響應(yīng)的。
一下是幾個(gè)關(guān)鍵的類(lèi)和接口
//主要接口 NestedScrollingChild NestedScrollingParent //幫助類(lèi) NestedScrollingChildHelper NestedScrollingParentHelper
一些新的系統(tǒng)View已經(jīng)幫我們實(shí)現(xiàn)了以上兩個(gè)接口,也就是說(shuō)他們是支持NestedScrolling,例如:
NestedScrollView已經(jīng)實(shí)現(xiàn)了NestedScrollingChild和NestedScrollingParent兩個(gè)接口
RecycleView已經(jīng)實(shí)現(xiàn)了NestedScrollingChild
CoordinatorLayout實(shí)現(xiàn)了NestedScrollingParent
NestedScrollingChild接口
//開(kāi)始、停止嵌套滾動(dòng) public boolean startNestedScroll(int axes); public void stopNestedScroll(); //觸摸滾動(dòng)相關(guān) public boolean dispatchNestedPreScroll(int dx, int dy, int[] consumed, int[] offsetInWindow); public boolean dispatchNestedScroll(int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed, int[] offsetInWindow); //慣性滾動(dòng)相關(guān) public boolean dispatchNestedPreFling(float velocityX, float velocityY); public boolean dispatchNestedFling(float velocityX, float velocityY, boolean consumed);
public boolean startNestedScroll(int axes);
開(kāi)啟嵌套滾動(dòng)流程(實(shí)際上是進(jìn)行了一些嵌套滾動(dòng)前準(zhǔn)備工作)。
當(dāng)找到了能夠配合當(dāng)前子view進(jìn)行嵌套滾動(dòng)的父view時(shí),返回值為true(Returns:true if a cooperative parent was found and nested scrolling has been enabled for the current gesture)。
public boolean dispatchNestedPreScroll(int dx, int dy, int[] consumed, int[] offsetInWindow);
在子view自己進(jìn)行滾動(dòng)之前調(diào)用此方法,詢問(wèn)父view是否要在子view之前進(jìn)行滾動(dòng)。
此方法的前兩個(gè)參數(shù)用于告訴父View此次要滾動(dòng)的距離;而第三第四個(gè)參數(shù)用于子view獲取父view消費(fèi)掉的距離和父view位置的偏移量。
第一第二個(gè)參數(shù)為輸入?yún)?shù),即常規(guī)的函數(shù)參數(shù),調(diào)用函數(shù)的時(shí)候我們需要為其傳遞確切的值。而第三第四個(gè)參數(shù)為輸出參數(shù),調(diào)用函數(shù)時(shí)我們只需要傳遞容器(在這里就是兩個(gè)數(shù)組),在調(diào)用結(jié)束后,我們就可以從容器中獲取函數(shù)輸出的值。
如果parent消費(fèi)了一部分或全部距離,則此方法返回true。
public boolean dispatchNestedScroll(int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed, int[] offsetInWindow);
在子view自己進(jìn)行滾動(dòng)之后調(diào)用此方法,詢問(wèn)父view是否還要進(jìn)行余下(unconsumed)的滾動(dòng)。
前四個(gè)參數(shù)為輸入?yún)?shù),用于告訴父view已經(jīng)消費(fèi)和尚未消費(fèi)的距離,最后一個(gè)參數(shù)為輸出參數(shù),用于子view獲取父view位置的偏移量。
返回值:(翻譯出來(lái)可能有歧義,直接放原文)true if the event was dispatched, false if it could not be dispatched.
public void stopNestedScroll();
最后,stopNestedScroll()方法與startNestedScroll(int axes)對(duì)應(yīng),用于結(jié)束嵌套滾動(dòng)流程;而慣性滾動(dòng)相關(guān)的兩個(gè)方法與觸摸滾動(dòng)相關(guān)的兩個(gè)方法類(lèi)似,這里不再贅述。
NestedScrollingParent接口概述
//當(dāng)開(kāi)啟、停止嵌套滾動(dòng)時(shí)被調(diào)用 public boolean onStartNestedScroll(View child, View target, int nestedScrollAxes); public void onNestedScrollAccepted(View child, View target, int nestedScrollAxes); public void onStopNestedScroll(View target); //當(dāng)觸摸嵌套滾動(dòng)時(shí)被調(diào)用 public void onNestedPreScroll(View target, int dx, int dy, int[] consumed); public void onNestedScroll(View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed); //當(dāng)慣性嵌套滾動(dòng)時(shí)被調(diào)用 public boolean onNestedPreFling(View target, float velocityX, float velocityY); public boolean onNestedFling(View target, float velocityX, float velocityY, boolean consumed);
從命名可以看出,這幾個(gè)都是回調(diào)方法。當(dāng)調(diào)用NestedScrollingChild中的方法時(shí),NestedScrollingParent中與之相對(duì)應(yīng)的方法就會(huì)被回調(diào)。方法之間的具體對(duì)應(yīng)關(guān)系如下:

從上面的接口還有方法我們可以得出一些簡(jiǎn)單的流程
- 調(diào)用child的startNestedScroll()來(lái)發(fā)起嵌套滑動(dòng)流程(實(shí)質(zhì)上是尋找能夠配合child進(jìn)行嵌套滾動(dòng)的parent)。parent的onStartNestedScroll()會(huì)被調(diào)用,若此方法返回true,則OnNestScrollAccepted()也會(huì)被調(diào)用。
- chuld每次滾動(dòng)前,可以先詢問(wèn)parent是否要滾動(dòng),即調(diào)用dispatchNestedScroll(),這時(shí)可以回調(diào)到parent的OnNestedPreScroll(),parent可以在這個(gè)回調(diào)中先于child滾動(dòng)。
- dispatchNestedPreScroll()之后,child可以進(jìn)行自己的滾動(dòng)操作。
3,自定義NestedScrolling控件
先看一下效果
先看一下布局文件activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<com.qianmo.mynestedscrolling.view.MyNestedScrollParent
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#f0f"
android:text="上面的圖片會(huì)被隱藏,而這個(gè)文字不會(huì)被隱藏"/>
<com.qianmo.mynestedscrolling.view.MyNestedScrollChild
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="123\n456\n789\n111\n222\n333\n444\n555\n666\n777\n888\n999\n14\n12\n13\n44\n55\n66\n77\n88\n99\n11\n22\n33\n44\n55\n66\n77\n88\n99\n77\n88\n88\n8\n88\n88\n"
android:textColor="#f0f"
android:textSize="20sp"/>
</com.qianmo.mynestedscrolling.view.MyNestedScrollChild>
</com.qianmo.mynestedscrolling.view.MyNestedScrollParent>
</RelativeLayout>
布局文件只是簡(jiǎn)單的嵌套,MyNestedScrollParent繼承Linearlayout,并實(shí)現(xiàn)NestedScrollingParent接口,MyNestedScrollChild同理,先來(lái)看看MyNestedScrollChild這個(gè)類(lèi)吧。
MyNestedScrollChild.java
package com.qianmo.mynestedscrolling.view;
import android.content.Context;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v4.view.NestedScrollingChild;
import android.support.v4.view.NestedScrollingChildHelper;
import android.support.v4.view.ViewCompat;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.LinearLayout;
public class MyNestedScrollChild extends LinearLayout implements NestedScrollingChild {
private NestedScrollingChildHelper mNestedScrollingChildHelper;
private final int[] offset = new int[2]; //偏移量
private final int[] consumed = new int[2]; //消費(fèi)
private int lastY;
private int showHeight;
public MyNestedScrollChild(Context context) {
super(context);
}
public MyNestedScrollChild(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//第一次測(cè)量,因?yàn)椴季治募懈叨仁莣rap_content,因此測(cè)量模式為atmost,即高度不超過(guò)父控件的剩余空間
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
showHeight = getMeasuredHeight();
//第二次測(cè)量,對(duì)稿哦度沒(méi)有任何限制,那么測(cè)量出來(lái)的就是完全展示內(nèi)容所需要的高度
heightMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
//按下
case MotionEvent.ACTION_DOWN:
lastY = (int) event.getRawY();
break;
//移動(dòng)
case MotionEvent.ACTION_MOVE:
int y = (int) (event.getRawY());
int dy = y - lastY;
lastY = y;
if (startNestedScroll(ViewCompat.SCROLL_AXIS_HORIZONTAL)
&& dispatchNestedPreScroll(0, dy, consumed, offset)) //如果找到了支持嵌套滑動(dòng)的父類(lèi),父類(lèi)進(jìn)行了一系列的滑動(dòng)
{
//獲取滑動(dòng)距離
int remain = dy - consumed[1];
if (remain != 0) {
scrollBy(0, -remain);
}
} else {
scrollBy(0, -dy);
}
break;
}
return true;
}
//限制滾動(dòng)范圍
@Override
public void scrollTo(int x, int y) {
int maxY = getMeasuredHeight() - showHeight;
if (y > maxY) {
y = maxY;
}
if (y < 0) {
y = 0;
}
super.scrollTo(x, y);
}
//初始化helper對(duì)象
private NestedScrollingChildHelper getScrollingChildHelper() {
if (mNestedScrollingChildHelper == null) {
mNestedScrollingChildHelper = new NestedScrollingChildHelper(this);
mNestedScrollingChildHelper.setNestedScrollingEnabled(true);
}
return mNestedScrollingChildHelper;
}
//實(shí)現(xiàn)一下接口
@Override
public void setNestedScrollingEnabled(boolean enabled) {
getScrollingChildHelper().setNestedScrollingEnabled(enabled);
}
@Override
public boolean isNestedScrollingEnabled() {
return getScrollingChildHelper().isNestedScrollingEnabled();
}
@Override
public boolean startNestedScroll(int axes) {
return getScrollingChildHelper().startNestedScroll(axes);
}
@Override
public void stopNestedScroll() {
getScrollingChildHelper().stopNestedScroll();
}
@Override
public boolean hasNestedScrollingParent() {
return getScrollingChildHelper().hasNestedScrollingParent();
}
@Override
public boolean dispatchNestedScroll(int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed, int[] offsetInWindow) {
return getScrollingChildHelper().dispatchNestedScroll(dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed, offsetInWindow);
}
@Override
public boolean dispatchNestedPreScroll(int dx, int dy, int[] consumed, int[] offsetInWindow) {
return getScrollingChildHelper().dispatchNestedPreScroll(dx, dy, consumed, offsetInWindow);
}
@Override
public boolean dispatchNestedFling(float velocityX, float velocityY, boolean consumed) {
return getScrollingChildHelper().dispatchNestedFling(velocityX, velocityY, consumed);
}
@Override
public boolean dispatchNestedPreFling(float velocityX, float velocityY) {
return getScrollingChildHelper().dispatchNestedPreFling(velocityX, velocityY);
}
}
主要是在OnTouchEvent中先后調(diào)用了startNestedScroll()和dispatchNestedPreScroll()方法,在借助helper來(lái)完成NestedScrollingParent接口方法
MyNestedScrollParent.java
package com.qianmo.mynestedscrolling.view;
import android.content.Context;
import android.support.v4.view.NestedScrollingParent;
import android.support.v4.view.NestedScrollingParentHelper;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
/**
* Created by wangjitao on 2017/2/14 0014.
* 嵌套滑動(dòng)機(jī)制父View
*/
public class MyNestedScrollParent extends LinearLayout implements NestedScrollingParent {
private ImageView img;
private TextView tv;
private MyNestedScrollChild myNestedScrollChild;
private NestedScrollingParentHelper mNestedScrollingParentHelper;
private int imgHeight;
private int tvHeight;
public MyNestedScrollParent(Context context) {
super(context);
}
public MyNestedScrollParent(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private void init() {
mNestedScrollingParentHelper = new NestedScrollingParentHelper(this);
}
//獲取子view
@Override
protected void onFinishInflate() {
img = (ImageView) getChildAt(0);
tv = (TextView) getChildAt(1);
myNestedScrollChild = (MyNestedScrollChild) getChildAt(2);
img.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
if (imgHeight <= 0) {
imgHeight = img.getMeasuredHeight();
}
}
});
tv.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
if (tvHeight <= 0) {
tvHeight = tv.getMeasuredHeight();
}
}
});
}
//在此可以判斷參數(shù)target是哪一個(gè)子view以及滾動(dòng)的方向,然后決定是否要配合其進(jìn)行嵌套滾動(dòng)
@Override
public boolean onStartNestedScroll(View child, View target, int nestedScrollAxes) {
if (target instanceof MyNestedScrollChild) {
return true;
}
return false;
}
@Override
public void onNestedScrollAccepted(View child, View target, int nestedScrollAxes) {
mNestedScrollingParentHelper.onNestedScrollAccepted(child, target, nestedScrollAxes);
}
@Override
public void onStopNestedScroll(View target) {
mNestedScrollingParentHelper.onStopNestedScroll(target);
}
//先于child滾動(dòng)
//前3個(gè)為輸入?yún)?shù),最后一個(gè)是輸出參數(shù)
@Override
public void onNestedPreScroll(View target, int dx, int dy, int[] consumed) {
if (showImg(dy) || hideImg(dy)) {//如果需要顯示或隱藏圖片,即需要自己(parent)滾動(dòng)
scrollBy(0, -dy);//滾動(dòng)
consumed[1] = dy;//告訴child我消費(fèi)了多少
}
}
//后于child滾動(dòng)
@Override
public void onNestedScroll(View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) {
}
//返回值:是否消費(fèi)了fling
@Override
public boolean onNestedPreFling(View target, float velocityX, float velocityY) {
return false;
}
//返回值:是否消費(fèi)了fling
@Override
public boolean onNestedFling(View target, float velocityX, float velocityY, boolean consumed) {
return false;
}
@Override
public int getNestedScrollAxes() {
return mNestedScrollingParentHelper.getNestedScrollAxes();
}
//下拉的時(shí)候是否要向下滾動(dòng)以顯示圖片
public boolean showImg(int dy) {
if (dy > 0) {
if (getScrollY() > 0 && myNestedScrollChild.getScrollY() == 0) {
return true;
}
}
return false;
}
//上拉的時(shí)候,是否要向上滾動(dòng),隱藏圖片
public boolean hideImg(int dy) {
if (dy < 0) {
if (getScrollY() < imgHeight) {
return true;
}
}
return false;
}
//scrollBy內(nèi)部會(huì)調(diào)用scrollTo
//限制滾動(dòng)范圍
@Override
public void scrollTo(int x, int y) {
if (y < 0) {
y = 0;
}
if (y > imgHeight) {
y = imgHeight;
}
super.scrollTo(x, y);
}
}
MyNestedScrollParent主要是實(shí)現(xiàn)一下功能
①、在onStartNestedScroll()中判斷參數(shù)target是哪一個(gè)子view以及滾動(dòng)的方向,然后決定是否要配合其進(jìn)行嵌套滾動(dòng)
②、在onNestedPreScroll()中獲取需要滾動(dòng)的距離,根據(jù)情況決定自己是否要進(jìn)行滾動(dòng),最后還要將自己滾動(dòng)消費(fèi)掉的距離存儲(chǔ)在consumed數(shù)組中回傳給child
就這樣基本實(shí)現(xiàn)了,很簡(jiǎn)單有沒(méi)有,再看看我們接下來(lái)要實(shí)現(xiàn)的效果,如圖:

上源碼 :MyNestedScrolling_jb51.rar
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android RecyclerView滑動(dòng)刪除和拖動(dòng)排序
這篇文章主要介紹了Android RecyclerView滑動(dòng)刪除和拖動(dòng)排序的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-07-07
PC版與Android手機(jī)版帶斷點(diǎn)續(xù)傳的多線程下載
這篇文章主要介紹了PC版與Android手機(jī)版帶斷點(diǎn)續(xù)傳的多線程下載的相關(guān)資料,需要的朋友可以參考下2015-10-10
Android WebView實(shí)現(xiàn)長(zhǎng)按保存圖片及長(zhǎng)按識(shí)別二維碼功能
本文要使用Android WebView實(shí)現(xiàn)長(zhǎng)按保存圖片及長(zhǎng)按識(shí)別二維碼功能,當(dāng)用戶在瀏覽網(wǎng)頁(yè)的時(shí)候,長(zhǎng)按某一區(qū)域,識(shí)別如果是圖片,則彈出彈框,出現(xiàn)保存圖片的功能2018-01-01
Android編程向服務(wù)器發(fā)送請(qǐng)求時(shí)出現(xiàn)中文亂碼問(wèn)題的解決方法
這篇文章主要介紹了Android編程向服務(wù)器發(fā)送請(qǐng)求時(shí)出現(xiàn)中文亂碼問(wèn)題的解決方法,實(shí)例分析了Android參數(shù)傳遞過(guò)程中中文亂碼的解決技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11
詳解android 通過(guò)uri獲取bitmap圖片并壓縮
這篇文章主要介紹了詳解android 通過(guò)uri獲取bitmap圖片并壓縮的相關(guān)資料,希望通過(guò)本文能幫助到大家,讓大家理解這部分內(nèi)容,需要的朋友可以參考下2017-10-10
Android Studio使用小技巧:布局預(yù)覽時(shí)填充數(shù)據(jù)
這篇文章主要介紹了Android Studio使用小技巧:布局預(yù)覽時(shí)填充數(shù)據(jù),本文給出了代碼操作實(shí)例和效果圖例,需要的朋友可以參考下2015-05-05
Android簡(jiǎn)單實(shí)現(xiàn)計(jì)算器功能
這篇文章主要為大家詳細(xì)介紹了Android簡(jiǎn)單實(shí)現(xiàn)計(jì)算器功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08
Android雙向選擇控件DoubleSeekBar使用詳解
這篇文章主要為大家詳細(xì)介紹了Android雙向選擇控件DoubleSeekBar的使用方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-08-08
Android模擬器對(duì)應(yīng)的電腦快捷鍵說(shuō)明
Android模擬器對(duì)應(yīng)的電腦快捷鍵說(shuō)明,需要的朋友可以參考一下2013-06-06

