没有合适的资源?快使用搜索试试~ 我知道了~
Android NestedScrolling嵌套滚动的示例代码
0 下载量 142 浏览量
2021-01-20
09:39:07
上传
评论
收藏 59KB PDF 举报
温馨提示
试读
5页
一、什么是NestedScrolling? Android在Lollipop版本中引入了NestedScrolling——嵌套滚动机制。在Android的事件处理机制中,事件序列只能由父View和子View中的一个处理。在嵌套滚动机制中,子View处理事件前会将事件传给父View处理,两者协作配合处理事件。 在嵌套滚动机制中,父View需实现NestedScrollingParent接口,子View需要实现NestedScrollingChild接口。从Lollipop起View都已经实现了NestedScrollingChild的方法。嵌套滚动过程如下: 开始滚动前,子View调用s
资源详情
资源评论
资源推荐
Android NestedScrolling嵌套滚动的示例代码嵌套滚动的示例代码
一、什么是一、什么是NestedScrolling?
Android在Lollipop版本中引入了NestedScrolling——嵌套滚动机制。在Android的事件处理机制中,事件序列只能由父View和
子View中的一个处理。在嵌套滚动机制中,子View处理事件前会将事件传给父View处理,两者协作配合处理事件。
在嵌套滚动机制中,父View需实现NestedScrollingParent接口,子View需要实现NestedScrollingChild接口。从Lollipop起
View都已经实现了NestedScrollingChild的方法。嵌套滚动过程如下:
开始滚动前,子View调用startNestedScroll方法。该方法会调用父View的onStartNestedScroll方法并返回onStartNestedScroll
的值。如果返回true,则表示父View愿意接收后续的滚动事件,此时父View的onNestedScrollAccepted会被调用。该方法一
般是在子View处理DOWN事件时调用。
子View滚动某个距离前,调用dispatchNestedPreScroll方法,把滚动距离传给父View。该方法回调父View的
onNestedPreScroll方法,如果父View需要消耗滚动距离,只需要把需要消耗的距离赋给onNestedPreScroll方法的参数
consumed。该参数是一个数组,consumed[0]表示消耗的水平滚动距离,consumed[1]表示消耗的垂直滚动距离。
dispatchNestedPreScroll返回true则表示父View消耗了部分或者全部滚动距离。
子View滚动某个距离后,调用dispatchNestedScroll方法。如果该方法返回true则表示,子View会调用父View的
onNestedScroll方法,把已消耗和未消耗的滚动距离传给父View。
子View处理Fling事件前,调用dispatchNestedPreFling方法。该方法会调用父View的onNestedPreFling并返回
onNestedPreFling的值。如果true,则表示父View处理消耗了该Fling事件,则子View不应该处理该Fling事件。
如果dispatchNestedPreFling方法返回false,子View在处理Fling事件后会调用dispatchNestedFling方法,该方法会调用父
View的onNestedFling方法。onNestedFling方法返回true表示父View消耗或处理了Fling事件。
当子View停止滚动时,调用stopNestedScroll方法。该方法会调用父View的onStopNestedScroll方法。
上面提及的各个方法的具体用法请参考官方文档。
二、怎么实现二、怎么实现NestedScrollingChild?
Android为NestedScrollingChild提供了一个代理类NestedScrollingChildHelper。所以,NestedScrollingChild的最简单的实现
如下。
public class NestedScrollingChildView extends FrameLayout implements NestedScrollingChild {
private final NestedScrollingChildHelper mChildHelper;
public NestedScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mChildHelper = new NestedScrollingChildHelper(this);
}
@Override
public void setNestedScrollingEnabled(boolean enabled) {
mChildHelper.setNestedScrollingEnabled(enabled);
}
@Override
public boolean isNestedScrollingEnabled() {
return mChildHelper.isNestedScrollingEnabled();
}
@Override
public boolean startNestedScroll(int axes) {
return mChildHelper.startNestedScroll(axes);
}
@Override
public void stopNestedScroll() {
mChildHelper.stopNestedScroll();
}
@Override
public boolean hasNestedScrollingParent() {
return mChildHelper.hasNestedScrollingParent();
}
@Override
public boolean dispatchNestedScroll(int dxConsumed, int dyConsumed, int dxUnconsumed,
int dyUnconsumed, int[] offsetInWindow) {
weixin_38660327
- 粉丝: 8
- 资源: 953
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0