国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > Android > 正文

android嵌套滾動(dòng)入門(mén)實(shí)踐

2019-10-22 18:10:19
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

嵌套滾動(dòng)是 Android OS 5.0之后,google 為我們提供的新特性。這種機(jī)制打破了我們對(duì)之前 Android 傳統(tǒng)的事件處理的認(rèn)知。從一定意義上可以理解為嵌套滾動(dòng)是逆向的事件傳遞機(jī)制。

android,嵌套,滾動(dòng)

如上圖所示,其原理就是這樣。那么下邊我們從代碼的層面看一下實(shí)現(xiàn)。

代碼中主要涉及到了四個(gè)類(lèi):

NestedScrollingChild、NestedScrollingChildHelper、NestedScrollingParent、NestedScrollingParentHelper

先看NestedScrollingChild 接口中定義的方法: 

public interface NestedScrollingChild {  /**   * 設(shè)置是否啟用嵌套滾動(dòng)   */  public void setNestedScrollingEnabled(boolean enabled);  /**   * 判斷是否啟用嵌套滾動(dòng)   */  public boolean isNestedScrollingEnabled();  /**   * 開(kāi)始嵌套滾動(dòng)      * @param axes 標(biāo)識(shí)方向,有 x, y 方形和默認(rèn)值0   */  public boolean startNestedScroll(int axes);  /**   * 嵌套滾動(dòng)結(jié)束   */  public void stopNestedScroll();  /**       * 判斷父 view 是否支持嵌套滾動(dòng)   */  public boolean hasNestedScrollingParent();  /**   * 分發(fā)嵌套滾動(dòng),一般再 onTouch/onInterceptTouchEvent/dispatchTouchEvent 中調(diào)用   * @param dxConsumed x軸滾動(dòng)的距離   * @param dyConsumed y 軸滾動(dòng)的距離   * @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);  /**   * 滾動(dòng)之前調(diào)用,進(jìn)行分發(fā)預(yù)滾動(dòng)事件   */  public boolean dispatchNestedPreScroll(int dx, int dy, int[] consumed, int[] offsetInWindow);  /**   * 滑動(dòng)時(shí)調(diào)用 ,分發(fā)滑動(dòng)事件   */  public boolean dispatchNestedFling(float velocityX, float velocityY, boolean consumed);  /**  * 滾動(dòng)之前調(diào)用,分發(fā)預(yù)滾動(dòng)事件   */  public boolean dispatchNestedPreFling(float velocityX, float velocityY);}NestedScrollingParent 接口中的方法均是與NestedScrollingChild 中的方法一一對(duì)應(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è)類(lèi)僅僅是定義了功能接口,真正的實(shí)現(xiàn)的代碼都包含在了NestedScrollingChildHelper和NestedScrollingParentHelper中。

處理流程:

1、當(dāng) NestedScrollingChild(下文用Child代替) 要開(kāi)始滑動(dòng)的時(shí)候會(huì)調(diào)用 onStartNestedScroll ,然后交給代理類(lèi)NestedScrollingChildHelper(下文ChildHelper代替)的onStartNestedScroll請(qǐng)求給最近的NestedScrollingParent(下文Parent代替).

2、當(dāng)ChildHelper的onStartNestedScroll方法 返回 true 表示同意一起處理 Scroll 事件的時(shí)候時(shí)候,ChildHelper會(huì)通知Parent回調(diào)onNestedScrollAccepted 做一些準(zhǔn)備動(dòng)作

3、當(dāng)Child 要開(kāi)始滑動(dòng)的時(shí)候,會(huì)先發(fā)送onNestedPreScroll,交給ChildHelper的onNestedPreScroll 請(qǐng)求給Parent ,告訴它我現(xiàn)在要滑動(dòng)多少距離,你覺(jué)得行不行,這時(shí)候Parent 根據(jù)實(shí)際情況告訴Child 現(xiàn)在只允許你滑動(dòng)多少距離.然后 ChildHelper根據(jù) onNestedPreScroll 中回調(diào)回來(lái)的信息對(duì)滑動(dòng)距離做相對(duì)應(yīng)的調(diào)整.

4、在滑動(dòng)的過(guò)程中 Child 會(huì)發(fā)送onNestedScroll通知ChildeHelpaer的onNestedScroll告知Parent 當(dāng)前 Child 的滑動(dòng)情況.

5、當(dāng)要進(jìn)行滑行的時(shí)候,會(huì)先發(fā)送onNestedFling 請(qǐng)求給Parent,告訴它 我現(xiàn)在要滑行了,你說(shuō)行不行, 這時(shí)候Parent會(huì)根據(jù)情況告訴 Child 你是否可以滑行.

6、Child 通過(guò)onNestedFling 返回的 Boolean 值來(lái)覺(jué)得是否進(jìn)行滑行.如果要滑行的話,會(huì)在滑行的時(shí)候發(fā)送onNestedFling 通知告知 Parent 滑行情況.

7、當(dāng)滑動(dòng)事件結(jié)束就會(huì)child發(fā)送onStopNestedScroll通知 Parent 去做相關(guān)操作.

廢話不多說(shuō)了,看代碼和 demo 才是正事兒。https://github.com/JeffWangGithub/StickLayout

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持VEVB武林網(wǎng)。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到Android開(kāi)發(fā)頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 出国| 清徐县| 太仆寺旗| 临高县| 安溪县| 拜城县| 辽源市| 北宁市| 永德县| 凤城市| 裕民县| 和林格尔县| 克什克腾旗| 清远市| 桐柏县| 田阳县| 贡觉县| 伊宁县| 洪洞县| 彰化市| 富裕县| 阳高县| 永修县| 丰台区| 阳西县| 北京市| 闻喜县| 桐柏县| 冷水江市| 芮城县| 乃东县| 灵山县| 溆浦县| 花莲县| 措勤县| 吉木萨尔县| 平果县| 北辰区| 新巴尔虎右旗| 花莲市| 金昌市|