栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > Android

android嵌套滚动入门实践

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

android嵌套滚动入门实践

嵌套滚动是 Android OS 5.0之后,google 为我们提供的新特性。这种机制打破了我们对之前 Android 传统的事件处理的认知。从一定意义上可以理解为嵌套滚动是逆向的事件传递机制。

如上图所示,其原理就是这样。那么下边我们从代码的层面看一下实现。

代码中主要涉及到了四个类:

NestedScrollingChild、NestedScrollingChildHelper、NestedScrollingParent、NestedScrollingParentHelper

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

public interface NestedScrollingChild {
  
  public void setNestedScrollingEnabled(boolean enabled);

  
  public boolean isNestedScrollingEnabled();

  
  public boolean startNestedScroll(int axes);

  
  public void stopNestedScroll();

  
  public boolean hasNestedScrollingParent();

  
  public boolean dispatchNestedScroll(int dxConsumed, int dyConsumed,
      int dxUnconsumed, int dyUnconsumed, int[] offsetInWindow);

  
  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);
}
NestedScrollingParent 接口中的方法均是与NestedScrollingChild 中的方法一一对应的:
 
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();
}

以上两个类仅仅是定义了功能接口,真正的实现的代码都包含在了NestedScrollingChildHelper和NestedScrollingParentHelper中。

处理流程:

1、当 NestedScrollingChild(下文用Child代替) 要开始滑动的时候会调用 onStartNestedScroll ,然后交给代理类NestedScrollingChildHelper(下文ChildHelper代替)的onStartNestedScroll请求给最近的NestedScrollingParent(下文Parent代替).

2、当ChildHelper的onStartNestedScroll方法 返回 true 表示同意一起处理 Scroll 事件的时候时候,ChildHelper会通知Parent回调onNestedScrollAccepted 做一些准备动作

3、当Child 要开始滑动的时候,会先发送onNestedPreScroll,交给ChildHelper的onNestedPreScroll 请求给Parent ,告诉它我现在要滑动多少距离,你觉得行不行,这时候Parent 根据实际情况告诉Child 现在只允许你滑动多少距离.然后 ChildHelper根据 onNestedPreScroll 中回调回来的信息对滑动距离做相对应的调整.

4、在滑动的过程中 Child 会发送onNestedScroll通知ChildeHelpaer的onNestedScroll告知Parent 当前 Child 的滑动情况.

5、当要进行滑行的时候,会先发送onNestedFling 请求给Parent,告诉它 我现在要滑行了,你说行不行, 这时候Parent会根据情况告诉 Child 你是否可以滑行.

6、Child 通过onNestedFling 返回的 Boolean 值来觉得是否进行滑行.如果要滑行的话,会在滑行的时候发送onNestedFling 通知告知 Parent 滑行情况.

7、当滑动事件结束就会child发送onStopNestedScroll通知 Parent 去做相关操作.

废话不多说了,看代码和 demo 才是正事儿。https://github.com/JeffWangGithub/StickLayout

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/156067.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号