【发布时间】: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
.. />
我尝试过scrollTo、scrollBy,但似乎无法计算出正确的 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