【问题标题】:Programmatically scrolling to specific view in CoordinatorLayout, or sticky behaviour以编程方式滚动到 CoordinatorLayout 中的特定视图,或粘滞行为
【发布时间】:2019-12-12 14:46:39
【问题描述】:

我有一个 CoordinatorLayout,我需要以编程方式垂直滚动以将特定视图作为粘性视图锚定到顶部。粘性通过在我想要粘性的视图上设置一个带有app:layout_scrollFlags="scroll|snap" 的 AppBarLayout 来工作。 CoordinatorLayout 的编程滚动被证明是有问题的。

CoordinatorLayout内容的结构是:

<com.google.android.material.appbar.AppBarLayout
 …   
</com.google.android.material.appbar.AppBarLayout>

<!--list of reviews-->
<RecyclerView
    ../>


<androidx.constraintlayout.widget.ConstraintLayout
    ..
</androidx.constraintlayout.widget.ConstraintLayout>

<com.google.android.material.floatingactionbutton.FloatingActionButton
   .. />

我尝试过scrollToscrollBy,但似乎无法计算出正确的 Y 值。我认为 height - view.top 可以,但它没有。

我尝试过包裹在 ScrollView 中,但这会导致同一布局中的 RecyclerView 消失,并且 smoothScroll 无论如何也不起作用。

我试过了

val view = mLayout.findViewById<ConstraintLayout>(R.id.view)
val rect = Rect(0, 0, view.getWidth(), view.getHeight())
view.requestRectangleOnScreen(rect, true) // and tried false

我试过view.parent.requestChildFocus(view, view)

--

我现在正在尝试一种新方法,我正在尝试将视图设置在需要滚动到 View.GONE 的视图之上——这可行,除了将上面的视图设置为 GONE 会导致锚定视图丢失它的“粘性”。我尝试在它上面添加这个视图,但是一旦我将上面的视图设置为GONE,粘性就不起作用了。

<View
        android:id="@+id/sticky_helper"
        android:layout_width="wrap_content"
        android:layout_height="1dp"
        app:layout_scrollFlags="scroll|snap"/>

但是,当我将视图设置为 INVISIBLE 时,粘性仍然有效,因此即使上面的视图仍然可见,它似乎与将它们设置为 GONE 有关。

如果有人对粘性有解决方法,或者确实知道如何滚动到 CoordinatorLayout 中的特定视图,我将不胜感激。

【问题讨论】:

  • CoordinatorLayout 嵌套内容使用什么布局?
  • @MaratZangiev 请在我的问题中查看更新。
  • 你想滚动RecyclerView吗?或者到您的CoordinatorLayout-s 内部的任何视图?
  • @MaratZangiev 不是RecyclerView。我想滚动到 AppBarLayout 内的视图。

标签: android android-xml android-coordinatorlayout coordinator-layout


【解决方案1】:

希望对您有所帮助:

  1. 将您的 RecyclerView 滚动到 0 位置:

    recyclerView.smoothScrollToPosition(0)

  2. 扩展您的AppBarLayout

    app_bar.setExpanded(true, true)

因为我不知道你的 AppBarLayout 里面有什么,所以不能说如何以正确的方式滚动它:) 但是你的 AppBarLayout 将被扩展并且所有的视图都将被显示

【讨论】:

  • 非常感谢,我已经尝试过类似的方法,但没有给出我需要的确切行为。我现在有了想要使用 CollapsingToolbarLayout 的行为。
猜你喜欢
  • 2016-02-25
  • 1970-01-01
  • 1970-01-01
  • 2018-11-22
  • 1970-01-01
  • 2011-05-10
  • 2017-06-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多