android嵌套滾動入門實(shí)踐
嵌套滾動是 Android OS 5.0之后,google 為我們提供的新特性。這種機(jī)制打破了我們對之前 Android 傳統(tǒng)的事件處理的認(rèn)知。從一定意義上可以理解為嵌套滾動是逆向的事件傳遞機(jī)制。
如上圖所示,其原理就是這樣。那么下邊我們從代碼的層面看一下實(shí)現(xiàn)。
代碼中主要涉及到了四個(gè)類:
NestedScrollingChild、NestedScrollingChildHelper、NestedScrollingParent、NestedScrollingParentHelper
先看NestedScrollingChild 接口中定義的方法:
public interface NestedScrollingChild { /** * 設(shè)置是否啟用嵌套滾動 */ public void setNestedScrollingEnabled(boolean enabled); /** * 判斷是否啟用嵌套滾動 */ public boolean isNestedScrollingEnabled(); /** * 開始嵌套滾動 * @param axes 標(biāo)識方向,有 x, y 方形和默認(rèn)值0 */ public boolean startNestedScroll(int axes); /** * 嵌套滾動結(jié)束 */ public void stopNestedScroll(); /** * 判斷父 view 是否支持嵌套滾動 */ public boolean hasNestedScrollingParent(); /** * 分發(fā)嵌套滾動,一般再 onTouch/onInterceptTouchEvent/dispatchTouchEvent 中調(diào)用 * @param dxConsumed x軸滾動的距離 * @param dyConsumed y 軸滾動的距離 * @param dxUnconsumed x 軸上未消費(fèi)的距離 * @param dyUnconsumed y 軸上未消費(fèi)的距離 * @param offsetInWindow 子 View 的窗體偏移 */ public boolean dispatchNestedScroll(int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed, int[] offsetInWindow); /** * 滾動之前調(diào)用,進(jìn)行分發(fā)預(yù)滾動事件 */ public boolean dispatchNestedPreScroll(int dx, int dy, int[] consumed, int[] offsetInWindow); /** * 滑動時(shí)調(diào)用 ,分發(fā)滑動事件 */ public boolean dispatchNestedFling(float velocityX, float velocityY, boolean consumed); /** * 滾動之前調(diào)用,分發(fā)預(yù)滾動事件 */ public boolean dispatchNestedPreFling(float velocityX, float velocityY); } NestedScrollingParent 接口中的方法均是與NestedScrollingChild 中的方法一一對應(yīng)的: public interface NestedScrollingParent { public boolean onStartNestedScroll(View child, View target, int nestedScrollAxes); public void onNestedScrollAccepted(View child, View target, int nestedScrollAxes); public void onStopNestedScroll(View target); public void onNestedScroll(View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed); public void onNestedPreScroll(View target, int dx, int dy, int[] consumed); public boolean onNestedFling(View target, float velocityX, float velocityY,boolean consumed); public boolean onNestedPreFling(View target, float velocityX, float velocityY); public int getNestedScrollAxes(); }
以上兩個(gè)類僅僅是定義了功能接口,真正的實(shí)現(xiàn)的代碼都包含在了NestedScrollingChildHelper和NestedScrollingParentHelper中。
處理流程:
1、當(dāng) NestedScrollingChild(下文用Child代替) 要開始滑動的時(shí)候會調(diào)用 onStartNestedScroll ,然后交給代理類NestedScrollingChildHelper(下文ChildHelper代替)的onStartNestedScroll請求給最近的NestedScrollingParent(下文Parent代替).
2、當(dāng)ChildHelper的onStartNestedScroll方法 返回 true 表示同意一起處理 Scroll 事件的時(shí)候時(shí)候,ChildHelper會通知Parent回調(diào)onNestedScrollAccepted 做一些準(zhǔn)備動作
3、當(dāng)Child 要開始滑動的時(shí)候,會先發(fā)送onNestedPreScroll,交給ChildHelper的onNestedPreScroll 請求給Parent ,告訴它我現(xiàn)在要滑動多少距離,你覺得行不行,這時(shí)候Parent 根據(jù)實(shí)際情況告訴Child 現(xiàn)在只允許你滑動多少距離.然后 ChildHelper根據(jù) onNestedPreScroll 中回調(diào)回來的信息對滑動距離做相對應(yīng)的調(diào)整.
4、在滑動的過程中 Child 會發(fā)送onNestedScroll通知ChildeHelpaer的onNestedScroll告知Parent 當(dāng)前 Child 的滑動情況.
5、當(dāng)要進(jìn)行滑行的時(shí)候,會先發(fā)送onNestedFling 請求給Parent,告訴它 我現(xiàn)在要滑行了,你說行不行, 這時(shí)候Parent會根據(jù)情況告訴 Child 你是否可以滑行.
6、Child 通過onNestedFling 返回的 Boolean 值來覺得是否進(jìn)行滑行.如果要滑行的話,會在滑行的時(shí)候發(fā)送onNestedFling 通知告知 Parent 滑行情況.
7、當(dāng)滑動事件結(jié)束就會child發(fā)送onStopNestedScroll通知 Parent 去做相關(guān)操作.
廢話不多說了,看代碼和 demo 才是正事兒。https://github.com/JeffWangGithub/StickLayout
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android中使用Bitmap類將矩形圖片轉(zhuǎn)為圓形的方法
這篇文章主要介紹了Android中使用Bitmap類將矩形圖片轉(zhuǎn)為圓形的方法,同時(shí)文中也介紹了如何利用矩形直接來畫圓角,需要的朋友可以參考下2016-03-03Android使用OKHttp庫實(shí)現(xiàn)視頻文件的上傳到服務(wù)器功能
這篇文章主要介紹了Android使用OKHttp庫實(shí)現(xiàn)視頻文件的上傳到服務(wù)器功能,需要的朋友可以參考下2018-03-03Android啟動頁設(shè)置及動態(tài)權(quán)限跳轉(zhuǎn)問題解決
在我遇到這個(gè)實(shí)際問題之前,我一直認(rèn)為啟動頁的作用是美化產(chǎn)品,提升軟件逼格。但實(shí)際上,它更重要的是起到了一個(gè)攔截器的作用,這篇文章主要介紹了Android啟動頁設(shè)置以及動態(tài)權(quán)限跳轉(zhuǎn),需要的朋友可以參考下2022-04-04在ubuntu下編譯ijkplayer-android的方法
下面小編就為大家分享一篇在ubuntu下編譯ijkplayer-android的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01Android Canvas之drawBitmap方法案例詳解
這篇文章主要介紹了Android Canvas之drawBitmap方法案例詳解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08android中Invalidate和postInvalidate的更新view區(qū)別
Android中實(shí)現(xiàn)view的更新有兩組方法,一組是invalidate,另一組是postInvalidate,其中前者是在UI線程自身中使用,而后者在非UI線程中使用,感興趣的朋友可以了解下哦2013-01-01當(dāng)ListView有Header時(shí) onItemClick里的position不正確的原因
這篇文章主要介紹了當(dāng)ListView有Header時(shí) onItemClick里的position不正確的原因的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-07-07