[Android] Fix TV browser position restoration when navigating

Nicolas Pomepuy git at videolan.org
Tue Apr 19 09:00:43 UTC 2022


vlc-android | branch: master | Nicolas Pomepuy <nicolas at videolabs.io> | Tue Apr 19 10:11:45 2022 +0200| [cd091fa440a67ea423d7400c0d09fd82f6489f6d] | committer: Duncan McNamara

Fix TV browser position restoration when navigating

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

 .../videolan/television/ui/FileTvItemAdapter.kt    |  4 ++++
 .../videolan/television/ui/MediaTvItemAdapter.kt   |  5 ++++
 .../television/ui/browser/BaseBrowserTvFragment.kt | 28 ++++++++--------------
 3 files changed, 19 insertions(+), 18 deletions(-)

diff --git a/application/television/src/main/java/org/videolan/television/ui/FileTvItemAdapter.kt b/application/television/src/main/java/org/videolan/television/ui/FileTvItemAdapter.kt
index a1581dac6..0e06d5cad 100644
--- a/application/television/src/main/java/org/videolan/television/ui/FileTvItemAdapter.kt
+++ b/application/television/src/main/java/org/videolan/television/ui/FileTvItemAdapter.kt
@@ -126,6 +126,8 @@ class FileTvItemAdapter(private val eventsHandler: IEventsHandler<MediaLibraryIt
 
         override fun getItem(layoutPosition: Int) = this at FileTvItemAdapter.getItem(layoutPosition)
 
+        override fun getView() = binding.container
+
         init {
             binding.holder = this
             binding.isPresent = true
@@ -213,6 +215,8 @@ class FileTvItemAdapter(private val eventsHandler: IEventsHandler<MediaLibraryIt
 
         override fun getItem(layoutPosition: Int) = this at FileTvItemAdapter.getItem(layoutPosition)
 
+        override fun getView() = binding.container
+
         init {
             binding.holder = this
             binding.isPresent = true
diff --git a/application/television/src/main/java/org/videolan/television/ui/MediaTvItemAdapter.kt b/application/television/src/main/java/org/videolan/television/ui/MediaTvItemAdapter.kt
index 4fd67a7ab..23d836fe5 100644
--- a/application/television/src/main/java/org/videolan/television/ui/MediaTvItemAdapter.kt
+++ b/application/television/src/main/java/org/videolan/television/ui/MediaTvItemAdapter.kt
@@ -164,6 +164,8 @@ class MediaTvItemAdapter(type: Int, private val eventsHandler: IEventsHandler<Me
 
         abstract fun setCoverlay(selected: Boolean)
 
+        abstract fun getView(): View
+
         fun isPresent() = (getItem(layoutPosition) as? MediaWrapper)?.isPresent ?: true
         fun isNetwork() = !(getItem(layoutPosition) as? MediaWrapper)?.uri?.scheme.isSchemeFile()
         fun isSD() = (getItem(layoutPosition) as? MediaWrapper)?.uri?.isSD() == true
@@ -177,6 +179,7 @@ class MediaTvItemAdapter(type: Int, private val eventsHandler: IEventsHandler<Me
     ) : AbstractMediaItemViewHolder<MediaBrowserTvItemBinding>(binding)
     {
         override fun getItem(layoutPosition: Int) =  this at MediaTvItemAdapter.getItem(layoutPosition)
+        override fun getView() = binding.container
 
         init {
             binding.holder = this
@@ -260,6 +263,8 @@ class MediaTvItemAdapter(type: Int, private val eventsHandler: IEventsHandler<Me
     ) : AbstractMediaItemViewHolder<MediaBrowserTvItemListBinding>(binding) {
         override fun getItem(layoutPosition: Int) = this at MediaTvItemAdapter.getItem(layoutPosition)
 
+        override fun getView() = binding.container
+
         init {
             binding.holder = this
             if (defaultCover != null) binding.cover = defaultCover
diff --git a/application/television/src/main/java/org/videolan/television/ui/browser/BaseBrowserTvFragment.kt b/application/television/src/main/java/org/videolan/television/ui/browser/BaseBrowserTvFragment.kt
index 8d80b2342..9b92d74e2 100644
--- a/application/television/src/main/java/org/videolan/television/ui/browser/BaseBrowserTvFragment.kt
+++ b/application/television/src/main/java/org/videolan/television/ui/browser/BaseBrowserTvFragment.kt
@@ -36,10 +36,7 @@ import android.view.MenuItem
 import android.view.View
 import android.view.ViewGroup
 import android.widget.PopupMenu
-import androidx.annotation.MainThread
 import androidx.constraintlayout.widget.ConstraintLayout
-import androidx.constraintlayout.widget.ConstraintSet
-import androidx.databinding.BindingAdapter
 import androidx.fragment.app.Fragment
 import androidx.leanback.app.BackgroundManager
 import androidx.lifecycle.lifecycleScope
@@ -67,7 +64,6 @@ import org.videolan.vlc.viewmodels.SortableModel
 import org.videolan.vlc.viewmodels.browser.TYPE_FILE
 import org.videolan.vlc.viewmodels.browser.TYPE_NETWORK
 import org.videolan.vlc.viewmodels.tv.TvBrowserModel
-import java.util.*
 
 private const val TAG = "MediaBrowserTvFragment"
 
@@ -193,13 +189,11 @@ abstract class BaseBrowserTvFragment<T> : Fragment(), BrowserFragmentInterface,
                 override fun requestChildRectangleOnScreen(parent: RecyclerView, child: View, rect: Rect, immediate: Boolean, focusedChildVisible: Boolean) = false
                 override fun onLayoutChildren(recycler: RecyclerView.Recycler?, state: RecyclerView.State?) {
                     super.onLayoutChildren(recycler, state)
-                    if (previouslySelectedItem != -1) {
-                        for (i in 0 until childCount) {
-                            if (i == previouslySelectedItem) {
-                                getChildAt(i)?.requestFocus()
-                                scrollToPosition(getPosition(getChildAt(i)!!))
-                                previouslySelectedItem = -1
-                            }
+                    if (previouslySelectedItem != -1 && binding.list.adapter?.itemCount ?: 0 > previouslySelectedItem) {
+                        scrollToPosition(previouslySelectedItem)
+                        binding.list.findViewHolderForLayoutPosition(previouslySelectedItem)?.let { holder ->
+                            (holder as MediaTvItemAdapter.AbstractMediaItemViewHolder<*>).getView().requestFocus()
+                            previouslySelectedItem = -1
                         }
                     }
                 }
@@ -227,13 +221,11 @@ abstract class BaseBrowserTvFragment<T> : Fragment(), BrowserFragmentInterface,
             gridLayoutManager = object : LinearLayoutManager(requireActivity()) {
                 override fun onLayoutChildren(recycler: RecyclerView.Recycler?, state: RecyclerView.State?) {
                     super.onLayoutChildren(recycler, state)
-                    if (previouslySelectedItem != -1) {
-                        for (i in 0 until childCount) {
-                            if (i == previouslySelectedItem) {
-                                getChildAt(i)?.requestFocus()
-                                scrollToPosition(getPosition(getChildAt(i)!!))
-                                previouslySelectedItem = -1
-                            }
+                    if (previouslySelectedItem != -1 && binding.list.adapter?.itemCount ?: 0 > previouslySelectedItem) {
+                        scrollToPosition(previouslySelectedItem)
+                        binding.list.findViewHolderForLayoutPosition(previouslySelectedItem)?.let { holder ->
+                            (holder as MediaTvItemAdapter.AbstractMediaItemViewHolder<*>).getView().requestFocus()
+                            previouslySelectedItem = -1
                         }
                     }
                 }



More information about the Android mailing list