comparison android/DWindows.kt @ 2494:b3e28eed0e50

Android: Fix the basics of notebook control... return actual page IDs. Also need to ensure the box packed in notebookPagePack is MATCH_PARENT. Also prevent multiple calls to dwindowsInit() from spawning multiple instances of dwmain().
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Sun, 02 May 2021 10:46:21 +0000
parents bca7e0ab0ccc
children 5664c91d03fb
comparison
equal deleted inserted replaced
2493:bca7e0ab0ccc 2494:b3e28eed0e50
30 import java.util.* 30 import java.util.*
31 31
32 32
33 class DWTabViewPagerAdapter : RecyclerView.Adapter<DWTabViewPagerAdapter.EventViewHolder>() { 33 class DWTabViewPagerAdapter : RecyclerView.Adapter<DWTabViewPagerAdapter.EventViewHolder>() {
34 public val viewList = mutableListOf<LinearLayout>() 34 public val viewList = mutableListOf<LinearLayout>()
35 public val pageList = mutableListOf<Long>()
36 public var currentPageID = 0L
35 37
36 override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = 38 override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) =
37 EventViewHolder(viewList.get(0)) 39 EventViewHolder(viewList.get(0))
38 40
39 override fun getItemCount() = viewList.count() 41 override fun getItemCount() = viewList.count()
360 var h: Int = RelativeLayout.LayoutParams.WRAP_CONTENT 362 var h: Int = RelativeLayout.LayoutParams.WRAP_CONTENT
361 var dataArrayMap = SimpleArrayMap<String, Long>() 363 var dataArrayMap = SimpleArrayMap<String, Long>()
362 364
363 notebook.tag = dataArrayMap 365 notebook.tag = dataArrayMap
364 notebook.id = cid 366 notebook.id = cid
367 tabs.id = View.generateViewId()
368 pager.id = View.generateViewId()
365 pager.adapter = DWTabViewPagerAdapter() 369 pager.adapter = DWTabViewPagerAdapter()
366 TabLayoutMediator(tabs, pager) { tab, position -> 370 TabLayoutMediator(tabs, pager) { tab, position ->
367 tab.text = "OBJECT ${(position + 1)}" 371 //tab.text = "OBJECT ${(position + 1)}"
368 }.attach() 372 }.attach()
369 373
370 var params: RelativeLayout.LayoutParams = RelativeLayout.LayoutParams(w, h) 374 var params: RelativeLayout.LayoutParams = RelativeLayout.LayoutParams(w, h)
371 tabs.layoutParams = params 375 if(top != 0) {
372 notebook.addView(tabs) 376 params.addRule(RelativeLayout.ABOVE, pager.id)
373 if (top != 0) {
374 notebook.addView(pager, 0)
375 } else { 377 } else {
376 notebook.addView(pager) 378 params.addRule(RelativeLayout.BELOW, pager.id)
377 } 379 }
380 tabs.tabGravity = TabLayout.GRAVITY_FILL
381 tabs.tabMode = TabLayout.MODE_FIXED
382 notebook.addView(tabs, params)
383 notebook.addView(pager, RelativeLayout.LayoutParams(w, w))
378 return notebook 384 return notebook
379 } 385 }
380 386
381 fun notebookPageNew(notebook: RelativeLayout, flags: Long, front: Int): Any? 387 fun notebookPageNew(notebook: RelativeLayout, flags: Long, front: Int): Long
382 { 388 {
383 var pager: ViewPager2? = null 389 var pager: ViewPager2? = null
384 var tabs: TabLayout? = null 390 var tabs: TabLayout? = null
385 var tab: Any? = null 391 var pageID = 0L
386 392
387 if(notebook.getChildAt(0) is ViewPager2 && notebook.getChildAt(1) is TabLayout) { 393 if(notebook.getChildAt(0) is ViewPager2 && notebook.getChildAt(1) is TabLayout) {
388 pager = notebook.getChildAt(0) as ViewPager2 394 pager = notebook.getChildAt(0) as ViewPager2
389 tabs = notebook.getChildAt(1) as TabLayout 395 tabs = notebook.getChildAt(1) as TabLayout
390 } else if(notebook.getChildAt(1) is ViewPager2 && notebook.getChildAt(0) is TabLayout) { 396 } else if(notebook.getChildAt(1) is ViewPager2 && notebook.getChildAt(0) is TabLayout) {
392 tabs = notebook.getChildAt(0) as TabLayout 398 tabs = notebook.getChildAt(0) as TabLayout
393 } 399 }
394 400
395 if(pager != null && tabs != null) { 401 if(pager != null && tabs != null) {
396 var adapter: DWTabViewPagerAdapter = pager.adapter as DWTabViewPagerAdapter 402 var adapter: DWTabViewPagerAdapter = pager.adapter as DWTabViewPagerAdapter
397 403 var tab = tabs.newTab()
398 tab = tabs.newTab() 404
405 // Increment our page ID... making sure no duplicates exist
406 do {
407 adapter.currentPageID += 1
408 }
409 while(adapter.currentPageID == 0L || adapter.pageList.contains(adapter.currentPageID))
410 pageID = adapter.currentPageID
399 // Temporarily add a black tab with an empty layout/box 411 // Temporarily add a black tab with an empty layout/box
400 if(front != 0) { 412 if(front != 0) {
401 adapter.viewList.add(0, LinearLayout(this)) 413 adapter.viewList.add(0, LinearLayout(this))
414 adapter.pageList.add(0, pageID)
402 tabs.addTab(tab, 0) 415 tabs.addTab(tab, 0)
403 } else { 416 } else {
404 adapter.viewList.add(LinearLayout(this)) 417 adapter.viewList.add(LinearLayout(this))
418 adapter.pageList.add(pageID)
405 tabs.addTab(tab) 419 tabs.addTab(tab)
406 } 420 }
407 } 421 }
408 return tab 422 return pageID
409 } 423 }
410 424
411 fun notebookPageDestroy(notebook: RelativeLayout, tab: TabLayout.Tab) 425 fun notebookPageDestroy(notebook: RelativeLayout, pageID: Long)
412 { 426 {
413 var pager: ViewPager2? = null 427 var pager: ViewPager2? = null
414 var tabs: TabLayout? = null 428 var tabs: TabLayout? = null
415 429
416 if(notebook.getChildAt(0) is ViewPager2 && notebook.getChildAt(1) is TabLayout) { 430 if(notebook.getChildAt(0) is ViewPager2 && notebook.getChildAt(1) is TabLayout) {
421 tabs = notebook.getChildAt(0) as TabLayout 435 tabs = notebook.getChildAt(0) as TabLayout
422 } 436 }
423 437
424 if(pager != null && tabs != null) { 438 if(pager != null && tabs != null) {
425 var adapter: DWTabViewPagerAdapter = pager.adapter as DWTabViewPagerAdapter 439 var adapter: DWTabViewPagerAdapter = pager.adapter as DWTabViewPagerAdapter
426 440 val index = adapter.pageList.indexOf(pageID)
427 adapter.viewList.removeAt(tab.position) 441 val tab = tabs.getTabAt(index)
428 tabs.removeTab(tab) 442
429 } 443 if (tab != null) {
430 } 444 adapter.viewList.removeAt(index)
431 445 adapter.pageList.removeAt(index)
432 fun notebookPageSetText(notebook: RelativeLayout, tab: TabLayout.Tab, text: String) 446 tabs.removeTab(tab)
447 }
448 }
449 }
450
451 fun notebookPageSetText(notebook: RelativeLayout, pageID: Long, text: String)
433 { 452 {
434 var pager: ViewPager2? = null 453 var pager: ViewPager2? = null
435 var tabs: TabLayout? = null 454 var tabs: TabLayout? = null
436 455
437 if(notebook.getChildAt(0) is ViewPager2 && notebook.getChildAt(1) is TabLayout) { 456 if(notebook.getChildAt(0) is ViewPager2 && notebook.getChildAt(1) is TabLayout) {
441 pager = notebook.getChildAt(1) as ViewPager2 460 pager = notebook.getChildAt(1) as ViewPager2
442 tabs = notebook.getChildAt(0) as TabLayout 461 tabs = notebook.getChildAt(0) as TabLayout
443 } 462 }
444 463
445 if(pager != null && tabs != null) { 464 if(pager != null && tabs != null) {
446 tab.text = text 465 val adapter: DWTabViewPagerAdapter = pager.adapter as DWTabViewPagerAdapter
447 } 466 val index = adapter.pageList.indexOf(pageID)
448 } 467 val tab = tabs.getTabAt(index)
449 468
450 fun notebookPack(notebook: RelativeLayout, tab: TabLayout.Tab, box: LinearLayout) 469 if (tab != null) {
470 tab.text = text
471 }
472 }
473 }
474
475 fun notebookPagePack(notebook: RelativeLayout, pageID: Long, box: LinearLayout)
451 { 476 {
452 var pager: ViewPager2? = null 477 var pager: ViewPager2? = null
453 var tabs: TabLayout? = null 478 var tabs: TabLayout? = null
454 479
455 if(notebook.getChildAt(0) is ViewPager2 && notebook.getChildAt(1) is TabLayout) { 480 if(notebook.getChildAt(0) is ViewPager2 && notebook.getChildAt(1) is TabLayout) {
460 tabs = notebook.getChildAt(0) as TabLayout 485 tabs = notebook.getChildAt(0) as TabLayout
461 } 486 }
462 487
463 if(pager != null && tabs != null) { 488 if(pager != null && tabs != null) {
464 var adapter: DWTabViewPagerAdapter = pager.adapter as DWTabViewPagerAdapter 489 var adapter: DWTabViewPagerAdapter = pager.adapter as DWTabViewPagerAdapter
465 490 val index = adapter.pageList.indexOf(pageID)
466 adapter.viewList[tab.position] = box 491
467 } 492 // Make sure the box is MATCH_PARENT
468 } 493 box.layoutParams = LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
469 494 LinearLayout.LayoutParams.MATCH_PARENT);
470 fun notebookPageGet(notebook: RelativeLayout): TabLayout.Tab? 495
496 adapter.viewList[index] = box
497 }
498 }
499
500 fun notebookPageGet(notebook: RelativeLayout): Long
471 { 501 {
472 var pager: ViewPager2? = null 502 var pager: ViewPager2? = null
473 var tabs: TabLayout? = null 503 var tabs: TabLayout? = null
474 504
475 if(notebook.getChildAt(0) is ViewPager2 && notebook.getChildAt(1) is TabLayout) { 505 if(notebook.getChildAt(0) is ViewPager2 && notebook.getChildAt(1) is TabLayout) {
479 pager = notebook.getChildAt(1) as ViewPager2 509 pager = notebook.getChildAt(1) as ViewPager2
480 tabs = notebook.getChildAt(0) as TabLayout 510 tabs = notebook.getChildAt(0) as TabLayout
481 } 511 }
482 512
483 if(pager != null && tabs != null) { 513 if(pager != null && tabs != null) {
484 return tabs.getTabAt(tabs.selectedTabPosition) 514 var adapter: DWTabViewPagerAdapter = pager.adapter as DWTabViewPagerAdapter
485 } 515 return adapter.pageList.get(tabs.selectedTabPosition)
486 return null 516 }
487 } 517 return 0L
488 518 }
489 fun notebookPageSet(notebook: RelativeLayout, tab: TabLayout.Tab) 519
520 fun notebookPageSet(notebook: RelativeLayout, pageID: Long)
490 { 521 {
491 var pager: ViewPager2? = null 522 var pager: ViewPager2? = null
492 var tabs: TabLayout? = null 523 var tabs: TabLayout? = null
493 524
494 if(notebook.getChildAt(0) is ViewPager2 && notebook.getChildAt(1) is TabLayout) { 525 if(notebook.getChildAt(0) is ViewPager2 && notebook.getChildAt(1) is TabLayout) {
498 pager = notebook.getChildAt(1) as ViewPager2 529 pager = notebook.getChildAt(1) as ViewPager2
499 tabs = notebook.getChildAt(0) as TabLayout 530 tabs = notebook.getChildAt(0) as TabLayout
500 } 531 }
501 532
502 if(pager != null && tabs != null) { 533 if(pager != null && tabs != null) {
534 val adapter: DWTabViewPagerAdapter = pager.adapter as DWTabViewPagerAdapter
535 val index = adapter.pageList.indexOf(pageID)
536 val tab = tabs.getTabAt(index)
537
503 tabs.selectTab(tab) 538 tabs.selectTab(tab)
504 } 539 }
505 } 540 }
506 541
507 fun sliderNew(vertical: Int, increments: Int, cid: Int): SeekBar 542 fun sliderNew(vertical: Int, increments: Int, cid: Int): SeekBar
630 return retval 665 return retval
631 } 666 }
632 667
633 fun dwindowsExit(exitcode: Int) 668 fun dwindowsExit(exitcode: Int)
634 { 669 {
635 this.finishActivity(exitcode) 670 this.finishAffinity()
671 System.exit(exitcode)
672 }
673
674 fun dwindowsShutdown()
675 {
676 this.finishAffinity()
636 } 677 }
637 678
638 /* 679 /*
639 * Native methods that are implemented by the 'dwindows' native library, 680 * Native methods that are implemented by the 'dwindows' native library,
640 * which is packaged with this application. 681 * which is packaged with this application.
641 */ 682 */
642 external fun dwindowsInit(dataDir: String): String 683 external fun dwindowsInit(dataDir: String)
643 external fun eventHandler(obj1: View, obj2: View?, message: Int, str1: String?, str2: String?, int1: Int, int2: Int, int3: Int, int4: Int): Int 684 external fun eventHandler(obj1: View, obj2: View?, message: Int, str1: String?, str2: String?, int1: Int, int2: Int, int3: Int, int4: Int): Int
644 external fun eventHandlerSimple(obj1: View, message: Int) 685 external fun eventHandlerSimple(obj1: View, message: Int)
645 external fun eventHandlerTimer(sigfunc: Long, data: Long): Int 686 external fun eventHandlerTimer(sigfunc: Long, data: Long): Int
646 687
647 companion object 688 companion object