changeset 2667:2ae70678c845

Android: Use a RecyclerView method to change the ViewPager2 page since the ViewPager2 method is not functioning. Also create a private function to switch windows. Next step is to implement functionality to switch between windows.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Fri, 15 Oct 2021 23:22:23 +0000
parents 4f2d433747e2
children 917f2d1f9cae
files android/DWindows.kt
diffstat 1 files changed, 34 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/android/DWindows.kt	Wed Oct 13 17:54:09 2021 +0000
+++ b/android/DWindows.kt	Fri Oct 15 23:22:23 2021 +0000
@@ -91,6 +91,7 @@
     val viewList = mutableListOf<LinearLayout>()
     val pageList = mutableListOf<Long>()
     var currentPageID = 0L
+    var recyclerView: RecyclerView? = null
 
     override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) =
             DWEventViewHolder(viewList.get(viewType))
@@ -99,6 +100,9 @@
     override fun getItemViewType(position: Int): Int {
         return position
     }
+    override fun onAttachedToRecyclerView(rv: RecyclerView) {
+        recyclerView = rv
+    }
     override fun onBindViewHolder(holder: DWEventViewHolder, position: Int) {
         holder.setIsRecyclable(false)
     }
@@ -1390,29 +1394,48 @@
         return text
     }
 
+    private fun windowSwitchWindow(index: Int)
+    {
+        val adapter: DWTabViewPagerAdapter = windowLayout!!.adapter as DWTabViewPagerAdapter
+
+        if (index != -1) {
+            val window = adapter.viewList[index] as View
+
+            // Only allow a window to become active if it is shown
+            if(window.visibility == View.VISIBLE) {
+                // Update the action bar title
+                this.title = windowTitles[index]
+                // Switch the visible view in the pager
+                if(adapter.recyclerView != null) {
+                    adapter.recyclerView!!.scrollToPosition(index)
+                }
+                // This is how I prefered to do it, but it doesn't work...
+                // So using he RecyclerView.scrollToPosition() instead
+                //windowLayout!!.setCurrentItem(index, true)
+
+                // If the new view has a default item, focus it
+                if(windowDefault[index] != null) {
+                    windowDefault[index]?.requestFocus()
+                }
+                // Invalidate the menu, so it gets updated for the new window
+                invalidateOptionsMenu()
+            }
+        }
+    }
+
     fun windowHideShow(window: View, state: Int)
     {
         if(windowLayout != null) {
             waitOnUiThread {
                 val adapter: DWTabViewPagerAdapter = windowLayout!!.adapter as DWTabViewPagerAdapter
                 val index = adapter.viewList.indexOf(window)
-                var defaultItem: View? = null
 
                 if(state == 0) {
                     window.visibility = View.GONE
                 } else {
                     window.visibility = View.VISIBLE
                 }
-                if (index != -1) {
-                    defaultItem = windowDefault[index]
-                    if(state != 0) {
-                        windowLayout!!.setCurrentItem(index, true)
-                    }
-                    invalidateOptionsMenu()
-                }
-                if(state != 0 && defaultItem != null) {
-                    defaultItem.requestFocus()
-                }
+                windowSwitchWindow(index)
             }
         }
     }