2017-06-07 53 views
0

我的應用程序圍繞一個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不同片段我需要修改才能使其工作。

有一個嵌套滾動視圖保持滾動位置的片段變化的最佳方式是什麼?

回答

1

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