日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android 中 Behavior, NestedScrollingParent, NestedScrollingChild 关系

發(fā)布時間:2025/5/22 Android 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android 中 Behavior, NestedScrollingParent, NestedScrollingChild 关系 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

NestedScroll 相關接口:NestedScrollingParent, NestScrollingChild

頂層布局需要用CoordinatorLayout, Behavior 是CoordinatorLayout 內部類

public class CoordinatorLayout extends ViewGroup implements NestedScrollingParent2 {public static abstract class Behavior<V extends View> {}}  

NestedScroll 解決了子View 滑動時,父View需要滑動的情況, 子View 把需要滑動的距離傳遞給父View, 父View 可以消耗一部分距離,然后通知子View

子View滑動回調父View的接口:

? ?子View父View
startNestedScroll? ?--開始滑動onStartNestedScroll,onNestedScrollAccepted
dispatchNestedPreScroll? --預滑動onNestedPreScroll
dispatchNestedScroll? ?--滑動onNestedScroll
stopNestedScroll? ?--停止滑動onStopNestedScroll

Behavior 是子View 與父View 之間協調者的角色, 父View的 事件它都有實現,在CoordinatorLayout 的方法實現中,都委托Behavior 進行處理。

@Overridepublic void onNestedScroll(View target, int dxConsumed, int dyConsumed,int dxUnconsumed, int dyUnconsumed) {

     // 遍歷所有子Viewfor (int i = 0; i < childCount; i++) {final View view = getChildAt(i);if (view.getVisibility() == GONE) {// If the child is GONE, skip...continue;}final LayoutParams lp = (LayoutParams) view.getLayoutParams();if (!lp.isNestedScrollAccepted(type)) {continue;}final Behavior viewBehavior = lp.getBehavior();if (viewBehavior != null) {

        //委托給behavior進行處理viewBehavior.onNestedScroll(this, view, target, dxConsumed, dyConsumed,dxUnconsumed, dyUnconsumed, type);accepted = true;}}if (accepted) {onChildViewsChanged(EVENT_NESTED_SCROLL);}}

  

另外還有兩個類:

NestedScrollingChildHelper輔助類
NestedScrollingParentHelper 輔助類

它們是為了適配Android 5.0 之前的版本

前面介紹了NestedScrollingParent, NestedScrollingChild, Behavior 三者之間的關系,下面看下嵌套滑動的常見實現:

1。 Toolbar+ 圖片,圖片滑動上去時,Toolbar顯示

? ? ?CoordinatorLayout + ToolbarLayout + CollapsingToolbarLayout + Toolbar?

2.? ?Toolbar + TabLayout + ViewPager?

?

轉載于:https://www.cnblogs.com/songsh/p/6530024.html

總結

以上是生活随笔為你收集整理的Android 中 Behavior, NestedScrollingParent, NestedScrollingChild 关系的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。