我有一个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,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中的特定视图,我将不胜感激。