ohos扩展包——CoordinatorLayout & AppBarLayout

jacksky
发布于 2021-9-29 18:24
9183浏览
0收藏

CoordinatorLayout

CoordinatorLayout实现了NestedScrollingParent接口,具体用法与Android中一致。

使用

参考其他CoordinatorLayout教程

CoordinatorLayout可以为每个子控件设置behavior,通过layout_behavior属性设置,取值为behavior类的全路径(内部类可以使用$符号),如:

<com.ryan.ohos.extension.nested.component.coordinatorlayout.CoordinatorLayout
    xmlns:ohos="http://schemas.huawei.com/res/ohos"
    ohos:height="match_parent"
    ohos:width="match_parent">

    <com.ryan.ohos.extension.nested.component.AppBarLayout
        ohos:height="match_content"
        ohos:width="match_parent">

        ....

    </com.ryan.ohos.extension.nested.component.AppBarLayout>

    <com.ryan.ohos.extension.nested.component.NestedListContainer
        ohos:height="match_parent"
        ohos:width="match_parent"
        ohos:background_element="#fff"
        ohos:layout_behavior="com.ryan.ohos.extension.nested.component.AppBarLayout$ScrollingViewBehavior" />a

</com.ryan.ohos.extension.nested.component.coordinatorlayout.CoordinatorLayout>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.

未实现功能

  • Anchor
  • Gravity
  • Layout direction
  • Window inset
  • Keyline/Keylines
  • PreDrawListener
  • ViewElevationComparator
  • StatusBar background
  • Draw behavior Scrim
  • Behavior.onRequestChildRectangleOnScreen
  • onRestoreInstanceState/onSaveInstanceState

 

AppBarLayout

AppBarLayout中提供了两个CoordinatorLayout.Behavior的实现,具体用法与Android中一致。

使用

参考其他AppBarLayout教程。

AppBarLayout最重要的属性是在子控件中的layout_scrollFlags,如下:

    <com.ryan.ohos.extension.nested.component.AppBarLayout
        ohos:height="match_content"
        ohos:width="match_parent">

        <com.ryan.ohos.extension.widget.toolbar.Toolbar
            ohos:height="match_content"
            ohos:width="match_parent"
            ohos:title="测试"
            ohos:gravity="center"
            ohos:background_element="$color:primaryColor"
            ohos:layout_scrollFlags="scroll|enterAlways"/>

    </com.ryan.ohos.extension.nested.component.AppBarLayout>

    <com.ryan.ohos.extension.nested.component.NestedScrollView
            ohos:height="match_parent"
            ohos:width="match_parent"
            ohos:background_element="#fff"
            ohos:layout_behavior="com.ryan.ohos.extension.nested.component.AppBarLayout$ScrollingViewBehavior">
    
            ...
    
        </com.ryan.ohos.extension.nested.component.NestedScrollView>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.

 

layout_scrollFlags有5种取值:

  • scroll 子View设置为scroll时,这个View将会随着可滚动View一起滚动,就好像子View是属于可滚动View的一部分一样。没有这个属性则不会跟着可滚动View一起滚动。

  • enterAlways 子View设置为enterAlways时, 当可滚动View向下滑动时,子View将直接向下滑动,而不管可滚动View是否在滑动。注意:要与scroll搭配使用,否者是不能滑动的,即scroll|enterAlways

  • enterAlwaysCollapsed enterAlwaysCollapsed是对enterAlways的补充,当可滚动View向下滑动的时候,子View(也就是设置了enterAlwaysCollapsed的View)下滑至折叠的高度,当可滚动View到达滑动范围的结束值的时候,子View剩下的部分开始滑动。这个折叠的高度是通过Component的minHeight(最小高度)指定的。注意:layout_scrollFlags需要设置成scroll|enterAlways|enterAlwaysCollapsed才能生效

  • exitUntilCollapsed 当可滚动View滑出屏幕时(也就时向上滑动时),子View先响应滑动事件,滑动至折叠高度,也就是通过minHeight设置的最小高度后,就固定不动了,再把滑动事件交给可滚动View继续滑动。layout_scrollFlags需要设置为scroll|exitUntilCollapsed

  • snap 在滚动结束后,如果子view只是部分可见,它将滑动到最近的边界。比如,如果view的底部只有25%可见,它将滚动离开屏幕,而如果底部有75%可见,它将滚动到完全显示。layout_scrollFlags需要设置为scroll|snap

具体效果可以自行实验

未实现功能

  • Theme
  • Elevation
  • Android_keyboardNavigationCluster
  • Android_touchscreenBlocksFocus
  • LiftOnScroll
  • StatusBarForeground

已于2021-9-29 18:27:17修改
收藏
回复
举报
1
1条回复
按时间正序
/
按时间倒序
mb60fe1583666eb
mb60fe1583666eb

你好有git链接吗?想看看学习一下 只有xml 介绍 看不明白

已于2021-11-16 11:59:08修改
回复
2021-11-16 11:58:00


回复
    相关推荐