我的应用程序使用单个活动体系结构,该体系结构在活动布局中具有NestedScrollView
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/root_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.android.navigationadvancedsample.MainActivity">
<androidx.core.widget.NestedScrollView
android:id="@+id/app_scroll_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:fillViewport="true">
<FrameLayout
android:id="@+id/nav_host_container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</androidx.core.widget.NestedScrollView>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_nav"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:menu="@menu/bottom_nav"/>
</LinearLayout>我的问题是,当我滚动一个片段,然后导航到另一个片段,然后返回时,滚动位置会被重置。我已经看到了另一个答案,它指出向布局添加it应该可以解决这个问题,但对我来说并非如此。此外,有趣的是,滚动位置可以很好地保存配置更改。
我用的是NavigationComponents,这有关系吗?Here's a sample project再现了这个问题(基于谷歌的NavigationAdvancedSample)
发布于 2019-07-18 20:43:06
NestedScrollView已经在activity_main布局中包装了导航容器。因此,它是保存滚动状态的活动。有3个片段。主页片段具有固定高度,排行榜和注册表片段正在滚动。当您在排行板或注册表中滚动并切换到另一个时,滚动状态不会改变(因为两者都可以滚动到大致相同的高度),但如果您切换到主页片段滚动状态,则会重置,因为它具有固定的高度(屏幕的大小)。在我研究它时,用ScrollView替换NestedScrollView并没有改变任何东西。我认为正确的设计是用NestedScrollView包装每个片段,并为每个片段设置和获取滚动状态。导航组件不会将片段添加到活动状态,而是替换它。因此,片断之后通过在它们之间切换来重新创建。所以你可以看到滚动状态正在被重置。您可以通过在第一个分片的onCreateView中放置一些日志来自己检查,可以看到日志出现了两次。
https://stackoverflow.com/questions/57093519
复制相似问题