[Android] Fix audio lists position restoration

Nicolas Pomepuy git at videolan.org
Mon Sep 16 09:14:36 CEST 2019


vlc-android | branch: master | Nicolas Pomepuy <nicolas.pomepuy at gmail.com> | Mon Sep 16 08:58:27 2019 +0200| [fd22be91c582e8b980dc8555534c144be2657b48] | committer: Nicolas Pomepuy

Fix audio lists position restoration

Fixes #1027

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

 .../src/org/videolan/vlc/gui/audio/AudioBrowserFragment.kt   | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserFragment.kt b/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserFragment.kt
index 9419290b2..1ed73127d 100644
--- a/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserFragment.kt
+++ b/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserFragment.kt
@@ -25,6 +25,7 @@ import android.content.SharedPreferences
 import android.content.res.Configuration
 import android.os.Bundle
 import android.os.Message
+import android.util.SparseArray
 import android.view.*
 import android.widget.Button
 import android.widget.TextView
@@ -71,6 +72,7 @@ class AudioBrowserFragment : BaseAudioBrowser<AudioBrowserViewModel>(), SwipeRef
     private lateinit var fastScroller: FastScroller
     override val hasTabs = true
     private var spacing = 0
+    private var restorePositions: SparseArray<Int> = SparseArray()
 
     /**
      * Handle changes on the list
@@ -133,13 +135,15 @@ class AudioBrowserFragment : BaseAudioBrowser<AudioBrowserViewModel>(), SwipeRef
             viewPager.adapter = audioPagerAdapter
             val tabPosition = settings.getInt(KEY_AUDIO_CURRENT_TAB, 0)
             currentTab = tabPosition
-            val positions = savedInstanceState?.getIntegerArrayList(KEY_LISTS_POSITIONS)
+            val position = savedInstanceState?.getIntegerArrayList(KEY_LISTS_POSITIONS)
+            position?.withIndex()?.forEach {
+                restorePositions.put(it.index, it.value)
+            }
             for (i in 0 until MODE_TOTAL) {
                 setupLayoutManager(i)
                 (lists[i].layoutManager as LinearLayoutManager).recycleChildrenOnDetach = true
                 val list = lists[i]
                 list.adapter = adapters[i]
-                if (positions != null) list.scrollToPosition(positions[i])
                 list.addOnScrollListener(scrollListener)
                 if (viewModel.providersInCard[i]) {
                     val lp = list.layoutParams
@@ -216,6 +220,10 @@ class AudioBrowserFragment : BaseAudioBrowser<AudioBrowserViewModel>(), SwipeRef
             provider.pagedList.observe(this, Observer { items ->
                 @Suppress("UNCHECKED_CAST")
                 if (items != null) adapters[index].submitList(items as PagedList<MediaLibraryItem>?)
+                restorePositions.get(index)?.let {
+                    lists[index].scrollToPosition(it)
+                    restorePositions.delete(index)
+                }
             })
             provider.loading.observe(this, Observer { loading ->
                 if (loading == null || currentTab != index) return at Observer



More information about the Android mailing list