[Android] Make the chapter list scroll to the current chapter

Nicolas Pomepuy git at videolan.org
Fri Sep 2 06:47:18 UTC 2022


vlc-android | branch: master | Nicolas Pomepuy <nicolas at videolabs.io> | Thu Sep  1 09:36:46 2022 +0200| [93aba96d393a89f535ee16c120c086bd2f4296c9] | committer: Nicolas Pomepuy

Make the chapter list scroll to the current chapter

Fixes #2654

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

 .../vlc-android/res/layout/chapter_list_item.xml     | 20 +++++++++-----------
 .../vlc-android/res/layout/dialog_select_chapter.xml | 15 ++++++++-------
 .../videolan/vlc/gui/dialogs/SelectChapterDialog.kt  | 19 +++++++++++++++----
 3 files changed, 32 insertions(+), 22 deletions(-)

diff --git a/application/vlc-android/res/layout/chapter_list_item.xml b/application/vlc-android/res/layout/chapter_list_item.xml
index 25341b0a2..dbdb1688c 100644
--- a/application/vlc-android/res/layout/chapter_list_item.xml
+++ b/application/vlc-android/res/layout/chapter_list_item.xml
@@ -20,36 +20,34 @@
 
     <androidx.constraintlayout.widget.ConstraintLayout
             android:layout_width="match_parent"
-            android:layout_height="wrap_content"
+            android:layout_height="48dp"
             android:background="@drawable/background_chapter_item"
             android:focusable="true"
             android:onClick="@{holder::onClick}"
-            android:selected="@{selected}"
-            android:orientation="vertical">
+            android:orientation="vertical"
+            android:selected="@{selected}">
 
         <TextView
                 android:id="@+id/chapterTitle"
                 android:layout_width="0dp"
                 android:layout_height="wrap_content"
-                android:layout_marginStart="8dp"
-                android:layout_marginTop="8dp"
-                android:layout_marginEnd="8dp"
-                android:layout_marginBottom="8dp"
+                android:layout_marginStart="16dp"
+                android:layout_marginEnd="16dp"
+                android:maxLines="1"
+                android:ellipsize="end"
                 android:text="@{chapter.name}"
-                app:selected="@{selected}"
                 app:layout_constraintBottom_toBottomOf="parent"
                 app:layout_constraintEnd_toStartOf="@+id/chapter_time"
                 app:layout_constraintStart_toStartOf="parent"
-                app:layout_constraintTop_toTopOf="parent" />
+                app:layout_constraintTop_toTopOf="parent"
+                app:selected="@{selected}" />
 
         <TextView
                 android:id="@+id/chapter_time"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:layout_alignParentRight="true"
-                android:layout_marginTop="8dp"
                 android:layout_marginEnd="8dp"
-                android:layout_marginBottom="8dp"
                 android:text="@{chapter.time}"
                 app:layout_constraintBottom_toBottomOf="parent"
                 app:layout_constraintEnd_toEndOf="parent"
diff --git a/application/vlc-android/res/layout/dialog_select_chapter.xml b/application/vlc-android/res/layout/dialog_select_chapter.xml
index cfef19b13..a9f1ecc63 100644
--- a/application/vlc-android/res/layout/dialog_select_chapter.xml
+++ b/application/vlc-android/res/layout/dialog_select_chapter.xml
@@ -1,14 +1,14 @@
 <?xml version="1.0" encoding="utf-8"?>
 <androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
         xmlns:app="http://schemas.android.com/apk/res-auto"
-        xmlns:tools="http://schemas.android.com/tools"
+        android:id="@+id/chapter_nested_scroll"
         android:layout_width="match_parent"
         android:layout_height="match_parent">
 
-    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+            xmlns:app="http://schemas.android.com/apk/res-auto"
             android:layout_width="match_parent"
-            android:layout_height="wrap_content"
-            android:gravity="center_horizontal"
+            android:layout_height="match_parent"
             android:orientation="vertical">
 
         <TextView
