ScrollView是一种带滚动功能的组件,它采用滑动的方式在有限的区域内显示更多的内容。
ScrollView的自有XML属性
match_viewport:是否拉伸匹配,boolean类型,对应方法setMatchViewportEnabled();
rebound_effect:回弹效果,boolean类型,对应方法setReboundEffect();
常用方法
doFling(int velocityX, int velocityY)
doFlingX(int velocityX)
doFlingY(int velocityY)
设置X轴和Y轴滚动的初始速度,单位(px)
fluentScrollBy(int dx, int dy)
fluentScrollByX(int dx)
fluentScrollByY(int dy)
根据像素数平滑滚动到指定位置,单位(px)
fluentScrollTo(int x, int y)
fluentScrollXTo(int x)
fluentScrollYTo(int y)
根据指定坐标平滑滚动到指定位置,单位(px)
setReboundEffect(boolean enabled)
设置是否启用回弹效果,默认false
setReboundEffectParams(int overscrollPercent, float overscrollRate, int remainVisiblePercent)
setReboundEffectParams(ReboundEffectParams reboundEffectParams)
setOverscrollPercent(int overscrollPercent)
setOverscrollRate(float overscrollRate)
setRemainVisiblePercent(int remainVisiblePercent)
配置回弹效果
overscrollPercent:过度滚动百分比,默认值40
overscrollRate:过度滚动率,默认值0.6
remainVisiblePercent:应保持可见内容的最小百分比,默认值20
竖向滑动子控件高设置为match_content,宽设置为match_parent
横向滑动子控件宽设置为match_content,高设置为match_parent
横竖都滑动子控件宽高设置为match_content
示例代码
<ScrollView
ohos:id="$+id:sv"
ohos:height="match_parent"
ohos:width="match_parent"
ohos:rebound_effect="true">
<!--内部可以有多个控件,最好只设置一个,实际滑动的是子控件,ScrollView自身没有设置布局方向的属性,所以需要在其子布局中设置。-->
<DirectionalLayout
ohos:id="$+id:layout"
ohos:height="match_content"
ohos:width="match_parent"
ohos:orientation="vertical">
<Image
ohos:height="match_content"
ohos:width="300vp"
ohos:image_src="$media:plant"
ohos:top_margin="16vp"/>
<!-- 放置任意需要展示的组件 -->
</DirectionalLayout>
</ScrollView>
效果图:
回弹效果图: