保存NestedScrollView的滚动状态

问题描述:

我的应用程序围绕一个HomeActivity进行,它包含底部的4个选项卡。这些标签中的每一个都是一个片段,所有这些标签都从一开始就被添加(不会被替换),并且在点击适当的标签时隐藏/显示它们。保存NestedScrollView的滚动状态

我的问题是,只要我改变标签,我的滚动状态就会丢失。显示该问题的每个片段使用android.support.v4.widget.NestedScrollView(参见下面的示例)。

注意:由于某些原因,使用RecyclerView或ListView的片段保持其滚动状态。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <include layout="@layout/include_appbar_title" /> 

    <android.support.v4.widget.NestedScrollView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <!-- Content --> 

    </android.support.v4.widget.NestedScrollView> 

</LinearLayout> 

我阅读了有关节能实例状态(this onethat one例如)几个职位,以及他们的解决方案要么不中我的情况下工作,或者是不实际的工具给我有4-12不同片段我需要修改才能使其工作。

有一个嵌套滚动视图保持滚动位置的片段变化的最佳方式是什么?

我在inthecheesefactory上找到的一个解决方案是,默认情况下,片段的状态已保存(从EditText中的输入到scroll位置),但只有在给xml元素一个ID时才会这样。

对我来说,只是增加一个ID我NestedScrollView解决了这一问题:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <include layout="@layout/include_appbar_title" /> 

    <android.support.v4.widget.NestedScrollView 
     android:id="@+id/NestedScrollView" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <!-- Content --> 

    </android.support.v4.widget.NestedScrollView> 

</LinearLayout> 
+0

你我晚上保存:) – ievgen