Skip to content
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

Fix iOS resizing when content in item template changes size #34

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 0 additions & 14 deletions .vscode/launch.json

This file was deleted.

12 changes: 0 additions & 12 deletions .vscode/tasks.json

This file was deleted.

22 changes: 20 additions & 2 deletions Sample/VirtualListViewSample/Section.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
namespace VirtualListViewSample;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;

public class Section : List<string>
namespace VirtualListViewSample;

public class Section : List<SectionItem>
{
public string Title { get; set; }
}

public partial class SectionItem : ObservableObject
{
[ObservableProperty]
string text;

[ObservableProperty]
bool isDetailVisible;

[RelayCommand]
void ToggleDetail()
{
IsDetailVisible = !IsDetailVisible;
}
}
6 changes: 3 additions & 3 deletions Sample/VirtualListViewSample/SectionedAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace VirtualListViewSample;

public class SectionedAdapter : VirtualListViewAdapterBase<Section, string>
public class SectionedAdapter : VirtualListViewAdapterBase<Section, SectionItem>
{
public SectionedAdapter(IList<Section> items) : base()
{
Expand All @@ -20,7 +20,7 @@ public override int GetNumberOfSections()
public override int GetNumberOfItemsInSection(int sectionIndex)
=> Items[sectionIndex].Count;

public override string GetItem(int sectionIndex, int itemIndex)
public override SectionItem GetItem(int sectionIndex, int itemIndex)
=> Items[sectionIndex][itemIndex];

public void AddItem(string sectionTitle, string itemName)
Expand All @@ -33,7 +33,7 @@ public void AddItem(string sectionTitle, string itemName)
Items.Add(section);
}

section.Add(itemName);
section.Add(new SectionItem { Text = itemName });
InvalidateData();
}

Expand Down
12 changes: 10 additions & 2 deletions Sample/VirtualListViewSample/SectionedAdapterPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,21 @@
</vlv:VirtualListView.SectionHeaderTemplate>
<vlv:VirtualListView.ItemTemplate>
<DataTemplate>
<Border
<Grid>
<Border
Margin="10,0,0,0"
Padding="4"
Background="LightBlue"
StrokeShape="{RoundRectangle CornerRadius=10}">
<Label Margin="10,6,10,6" Text="{Binding .}" />
<VerticalStackLayout>
<Label Margin="10,6,10,6" Text="{Binding Text}" />
<Button Text="Expand" Command="{Binding ToggleDetailCommand}" />
<Border Background="Green" WidthRequest="100" HeightRequest="100" IsVisible="{Binding IsDetailVisible}">

</Border>
</VerticalStackLayout>
</Border>
</Grid>
</DataTemplate>
</vlv:VirtualListView.ItemTemplate>
</vlv:VirtualListView>
Expand Down
54 changes: 31 additions & 23 deletions VirtualListView/Apple/CvCell.ios.maccatalyst.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using CoreGraphics;
using Foundation;
using Microsoft.Maui.Platform;
using Microsoft.VisualBasic;
using System.Diagnostics.CodeAnalysis;
using UIKit;

namespace Microsoft.Maui;
Expand Down Expand Up @@ -39,15 +37,15 @@
get
{
if (keyCommands?.TryGetTarget(out var commands) ?? false)
return commands;
return commands;

var v = new[]
{
UIKeyCommand.Create(new NSString("\r"), 0, new ObjCRuntime.Selector("keyCommandSelect")),
UIKeyCommand.Create(new NSString(" "), 0, new ObjCRuntime.Selector("keyCommandSelect")),
};

keyCommands = new WeakReference<UIKeyCommand[]>(v);
keyCommands = new WeakReference<UIKeyCommand[]>(v);

return v;
}
Expand Down Expand Up @@ -121,33 +119,43 @@

