[Android] Audio browser: save lists position on rotation

Geoffrey Métais git at videolan.org
Fri Oct 19 10:24:36 CEST 2018


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Fri Oct 19 10:23:27 2018 +0200| [9038981ce7da1d0727b1d497888f0e6664666a08] | committer: Geoffrey Métais

Audio browser: save lists position on rotation

> https://code.videolan.org/videolan/vlc-android/commit/9038981ce7da1d0727b1d497888f0e6664666a08
---

 .../vlc/gui/audio/AudioBrowserFragment.java        | 34 +++++++++++++++++-----
 1 file changed, 26 insertions(+), 8 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserFragment.java b/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserFragment.java
index cd07e70b2..8edee9b2e 100644
--- a/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserFragment.java
+++ b/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserFragment.java
@@ -20,20 +20,12 @@
 
 package org.videolan.vlc.gui.audio;
 
-import androidx.lifecycle.Observer;
-import androidx.lifecycle.ViewModelProviders;
-import androidx.paging.PagedList;
 import android.content.Context;
 import android.content.Intent;
 import android.content.SharedPreferences;
 import android.os.Bundle;
 import android.os.Handler;
 import android.os.Message;
-import androidx.annotation.Nullable;
-import com.google.android.material.tabs.TabLayout;
-import androidx.viewpager.widget.ViewPager;
-import androidx.recyclerview.widget.LinearLayoutManager;
-import androidx.recyclerview.widget.RecyclerView;
 import android.view.LayoutInflater;
 import android.view.Menu;
 import android.view.MotionEvent;
@@ -41,6 +33,8 @@ import android.view.View;
 import android.view.ViewGroup;
 import android.widget.TextView;
 
+import com.google.android.material.tabs.TabLayout;
+
 import org.videolan.medialibrary.media.MediaLibraryItem;
 import org.videolan.medialibrary.media.MediaWrapper;
 import org.videolan.vlc.MediaParsingServiceKt;
@@ -61,6 +55,17 @@ import org.videolan.vlc.viewmodels.paged.PagedGenresModel;
 import org.videolan.vlc.viewmodels.paged.PagedPlaylistsModel;
 import org.videolan.vlc.viewmodels.paged.PagedTracksModel;
 
+import java.util.ArrayList;
+
+import androidx.annotation.NonNull;
+import androidx.annotation.Nullable;
+import androidx.lifecycle.Observer;
+import androidx.lifecycle.ViewModelProviders;
+import androidx.paging.PagedList;
+import androidx.recyclerview.widget.LinearLayoutManager;
+import androidx.recyclerview.widget.RecyclerView;
+import androidx.viewpager.widget.ViewPager;
+
 public class AudioBrowserFragment extends BaseAudioBrowser implements SwipeRefreshLayout.OnRefreshListener, ViewPager.OnPageChangeListener, TabLayout.OnTabSelectedListener {
     public final static String TAG = "VLC/AudioBrowserFragment";
 
@@ -84,6 +89,7 @@ public class AudioBrowserFragment extends BaseAudioBrowser implements SwipeRefre
     private FastScroller mFastScroller;
     private SharedPreferences mSettings;
 
+    private static final String KEY_LISTS_POSITIONS = "key_lists_position";
     private static final int SET_REFRESHING = 103;
     private static final int UNSET_REFRESHING = 104;
     private static final int UPDATE_EMPTY_VIEW = 105;
@@ -133,18 +139,30 @@ public class AudioBrowserFragment extends BaseAudioBrowser implements SwipeRefre
         mViewPager.setAdapter(new AudioPagerAdapter(mLists, titles));
         mViewPager.setCurrentItem(mSettings.getInt(Constants.KEY_AUDIO_CURRENT_TAB, 0));
         final RecyclerView.RecycledViewPool rvp = new RecyclerView.RecycledViewPool();
+        final ArrayList<Integer> positions = savedInstanceState != null ? savedInstanceState.getIntegerArrayList(KEY_LISTS_POSITIONS) : null;
         for (int i = 0; i< MODE_TOTAL; ++i) {
             final LinearLayoutManager llm = new LinearLayoutManager(getActivity());
             llm.setRecycleChildrenOnDetach(true);
             mLists[i].setLayoutManager(llm);
             mLists[i].setRecycledViewPool(rvp);
             mLists[i].setAdapter(mAdapters[i]);
+            if (positions != null) mLists[i].scrollToPosition(positions.get(i));
         }
         mViewPager.setOnTouchListener(mSwipeFilter);
         setupTabLayout();
         mSwipeRefreshLayout.setOnRefreshListener(this);
     }
 
+    @Override
+    public void onSaveInstanceState(@NonNull Bundle outState) {
+        final ArrayList<Integer> positions = new ArrayList<>(MODE_TOTAL);
+        for (int i = 0; i< MODE_TOTAL; ++i) {
+            positions.add(((LinearLayoutManager)mLists[i].getLayoutManager()).findFirstCompletelyVisibleItemPosition());
+        }
+        outState.putIntegerArrayList(KEY_LISTS_POSITIONS, positions);
+        super.onSaveInstanceState(outState);
+    }
+
     private void setupModels() {
         final int current = mSettings.getInt(Constants.KEY_AUDIO_CURRENT_TAB, 0);
         for (int pass = 0 ; pass < 2; ++pass) {



More information about the Android mailing list