changeset 2683:e7885fd45f7b

Android: Apply weights to boxes as well as items. We have to update the weights whenever we add or remove something from a box that has already been packed, so adding a private boxUpdate method for doing that. I tried to clean up the existing code somewhat in the process but it caused other layout problems... so I went bare bones with the changes here and it seems to work. Will revisit the streamlining later.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Mon, 25 Oct 2021 22:34:54 +0000
parents 856d3c8b559f
children a26e5a2d94be
files android/DWindows.kt
diffstat 1 files changed, 54 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/android/DWindows.kt	Mon Oct 25 17:07:01 2021 +0000
+++ b/android/DWindows.kt	Mon Oct 25 22:34:54 2021 +0000
@@ -1633,6 +1633,36 @@
         return scrollBox
     }
 
+    // Update the layoutParams of a box after a change
+    private fun boxUpdate(box: LinearLayout)
+    {
+        val parent = box.parent
+
+        if(parent is LinearLayout) {
+            val params = box.layoutParams as LinearLayout.LayoutParams
+
+            if(parent.orientation == LinearLayout.VERTICAL) {
+                if(params.height == 0) {
+                    box.measure(0, 0)
+                    val calch = box.measuredHeight
+
+                    if(calch > 0) {
+                        params.weight = calch.toFloat()
+                    }
+                }
+            } else {
+                if(params.width == 0) {
+                    box.measure(0, 0)
+                    val calcw = box.measuredWidth
+
+                    if(calcw > 0) {
+                        params.weight = calcw.toFloat()
+                    }
+                }
+            }
+        }
+    }
+
     fun boxPack(
         boxview: View,
         packitem: View?,
@@ -1672,14 +1702,35 @@
 
                 // If it is a box, match parent based on direction
                 if ((item is LinearLayout) || (item is ScrollView)) {
+                    item.measure(0, 0)
                     if (box.orientation == LinearLayout.VERTICAL) {
                         if (hsize != 0) {
                             w = LinearLayout.LayoutParams.MATCH_PARENT
                         }
+                        if (vsize != 0) {
+                            val calch = item.measuredHeight
+
+                            if(calch > 0) {
+                                weight = calch.toFloat()
+                            } else {
+                                weight = 1F
+                            }
+                            h = 0
+                        }
                     } else {
                         if (vsize != 0) {
                             h = LinearLayout.LayoutParams.MATCH_PARENT
                         }
+                        if (hsize != 0) {
+                            val calcw = item.measuredWidth
+
+                            if(calcw > 0) {
+                                weight = calcw.toFloat()
+                            } else {
+                                weight = 1F
+                            }
+                            w = 0
+                        }
                     }
                 // If it isn't a box... set or calculate the size as needed
                 } else {
@@ -1756,6 +1807,7 @@
                 }
                 item.layoutParams = params
                 box.addView(item, index)
+                boxUpdate(box)
             }
         }
     }
@@ -1764,6 +1816,7 @@
         waitOnUiThread {
             val box: LinearLayout = item.parent as LinearLayout
             box.removeView(item)
+            boxUpdate(box)
         }
     }
 
@@ -1772,8 +1825,8 @@
 
         waitOnUiThread {
             item = box.getChildAt(index)
-
             box.removeView(item)
+            boxUpdate(box)
         }
         return item
     }