comparison android/DWindows.kt @ 2565:5463801a888f

Android: Attempt to rewrite container layout code in a more generic manner. Theoretically this should be able to display all columns with simpleMode set false. Still not seeing the icons though, so something is still wrong with the layout code.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Wed, 19 May 2021 09:41:17 +0000
parents 607acfe2c504
children 19d82c1f135f
comparison
equal deleted inserted replaced
2564:607acfe2c504 2565:5463801a888f
624 class DWContainerAdapter(c: Context) : BaseAdapter() 624 class DWContainerAdapter(c: Context) : BaseAdapter()
625 { 625 {
626 private var context = c 626 private var context = c
627 var model = DWContainerModel() 627 var model = DWContainerModel()
628 var selectedItem: Int = -1 628 var selectedItem: Int = -1
629 var simpleMode: Boolean = true
629 630
630 override fun getCount(): Int { 631 override fun getCount(): Int {
631 val count = model.numberOfRows() 632 val count = model.numberOfRows()
632 if(count > 0) { 633 if(count > 0) {
633 return count 634 return count
634 } 635 }
635 return 1 636 return 1
636 } 637 }
637 638
638 override fun getItem(position: Int): Any? { 639 override fun getItem(position: Int): Any? {
639 return model.getRowAndColumn(position, 1) 640 return model.getRowAndColumn(position, 0)
640 } 641 }
641 642
642 override fun getItemId(position: Int): Long { 643 override fun getItemId(position: Int): Long {
643 return position.toLong() 644 return position.toLong()
644 } 645 }
645 646
646 override fun getView(position: Int, view: View?, parent: ViewGroup): View { 647 override fun getView(position: Int, view: View?, parent: ViewGroup): View {
647 var rowView: ConstraintLayout? = view as ConstraintLayout? 648 var rowView: ConstraintLayout? = view as ConstraintLayout?
649 var displayColumns = model.numberOfColumns()
650
651 // In simple mode, limit the columns to 1 or 2
652 if(simpleMode == true) {
653 // If column 1 is bitmap and column 2 is text...
654 if(displayColumns > 1 && (model.getColumnType(0) and 1) != 0 &&
655 (model.getColumnType(1) and (1 shl 1)) != 0) {
656 displayColumns = 2
657 } else {
658 displayColumns = 1
659 }
660 }
648 661
649 // If the view passed in is null we need to create the layout 662 // If the view passed in is null we need to create the layout
650 if(rowView == null) { 663 if(rowView == null) {
651 rowView = ConstraintLayout(context) 664 rowView = ConstraintLayout(context)
652 val set = ConstraintSet() 665 val set = ConstraintSet()
653 // Every container at least has an icon and text 666 var lastID: Int = -1
654 val imageview = ImageView(context) 667
655 val textview = TextView(context) 668 // Initialize the constraint layout and set
656 669 rowView.id = View.generateViewId()
657 if (model.numberOfColumns() > 1 && model.numberOfRows() > position) {
658 val first = model.getRowAndColumn(position, 0)
659 val second = model.getRowAndColumn(position, 1)
660
661 if (first is Drawable) {
662 imageview.setImageDrawable(first)
663 }
664 if (second is String) {
665 textview.text = second
666 }
667 }
668
669 // Add the two main components to the layout
670 imageview.id = View.generateViewId()
671 textview.id = View.generateViewId()
672 rowView.addView(imageview)
673 rowView.addView(textview)
674 set.clone(rowView) 670 set.clone(rowView)
675 set.connect(imageview.id, ConstraintSet.LEFT, textview.id, ConstraintSet.RIGHT) 671
672 for(i in 0 until displayColumns) {
673 val content = model.getRowAndColumn(position, i)
674
675 // Image
676 if((model.getColumnType(i) and 1) != 0) {
677 val imageview = ImageView(context)
678 imageview.id = View.generateViewId()
679 imageview.layoutParams = ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.WRAP_CONTENT, ConstraintLayout.LayoutParams.WRAP_CONTENT)
680 if (content is Drawable) {
681 imageview.setImageDrawable(content)
682 }
683 rowView.addView(imageview)
684 // Add to the set...
685 if(lastID == -1) {
686 set.connect(imageview.id, ConstraintSet.TOP, rowView.id, ConstraintSet.BOTTOM)
687 } else {
688 set.connect(lastID, ConstraintSet.START, imageview.id, ConstraintSet.END)
689 }
690 lastID = imageview.id
691 } else {
692 // Everything else id displayed as text
693 val textview = TextView(context)
694 textview.id = View.generateViewId()
695 textview.layoutParams = ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.WRAP_CONTENT, ConstraintLayout.LayoutParams.WRAP_CONTENT)
696 if (content is String) {
697 textview.text = content
698 } else if(content is Int) {
699 textview.text = content.toString()
700 }
701 rowView.addView(textview)
702 // Add to the set...
703 if(lastID == -1) {
704 set.connect(textview.id, ConstraintSet.TOP, rowView.id, ConstraintSet.BOTTOM)
705 } else {
706 set.connect(lastID, ConstraintSet.START, textview.id, ConstraintSet.END)
707 }
708 lastID = textview.id
709 }
710 }
711
712 // Finally apply the layout
676 set.applyTo(rowView) 713 set.applyTo(rowView)
677 714
678 // TODO: Add code to optionally add other columns 715 // TODO: Add code to optionally add other columns
679 } else { 716 } else {
680 // Otherwise we just need to update the existing layout 717 // Otherwise we just need to update the existing layout
681 if (model.numberOfColumns() > 1 && model.numberOfRows() > position) { 718
682 val first = model.getRowAndColumn(position, 0) 719 for(i in 0 until displayColumns) {
683 val second = model.getRowAndColumn(position, 1) 720 val content = model.getRowAndColumn(position, i)
684 val imageview = rowView.getChildAt(0) 721
685 val textview = rowView.getChildAt(1) 722 // Image
686 723 if((model.getColumnType(i) and 1) != 0) {
687 if (first is Drawable && imageview is ImageView) { 724 val imageview = rowView.getChildAt(i)
688 imageview.setImageDrawable(first) 725
689 } 726 if (imageview is ImageView && content is Drawable) {
690 if (second is String && textview is TextView) { 727 imageview.setImageDrawable(content)
691 textview.text = second 728 }
729 } else {
730 // Text
731 val textview = rowView.getChildAt(i)
732
733 if (textview is TextView) {
734 if (content is String) {
735 textview.text = content
736 } else if (content is Int) {
737 textview.text = content.toString()
738 }
739 }
692 } 740 }
693 } 741 }
694 } 742 }
695 return rowView 743 return rowView
696 } 744 }