comparison android/DWindows.kt @ 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
comparison
equal deleted inserted replaced
2682:856d3c8b559f 2683:e7885fd45f7b
1631 scrollBox!!.addView(box) 1631 scrollBox!!.addView(box)
1632 } 1632 }
1633 return scrollBox 1633 return scrollBox
1634 } 1634 }
1635 1635
1636 // Update the layoutParams of a box after a change
1637 private fun boxUpdate(box: LinearLayout)
1638 {
1639 val parent = box.parent
1640
1641 if(parent is LinearLayout) {
1642 val params = box.layoutParams as LinearLayout.LayoutParams
1643
1644 if(parent.orientation == LinearLayout.VERTICAL) {
1645 if(params.height == 0) {
1646 box.measure(0, 0)
1647 val calch = box.measuredHeight
1648
1649 if(calch > 0) {
1650 params.weight = calch.toFloat()
1651 }
1652 }
1653 } else {
1654 if(params.width == 0) {
1655 box.measure(0, 0)
1656 val calcw = box.measuredWidth
1657
1658 if(calcw > 0) {
1659 params.weight = calcw.toFloat()
1660 }
1661 }
1662 }
1663 }
1664 }
1665
1636 fun boxPack( 1666 fun boxPack(
1637 boxview: View, 1667 boxview: View,
1638 packitem: View?, 1668 packitem: View?,
1639 index: Int, 1669 index: Int,
1640 width: Int, 1670 width: Int,
1670 if (box != null) { 1700 if (box != null) {
1671 var weight = 1F 1701 var weight = 1F
1672 1702
1673 // If it is a box, match parent based on direction 1703 // If it is a box, match parent based on direction
1674 if ((item is LinearLayout) || (item is ScrollView)) { 1704 if ((item is LinearLayout) || (item is ScrollView)) {
1705 item.measure(0, 0)
1675 if (box.orientation == LinearLayout.VERTICAL) { 1706 if (box.orientation == LinearLayout.VERTICAL) {
1676 if (hsize != 0) { 1707 if (hsize != 0) {
1677 w = LinearLayout.LayoutParams.MATCH_PARENT 1708 w = LinearLayout.LayoutParams.MATCH_PARENT
1678 } 1709 }
1710 if (vsize != 0) {
1711 val calch = item.measuredHeight
1712
1713 if(calch > 0) {
1714 weight = calch.toFloat()
1715 } else {
1716 weight = 1F
1717 }
1718 h = 0
1719 }
1679 } else { 1720 } else {
1680 if (vsize != 0) { 1721 if (vsize != 0) {
1681 h = LinearLayout.LayoutParams.MATCH_PARENT 1722 h = LinearLayout.LayoutParams.MATCH_PARENT
1723 }
1724 if (hsize != 0) {
1725 val calcw = item.measuredWidth
1726
1727 if(calcw > 0) {
1728 weight = calcw.toFloat()
1729 } else {
1730 weight = 1F
1731 }
1732 w = 0
1682 } 1733 }
1683 } 1734 }
1684 // If it isn't a box... set or calculate the size as needed 1735 // If it isn't a box... set or calculate the size as needed
1685 } else { 1736 } else {
1686 if(width != -1 || height != -1) { 1737 if(width != -1 || height != -1) {
1754 if (pad > 0) { 1805 if (pad > 0) {
1755 params.setMargins(pad, pad, pad, pad) 1806 params.setMargins(pad, pad, pad, pad)
1756 } 1807 }
1757 item.layoutParams = params 1808 item.layoutParams = params
1758 box.addView(item, index) 1809 box.addView(item, index)
1810 boxUpdate(box)
1759 } 1811 }
1760 } 1812 }
1761 } 1813 }
1762 1814
1763 fun boxUnpack(item: View) { 1815 fun boxUnpack(item: View) {
1764 waitOnUiThread { 1816 waitOnUiThread {
1765 val box: LinearLayout = item.parent as LinearLayout 1817 val box: LinearLayout = item.parent as LinearLayout
1766 box.removeView(item) 1818 box.removeView(item)
1819 boxUpdate(box)
1767 } 1820 }
1768 } 1821 }
1769 1822
1770 fun boxUnpackAtIndex(box: LinearLayout, index: Int): View? { 1823 fun boxUnpackAtIndex(box: LinearLayout, index: Int): View? {
1771 var item: View? = null 1824 var item: View? = null
1772 1825
1773 waitOnUiThread { 1826 waitOnUiThread {
1774 item = box.getChildAt(index) 1827 item = box.getChildAt(index)
1775
1776 box.removeView(item) 1828 box.removeView(item)
1829 boxUpdate(box)
1777 } 1830 }
1778 return item 1831 return item
1779 } 1832 }
1780 1833
1781 fun buttonNew(text: String, cid: Int): Button? { 1834 fun buttonNew(text: String, cid: Int): Button? {