【问题标题】:Is it possible to programmatically scroll to a child of on appbarlayout?是否可以以编程方式滚动到 appbarlayout 的子级?
【发布时间】:2020-08-19 13:48:08
【问题描述】:
<CoordinatorLayout>
<AppBarLayout>
<CollapsingToolbarLayout>
<Toolbar/>
</CollapsingToolbarLayout>
<View1/>
<View2/>
<View3/>
</AppBarLayout>
<RecyclerView/>(app_bar_scrolling_behavior)
</CoordinatorLayout>
我想以编程方式滚动到 view3 对 recyclerview 中某个项目的某些操作。有什么解决方案吗?
【问题讨论】:
标签:
java
android
kotlin
android-coordinatorlayout
android-appbarlayout
【解决方案1】:
通过使用 value animator,可以使用下面的代码 sn-p 来实现这一点。
`
recyclerview.scrollToPosition(0)
val params = appbar.layoutParams as CoordinatorLayout.LayoutParams
val behavior = params.behavior as AppBarLayout.Behavior?
if (behavior != null) {
val valueAnimator: ValueAnimator = ValueAnimator.ofInt()
valueAnimator.interpolator = DecelerateInterpolator()
valueAnimator.addUpdateListener { animation ->
behavior.topAndBottomOffset = (animation.animatedValue as Int)
appBar.requestLayout()
}
valueAnimator.setIntValues(
0, -appbar.totalScrollRange + view3.root.measuredHeight
)
valueAnimator.duration = 50
valueAnimator.start()
}
`