@@ -25,8 +25,9 @@
 
         <androidx.recyclerview.widget.RecyclerView
                 android:id="@+id/chapter_list"
-                android:layout_width="0dp"
-                android:layout_height="wrap_content"
+                android:layout_width="match_parent"
+                android:layout_height="0dp"
+                android:layout_weight="1"
                 android:layout_gravity="center_horizontal"
                 android:layout_marginTop="8dp"
                 android:layout_marginBottom="8dp"
@@ -38,5 +39,5 @@
                 app:layout_constraintStart_toStartOf="parent"
                 app:layout_constraintTop_toBottomOf="@+id/textView6" />
 
-    </androidx.constraintlayout.widget.ConstraintLayout>
+    </LinearLayout>
 </androidx.core.widget.NestedScrollView>
\ No newline at end of file
diff --git a/application/vlc-android/src/org/videolan/vlc/gui/dialogs/SelectChapterDialog.kt b/application/vlc-android/src/org/videolan/vlc/gui/dialogs/SelectChapterDialog.kt
index 8e0272b3e..7ed7636c4 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/dialogs/SelectChapterDialog.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/dialogs/SelectChapterDialog.kt
@@ -25,6 +25,8 @@ import android.os.Bundle
 import android.view.LayoutInflater
 import android.view.View
 import android.view.ViewGroup
+import androidx.core.view.ViewCompat
+import androidx.core.widget.NestedScrollView
 import androidx.databinding.DataBindingUtil
 import androidx.lifecycle.lifecycleScope
 import androidx.recyclerview.widget.LinearLayoutManager
@@ -32,12 +34,12 @@ import androidx.recyclerview.widget.RecyclerView
 import com.google.android.material.bottomsheet.BottomSheetBehavior.STATE_EXPANDED
 import kotlinx.coroutines.flow.onEach
 import org.videolan.medialibrary.Tools
+import org.videolan.tools.dp
 import org.videolan.vlc.PlaybackService
 import org.videolan.vlc.R
 import org.videolan.vlc.databinding.ChapterListItemBinding
 import org.videolan.vlc.util.TextUtils
 import org.videolan.vlc.util.launchWhenStarted
-import java.util.*
 
 class SelectChapterDialog : VLCBottomSheetDialogFragment(), IOnChapterSelectedListener {
 
@@ -49,6 +51,7 @@ class SelectChapterDialog : VLCBottomSheetDialogFragment(), IOnChapterSelectedLi
     }
 
     private lateinit var chapterList: RecyclerView
+    private lateinit var nestedScrollView: NestedScrollView
 
     private var service: PlaybackService? = null
 
@@ -58,6 +61,7 @@ class SelectChapterDialog : VLCBottomSheetDialogFragment(), IOnChapterSelectedLi
                               savedInstanceState: Bundle?): View? {
         val view = inflater.inflate(R.layout.dialog_select_chapter, container)
         chapterList = view.findViewById(R.id.chapter_list)
+        nestedScrollView = view.findViewById(R.id.chapter_nested_scroll)
 
         return view
     }
@@ -82,10 +86,17 @@ class SelectChapterDialog : VLCBottomSheetDialogFragment(), IOnChapterSelectedLi
 
         val adapter = ChapterAdapter(chapterData, service?.chapterIdx, this)
 
-        chapterList.layoutManager = LinearLayoutManager(requireActivity())
+        chapterList.layoutManager = object : LinearLayoutManager(activity, VERTICAL, false) {
+            override fun onLayoutCompleted(state: RecyclerView.State?) {
+                super.onLayoutCompleted(state)
+                service?.chapterIdx?.let { index ->
+                    //we cannot scroll the recyclerview as its height is wrap_content. We scroll the nestedScrollView instead
+                    nestedScrollView.smoothScrollTo(0, 48.dp * index)
+                }
+            }
+        }
+        ViewCompat.setNestedScrollingEnabled(chapterList, false)
         chapterList.adapter = adapter
-
-
     }
 
     override fun onChapterSelected(position: Int) {



More information about the Android mailing list