UI

Android-NestedScrollView

2021-04-27  本文已影响0人  森屿暖茶

NestedScrollView

在新版的support-v4兼容包里面有一个NestedScrollView控件,这个控件其实和普通的ScrollView并没有多大的区别,这个控件其实是Meterial Design中设计的一个控件,目的是跟MD中的其他控件兼容。应该说在MD中,RecyclerView代替了ListView,而NestedScrollView代替了ScrollView,他们两个都可以用来跟ToolBar交互,实现上拉下滑中ToolBar的变化。在NestedScrollView的名字中其实就可以看出他的作用了,Nested是嵌套的意思,而ToolBar基本需要嵌套使用。

此外,它还同时支持NestedScrollingParent或者NestedScrollingChild进行嵌套滚动操作。默认是启用嵌套滚动的。因为NestedScrollView就实现了它们的接口。

NestedScrollView 已经实现了 NestedScrollingChild 和 NestedScrollingParent 两个接口

RecycleView 已经实现了 NestedScrollingChild(所以我们常常可以使用NestedScrollView 嵌套RecycleView ,然后设置RecycleView 不可以滑动解决冲突的问题)

CoordinatorLayout 实现了 NestedScrollingParent

NestedScrolling 机制主要接口实现

NestedScrollingChild接口

源于网络

NestedScrollingParent接口

源于网络

它们的交互机制,父 view(实现NestedScrollingParent) 是接受回调并做出响应的。从方法名就可以看出相应机制是,事件分发给子view(实现NestedScrollingChild),然后在回调父view的方法,父view根据需求作出相应返回告知给子view。

源于网络

总结

在 NestedScrollingChild 处理点击事件,然后发起滚动请求;在 NestedScrollingParent 的 onNestedPreScroll 处理事件,是不是要自己消耗。所以我们常常要做的就是在子view里监听onTouch事件调用父view的一些接口处理,父view根据需要在相应的回调接口里作出响应并返回即可。

帮助类

NestedScrollingChildHelper  :NestedScrollingChild 的辅助类,帮我们实现了滑动的部分设置和方法。

NestedScrollingParentHelper:NestedScrollingParent  的辅助类,帮我们实现了滑动的部分设置和方法。

一位大神的demo。值得参考

https://github.com/hufeiyang/NestedScrollingParent2Layout

上一篇下一篇

猜你喜欢

热点阅读