【问题标题】:Android scrollview keep scroll position after fragment transaction片段事务后Android滚动视图保持滚动位置
【发布时间】:2021-01-05 04:03:30
【问题描述】:

我想保持片段的滚动视图滚动位置。这个片段 (HomeFragment.kt) 是带有底部导航 (DashboardFragment.kt) 的片段中的几个片段之一。

这是HomeFragment.kt 的加载方式(这是DashboardFragment.kt 的sn-p)

    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()
    }

这是滚动视图布局(这是HomeFragment.kt 布局的sn-p):

    <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>

如您所见,我已经有了滚动视图的 id。但是每次这个片段从另一个片段加载或导航回来(从另一个片段到DashboardFragment.kt)(我正在使用导航组件)时,它都会从顶部开始。

有没有办法在片段事务之后保持滚动视图?如果有任何我遗漏的细节,请告诉我。

【问题讨论】:

  • 由于您没有将事务添加到后台堆栈,我认为这就是它不会保存状态的原因。这是不将其添加到后台堆栈的要求吗?
  • @ADM 那么...我应该使用什么来代替 replace()
  • 我的意思是使用addTobackStack 不改变replace()
  • 如果您使用导航组件,则根本不应该使用beginTransaction()。它是哪一个?导航组件与否?
  • 您找出问题所在了吗? @dimashermanto

标签: android xml kotlin


【解决方案1】:

要对您的后台堆栈进行更多控制,您可以使用自定义堆栈并检查堆栈中只有一个片段实例。 同样对于您的问题,您可以隐藏堆栈顶部的其他片段:

   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();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-09-24
    • 2018-04-08
    • 1970-01-01
    • 1970-01-01
    • 2011-03-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多