-
Notifications
You must be signed in to change notification settings - Fork 2k
[Android] Shell: Defer tab infrastructure for single-page startup #37321
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -149,21 +149,6 @@ protected override AView CreatePlatformElement() | |
| _rootLayout = rootView.FindViewById<LinearLayout>(Resource.Id.shellsection_coordinator); | ||
| _viewPager = rootView.FindViewById<ViewPager2>(Resource.Id.shellsection_viewpager); | ||
|
|
||
| // Create TabLayout programmatically (no longer from XML layout). | ||
| // It will be placed into navigationlayout_toptabs via PlaceTopTabs(). | ||
| var context = MauiContext?.Context | ||
| ?? throw new InvalidOperationException("MauiContext.Context cannot be null"); | ||
|
|
||
| int actionBarHeight = context.GetActionBarHeight(); | ||
|
|
||
| _contentTabLayout = new TabLayout(context) | ||
| { | ||
| Id = AView.GenerateViewId(), | ||
| LayoutParameters = new LP(LP.MatchParent, actionBarHeight), | ||
| Visibility = ViewStates.Gone, // Hidden by default (shown when > 1 tab) | ||
| TabMode = TabLayout.ModeScrollable | ||
| }; | ||
|
|
||
| return rootView; | ||
| } | ||
|
|
||
|
|
@@ -253,18 +238,7 @@ internal void SetupViewPagerAdapter() | |
| var visibleItems = SectionController.GetItems(); | ||
| _viewPager.OffscreenPageLimit = Math.Max(visibleItems.Count, 1); | ||
|
|
||
| // Setup TabbedViewManager for top tab management. | ||
| // Pre-assign Shell's TabLayout (specific sizing) before SetElement. | ||
| _shellSectionAdapter = new ShellSectionTabbedViewAdapter(VirtualView); | ||
| _tabbedViewManager = new TabbedViewManager(MauiContext, _viewPager) | ||
| { | ||
| TabLayout = _contentTabLayout | ||
| }; | ||
| _tabbedViewManager.SetElement(_shellSectionAdapter); | ||
|
|
||
| // Register page change callback (stored in field for cleanup in DisconnectHandler) | ||
| _pageChangedCallback = new ViewPagerPageChangeCallback(this); | ||
| _viewPager.RegisterOnPageChangeCallback(_pageChangedCallback); | ||
| SetupTabbedViewManager(); | ||
|
|
||
| // Update TabLayout visibility based on item count | ||
| UpdateTabLayoutVisibility(); | ||
|
|
@@ -275,10 +249,6 @@ internal void SetupViewPagerAdapter() | |
| // Set initial position | ||
| SetInitialPosition(); | ||
|
|
||
| // Setup TabLayout appearance tracker | ||
| _tabLayoutAppearanceTracker = _shellContext.CreateTabLayoutAppearanceTracker(VirtualView); | ||
|
|
||
| // Register as appearance observer for TabLayout updates | ||
| var shell = VirtualView.FindParentOfType<Shell>(); | ||
| if (shell is not null) | ||
| { | ||
|
|
@@ -305,6 +275,53 @@ internal void SetupViewPagerAdapter() | |
| } | ||
| } | ||
|
|
||
| void SetupTabbedViewManager() | ||
| { | ||
| if (_tabbedViewManager is not null || VirtualView is null || _viewPager is null || MauiContext is null || _shellContext is null) | ||
|
jonathanpeppers marked this conversation as resolved.
|
||
| { | ||
| return; | ||
| } | ||
|
|
||
| if (SectionController.GetItems().Count <= 1) | ||
|
jonathanpeppers marked this conversation as resolved.
|
||
| { | ||
| return; | ||
| } | ||
|
|
||
| var context = MauiContext.Context | ||
| ?? throw new InvalidOperationException("MauiContext.Context cannot be null"); | ||
|
|
||
| _contentTabLayout = new TabLayout(context) | ||
| { | ||
| Id = AView.GenerateViewId(), | ||
| LayoutParameters = new LP(LP.MatchParent, context.GetActionBarHeight()), | ||
| Visibility = ViewStates.Gone, | ||
| TabMode = TabLayout.ModeScrollable | ||
| }; | ||
|
|
||
| _shellSectionAdapter = new ShellSectionTabbedViewAdapter(VirtualView); | ||
| _tabbedViewManager = new TabbedViewManager(MauiContext, _viewPager) | ||
| { | ||
| TabLayout = _contentTabLayout | ||
| }; | ||
| _tabbedViewManager.SetElement(_shellSectionAdapter); | ||
|
|
||
| _pageChangedCallback = new ViewPagerPageChangeCallback(this); | ||
| _viewPager.RegisterOnPageChangeCallback(_pageChangedCallback); | ||
|
|
||
| _tabLayoutAppearanceTracker = _shellContext.CreateTabLayoutAppearanceTracker(VirtualView); | ||
|
|
||
| // Initial setup registers the appearance observer immediately afterward. When | ||
| // setup was deferred, replay the appearance for the newly-created TabLayout. | ||
| if (_registeredShell is not null && IsCurrentlyActiveSection() && VirtualView.CurrentItem is ShellContent currentContent) | ||
| { | ||
| var page = ((IShellContentController)currentContent).GetOrCreateContent(); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
[moderate] Logic and Correctness — The deferred-setup appearance replay uses the section's root content page ( |
||
| if (page is not null) | ||
| { | ||
| ((IShellController)_registeredShell).AppearanceChanged(page, false); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| void SetInitialPosition() | ||
| { | ||
| if (VirtualView?.CurrentItem is null || _viewPager is null) | ||
|
|
@@ -646,6 +663,7 @@ void OnItemsCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e | |
| var visibleCount = SectionController.GetItems().Count; | ||
| _viewPager?.OffscreenPageLimit = Math.Max(visibleCount, 1); | ||
|
|
||
| SetupTabbedViewManager(); | ||
| UpdateTabLayoutVisibility(); | ||
| UpdateViewPagerUserInput(); | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.