简体   繁体   English

Android scrollview 在片段事务后保持滚动 position

[英]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.

相关问题 android片段未显示交易后的先前位置 - android fragment not showing the previous position after transaction 保持ScrollView滚动位置以获取动态内容 - Keep ScrollView scroll position for dynamic content 回到上一个片段(如 ListView)后,我可以保持 ScrollView 的位置吗? - Could I keep position of ScrollView after back to prev Fragment like ListView? 在加载了LiveDate对象的片段中旋转屏幕后,如何在ScrollView中保持滚动位置 - How to keep scrolling position in ScrollView after rotating the screen in fragment with LiveDate object loaded Android 在滚动视图中滚动到顶部下一个替换片段 - Android scroll to top next replacing fragment in scrollview 交易后,Android Fragment不会膨胀 - Android Fragment not inflating after transaction 替换交易后如何保持片段状态? - How to keep fragment state after replace transaction? Android listview在scrollview中,listview在scrollview滚动到特定位置时滚动 - Android listview in scrollview, listview scroll when scrollview scrolled specific position 片段弹出堆栈后如何保持网格视图滚动位置? - How to keep grid view scroll position after fragment pop back stack? ScrollView不会在片段中滚动 - ScrollView does not scroll in fragment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM