如何通过appbar_scrolling_view_behavior以编程方式设置视图高度?

时间:2020-08-17 17:14:54

标签: java android xml kotlin

我的布局如下。

<CoordinatorLayout>
<AppBarLayout>
<CollapsingToolbarLayout>
<Toolbar>
</CollapsingToolbarLayout>
<View1>
<View2>
</AppBarLayout>
<RecyclerView>(appbar_scrolling_view_behavior)

在一个用例中,我使用一个空状态的Viewholder来显示api中有空项目,而且我还必须相对于screenheight硬编码recyclerview的高度。结果,在完全向上滚动recyclerview时,在recyclerview的空白状态下方还有一些空间。我不需要此空间。有人可以帮我吗?

1 个答案:

答案 0 :(得分:0)

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()
}

在尝试了其他方法后发现了这种方法。

相关问题