2016-09-15 25 views
3

デバイスを回転させたときに、RecyclerViewの現在のスクロール位置を保持する方法に関する多くの提案を読んだことがあります。私は今、com.android.support:recyclerview-v7:23.0.+を使用すると、が実際にsavedInstanceStateに保存していることがわかりました。それは復元されたようですが、復元されたのは「やや正しく」です。RecyclerViewのスクロール位置を保持する

私はこれを経験しています:ポートレートモードで私のリストの一番下にスクロールしてから、私はランドスケープに切り替えます。現在の位置が正しく復元されます(もちろん、一部の項目は表示されません。スクロールを続けることができます)。

さらにスクロールすることなく、私は肖像画に戻って回転し、位置は私が実際に持っていたと思われる位置の上にいくつかの項目です。 リストは最下段にスクロールされなくなりました

私の質問は、実際に正しいスクロール位置を保存して復元するために他に何かできますか?

これはRecyclerViewのレイアウトである。

<android.support.v4.widget.SwipeRefreshLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/swipeLayout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="..."> 

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/itemsView" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:scrollbars="vertical" /> 

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

これは単一アイテムの短縮レイアウトである:RecyclerViewをホスト

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:paddingTop="8dp" 
    android:paddingBottom="8dp" 
    android:paddingLeft="12dp" 
    android:paddingRight="12dp"> 

    <!-- This linear layout aligns type indicator to the left and text content to the right --> 
    <LinearLayout 
     android:orientation="horizontal" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     ... 

    </LinearLayout> 

</LinearLayout> 

断片は、以下のコードを有する。

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
{ 
    View v = inflater.inflate(R.layout.calendar_fragment, container, false); 
    ButterKnife.bind(this, v); 

    mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { 
     @Override 
     public void onRefresh() { 
      ... 
     } 
    }); 

    if (mLayoutManager == null) 
    { 
     mLayoutManager = new LinearLayoutManager(getActivity()); 
     mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL); 
    } 

    // Get cached information or access storage 
    if (savedInstanceState != null) 
    { 
     // Initialize the area and the adapter from the saved state 
     mArea = savedInstanceState.getParcelable("AREA"); 
     Entry[] entries = (Entry[])savedInstanceState.getParcelableArray("ITEMS"); 
     mDataAdapter = new MyAdapter(this, mArea, entries); 
    } 
    else 
    { 
     // Initialize the area and the adapter form scratch 
     mArea = getAreaByPreference(); 
     mDataAdapter = new MyAdapter(this, mArea); 
    } 

    // Initialize the RecyclerView 
    mItemsView.setHasFixedSize(true); 
    mItemsView.setAdapter(mDataAdapter); 
    mItemsView.setLayoutManager(mLayoutManager); 

    return v; 
} 
+0

位置を保存してから 'void scrollToPosition(int position)'を試しましたか? https://developer.android.com/reference/android/support/v7/widget/LinearLayoutManager.html#scrollToPosition(int) – Jonsmoke

+0

いいえ、私はまだそれを試していませんでした。私が見つけた他のソリューションでは、 'LinearLayoutManager'自らの状態を保存して元に戻す。 –

+0

あなたはこの回答を見たことがありますか? http://stackoverflow.com/a/29166336/3658819 – Jonsmoke

答えて

2

これは、現在のAPI 24と最新のサポートライブラリには、これ以上のコーディングを行わずにデフォルトで必要に応じて動作します。

関連する問題