[英]Android scrollview keep scroll position after fragment transaction
I want to keep scrollview scroll position of a fragment.我想保持滚动视图滚动 position 的片段。 This fragment (
HomeFragment.kt
) is one of several fragments within a fragment with bottom navigation ( DashboardFragment.kt
).此片段 (
HomeFragment.kt
) 是具有底部导航 ( DashboardFragment.kt
) 的片段中的几个片段之一。
This is how HomeFragment.kt
loaded (This is a snippet of DashboardFragment.kt
)这就是
HomeFragment.kt
的加载方式(这是DashboardFragment.kt
的片段)
private fun loadHomeFragment() {
homeFragment = HomeFragment()
dashboardFragmentManager.beginTransaction()
.setCustomAnimations(
android.R.animator.fade_in,
android.R.animator.fade_out
)
.replace(R.id.content, homeFragment, "1")
.commit()
}
And this is the scrollview layout (This is a snippet of HomeFragment.kt
layout):这是滚动视图布局(这是
HomeFragment.kt
布局的片段):
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/swipeBaseLayout">
<ScrollView
android:id="@+id/baseLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="32dp">
<com.synnapps.carouselview.CarouselView
android:id="@+id/carouselView"
android:layout_width="match_parent"
android:layout_height="300dp"
app:fillColor="#FFFFFFFF"
app:pageColor="#00000000"
app:pageTransformInterval="800"
app:radius="3dp"
app:slideInterval="5000"
app:strokeColor="#FFFFFFFF"
app:strokeWidth="1px" />
<RelativeLayout>
.....
</RelativeLayout>
</RelativeLayout>
</ScrollView>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
As you can see, I already have an id for the scrollview.如您所见,我已经有了滚动视图的 id。 But every time this fragment loaded or navigated back from another fragment (From another fragment to DashboardFragment.kt ) (I'm using navigation component), it'll start from the top.
但是每次这个片段从另一个片段加载或导航回来(从另一个片段到DashboardFragment.kt )(我正在使用导航组件),它都会从顶部开始。
Is there any way to keep scrollview even after the fragment transaction?即使在片段事务之后,有没有办法保持滚动视图? If there's any detail I missed to point out, just let me know.
如果有任何我遗漏的细节要指出,请告诉我。
For more control on your backstack, you can have your custom stack and check there is only one instance of a fragment in the stack.为了更好地控制您的后台堆栈,您可以拥有自定义堆栈并检查堆栈中只有一个片段实例。 Also for your problem, you can hide other fragments that are on top of the stack:
同样对于您的问题,您可以隐藏堆栈顶部的其他片段:
showFragment(Fragment homeFragment) {
fragmentTransaction = fragmentManager.beginTransaction();
for (int i = 0; i < fragmentManager.getFragments().size(); i++) {
if (fragmentManager.getFragments().get(i) != homeFragment) {
fragmentTransaction.hide(fragmentManager.getFragments().get(i));
}
}
fragmentTransaction.show(homeFragment);
fragmentTransaction.commit();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.