public void SetupView(IView view)
{
// Create a new platform native view if we don't have one yet
if (!(NativeView?.TryGetTarget(out var _) ?? false))
{
if (view is VisualElement ve) {
ve.MeasureInvalidated += (s, e) => {

Check warning on line 123 in VirtualListView/Apple/CvCell.ios.maccatalyst.cs

View workflow job for this annotation

GitHub Actions / Build

Subscribing to events with instance method '' could cause memory leaks in an NSObject subclass. Remove the subscription or convert the method to a static method.

Check warning on line 123 in VirtualListView/Apple/CvCell.ios.maccatalyst.cs

View workflow job for this annotation

GitHub Actions / Build

Subscribing to events with instance method '' could cause memory leaks in an NSObject subclass. Remove the subscription or convert the method to a static method.

Check warning on line 123 in VirtualListView/Apple/CvCell.ios.maccatalyst.cs

View workflow job for this annotation

GitHub Actions / Build

Subscribing to events with instance method '' could cause memory leaks in an NSObject subclass. Remove the subscription or convert the method to a static method.

Check warning on line 123 in VirtualListView/Apple/CvCell.ios.maccatalyst.cs

View workflow job for this annotation

GitHub Actions / Build

Subscribing to events with instance method '' could cause memory leaks in an NSObject subclass. Remove the subscription or convert the method to a static method.

Check warning on line 123 in VirtualListView/Apple/CvCell.ios.maccatalyst.cs

View workflow job for this annotation

GitHub Actions / Build

Subscribing to events with instance method '' could cause memory leaks in an NSObject subclass. Remove the subscription or convert the method to a static method.

Check warning on line 123 in VirtualListView/Apple/CvCell.ios.maccatalyst.cs

View workflow job for this annotation

GitHub Actions / Build

Subscribing to events with instance method '' could cause memory leaks in an NSObject subclass. Remove the subscription or convert the method to a static method.

Check warning on line 123 in VirtualListView/Apple/CvCell.ios.maccatalyst.cs

View workflow job for this annotation

GitHub Actions / Build

Subscribing to events with instance method '' could cause memory leaks in an NSObject subclass. Remove the subscription or convert the method to a static method.

Check warning on line 123 in VirtualListView/Apple/CvCell.ios.maccatalyst.cs

View workflow job for this annotation

GitHub Actions / Build

Subscribing to events with instance method '' could cause memory leaks in an NSObject subclass. Remove the subscription or convert the method to a static method.

Check warning on line 123 in VirtualListView/Apple/CvCell.ios.maccatalyst.cs

View workflow job for this annotation

GitHub Actions / Build

Subscribing to events with instance method '' could cause memory leaks in an NSObject subclass. Remove the subscription or convert the method to a static method.

Check warning on line 123 in VirtualListView/Apple/CvCell.ios.maccatalyst.cs

View workflow job for this annotation

GitHub Actions / Build

Subscribing to events with instance method '' could cause memory leaks in an NSObject subclass. Remove the subscription or convert the method to a static method.
System.Diagnostics.Debug.WriteLine("Measure invalidated");
//this.SizeToFit();
};
}
// if (view is VisualElement ve) {
// ve.SizeChanged += (s, e) => {
// this.SetNeedsLayout();
// };
// }

// Create a new platform native view if we don't have one yet
if (!(NativeView?.TryGetTarget(out var _) ?? false))
{
var nativeView = view.ToPlatform(this.Handler.MauiContext);
nativeView.Frame = this.ContentView.Frame;
nativeView.AutoresizingMask = UIViewAutoresizing.FlexibleHeight | UIViewAutoresizing.FlexibleWidth;

nativeView.Frame = this.ContentView.Frame;
nativeView.AutoresizingMask = UIViewAutoresizing.FlexibleHeight | UIViewAutoresizing.FlexibleWidth;
this.ContentView.AddSubview(nativeView);

NativeView = new WeakReference<UIView>(nativeView);
}
}

if (!(VirtualView?.TryGetTarget(out var virtualView) ?? false) || (virtualView?.Handler is null))
{
VirtualView = new WeakReference<IView>(view);
}
}
if (!(VirtualView?.TryGetTarget(out var virtualView) ?? false) || (virtualView?.Handler is null))
{
VirtualView = new WeakReference<IView>(view);
}
}

public void UpdatePosition(PositionInfo positionInfo)
{
PositionInfo = positionInfo;
if (VirtualView?.TryGetTarget(out var virtualView) ?? false)
PositionInfo = positionInfo;
if (VirtualView?.TryGetTarget(out var virtualView) ?? false)
{
if (virtualView is IPositionInfo viewPositionInfo)
viewPositionInfo.Update(positionInfo);
}
}
if (virtualView is IPositionInfo viewPositionInfo)
viewPositionInfo.Update(positionInfo);
}
}

class TapHandlerCallback
{
Expand Down
Loading