【问题标题】: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()
    }
    

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

    【讨论】:

      猜你喜欢
      • 2017-12-29
      • 2019-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-10
      • 2013-09-03
      • 1970-01-01
      • 2012-01-23
      相关资源
      最近更新 更多