【问题标题】:How to programatically set height of view with appbar_scrolling_view_behavior?如何使用 appbar_scrolling_view_behavior 以编程方式设置视图高度?
【发布时间】:2020-08-17 17:14:54
【问题描述】:
我有以下布局。
<CoordinatorLayout>
<AppBarLayout>
<CollapsingToolbarLayout>
<Toolbar>
</CollapsingToolbarLayout>
<View1>
<View2>
</AppBarLayout>
<RecyclerView>(appbar_scrolling_view_behavior)
在一个用例中,我使用一个空状态视图来显示来自 api 的空项目。此外,我必须硬编码 recyclerview 相对于屏幕高度的高度。结果,在完全向上滚动 recyclerview 时,在 recyclerview 的空状态下方留下了一些空间。我不想要这个空间。有人可以帮我吗?
【问题讨论】:
标签:
java
android
xml
kotlin
【解决方案1】:
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 + view2.measuredHeight
)
valueAnimator.duration = 50
valueAnimator.start()
}
在尝试其他方法后发现这种方法可行。