![](/img/trans.png)
[英]Unable to scroll collapsing toolbar with recyclerview using fragment
[英]How to store and restore the scroll position of collapsing toolbar during fragment changes?
我想要的是:在切换片段时保存折叠工具栏的滚动位置。
现在工具栏会重新创建并且不会保存最后滚动的位置,但 recyclerView 会。 如何保存折叠工具栏滚动位置?
布局.xml
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/f_c_coordinator_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/f_c_app_bar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/f_c_collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:titleEnabled="false"
app:layout_scrollFlags="scroll">
<android.support.v7.widget.Toolbar
android:id="@+id/f_c_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:titleTextColor="@android:color/white"
app:layout_scrollFlags="scroll|enterAlways"
app:layout_collapseMode="parallax"
android:fitsSystemWindows="true"
app:popupTheme="@style/ThemeOverlay.AppCompat.Dark"/>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
....
/>
</android.support.design.widget.CoordinatorLayout>
在查看 StackOverflow 几天后,我实际上找到了解决方案。
我不确定是什么导致了这个问题,但是在活动之间切换时,CollapsingToolbar 会保存它的滚动位置。 使用 FragmentTransaction 处理 Fragment 时会出现此问题。
为了保存 CollpasingToolbar 的滚动状态,将此行添加到片段onViewCreated()方法中。
ViewCompat.requestApplyInsets(mCoordinatorLayout); // pass the coordinatorlayout id.
如果有人可以解释问题背后的确切原因,请自由发表评论。
您可以通过简单地添加 id attr 来保留滚动位置:
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:id="@+id/coordinator_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
@Ansh 的回答没有解决我的类似情况,但只是将 ID 设置为CoordinatorLayout
并且AppBarLayout
做到了! 这样, CollapsingToolbarLayout
状态将被自动保存。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.