forked from dotnet/maui
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[android] improve performance of Shell flyout opening
Context: dotnet#10713 Context: https://github.com/supershopping/ShellFlyoutLagging In reviewing the above customer sample, there is a noticeable delay on some devices when you open a Shell flyout for the first time. This was pretty close to a default Shell setup: <Shell Shell.FlyoutBehavior="Flyout"> <FlyoutItem Title="Main Page"> <ShellContent Title="Main Page" Route="MainPage" ContentTemplate="{DataTemplate local:MainPage}" /> </FlyoutItem> <FlyoutItem Title="Test Page"> <ShellContent Title="Test Page" Route="TestPage" ContentTemplate="{DataTemplate local:TestPage}" /> </FlyoutItem> </Shell> Reviewing the code, there was an oddity where we create & replace a `RecyclerView`'s `LayoutManager`: SetLayoutManager(_layoutManager = new ScrollLayoutManager(context, (int)Orientation.Vertical, false)); SetLayoutManager(new LinearLayoutManager(context, (int)Orientation.Vertical, false)); We think this is likely something that went wrong during a git merge, etc. In addition to removing code, I create a new `ShellRecyclerView` in Java that reduces the amount of interop calls from C# to Java. We can do all the work now in the `ShellRecyclerView`'s constructor: var adapter = new ShellFlyoutRecyclerAdapter(ShellContext, OnElementSelected); var recyclerView = new ShellRecyclerView(context, adapter); `dotnet-trace` output shows these changes have a reasonable impact on a Pixel 5 device: Before: 35.34ms microsoft.maui.controls!Microsoft.Maui.Controls.Platform.Compatibility.ShellFlyoutTemplatedContentRenderer.CreateFlyoutContent() After: 17.64ms microsoft.maui.controls!Microsoft.Maui.Controls.Platform.Compatibility.ShellFlyoutTemplatedContentRenderer.CreateFlyoutContent() Saving about ~17.7ms of time opening the Shell flyout on Android.
- Loading branch information
1 parent
1039c70
commit 1b74327
Showing
4 changed files
with
53 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
src/Core/AndroidNative/maui/src/main/java/com/microsoft/maui/ShellRecyclerView.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.microsoft.maui; | ||
|
||
import android.content.Context; | ||
import android.view.View; | ||
|
||
import androidx.annotation.NonNull; | ||
import androidx.coordinatorlayout.widget.CoordinatorLayout; | ||
import androidx.recyclerview.widget.LinearLayoutManager; | ||
import androidx.recyclerview.widget.RecyclerView; | ||
|
||
import com.google.android.material.appbar.AppBarLayout; | ||
|
||
/** | ||
* Used by Shell flyout menu | ||
*/ | ||
public class ShellRecyclerView extends RecyclerView { | ||
public ShellRecyclerView(@NonNull Context context, Adapter adapter) | ||
{ | ||
super(context); | ||
|
||
setClipToPadding(false); | ||
setLayoutManager(new LinearLayoutManager(context, RecyclerView.VERTICAL, false)); | ||
setLayoutParameters(this, true); | ||
setAdapter(adapter); | ||
} | ||
|
||
/** | ||
* Configures a default, scrollable CoordinatorLayout.LayoutParams that matches its parent | ||
* @param view | ||
* @param scrollingEnabled | ||
*/ | ||
public static void setLayoutParameters(View view, boolean scrollingEnabled) | ||
{ | ||
CoordinatorLayout.LayoutParams layoutParams = new CoordinatorLayout.LayoutParams(CoordinatorLayout.LayoutParams.MATCH_PARENT, CoordinatorLayout.LayoutParams.MATCH_PARENT); | ||
if (scrollingEnabled) { | ||
layoutParams.setBehavior(new AppBarLayout.ScrollingViewBehavior()); | ||
} | ||
view.setLayoutParams(layoutParams); | ||
} | ||
} |
Binary file not shown.