【发布时间】:2020-02-01 20:30:00
【问题描述】:
我正在尝试制作一个可扩展的 ListView,但在 Android 和 iOS 中遇到问题(不工作)(在 UWP 中工作)。尝试了很多布局,还尝试在列表中创建一个列表并面临相同的结果。 Android 和 iOS 拒绝更新 Cell 的高度。
我的 Xaml
<ListView ItemsSource="{Binding groups}"
IsGroupingEnabled="true"
HasUnevenRows="False"
SelectionMode="None"
VerticalScrollBarVisibility="Never"
>
<ListView.GroupHeaderTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Padding="5" VerticalOptions="FillAndExpand">
<StackLayout.GestureRecognizers>
<TapGestureRecognizer Command="{Binding HideShowItems}" CommandParameter="{Binding .}" />
</StackLayout.GestureRecognizers>
<Label Text="{Binding GroupKey}" HorizontalOptions="Center" HorizontalTextAlignment="Center" VerticalTextAlignment="Center" VerticalOptions="FillAndExpand"/>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.GroupHeaderTemplate>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.View>
<Grid Padding="0" HeightRequest="{Binding rowHeight}" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<BoxView Grid.Row="0" HorizontalOptions="FillAndExpand"
BackgroundColor="{Binding BtnColour}" />
</Grid>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
处理更改的命令
public Command HideShowItems => new Command(async (sender) =>
{
var dropDown = sender as DropDownMenu;
foreach(DropDownButton dr in dropDown)
{
if (dr.rowHeight > 0)
{
while(dr.rowHeight > 0)
{
dr.rowHeight = dr.rowHeight - 5;
await Task.Delay(5);
}
}
else
{
while(dr.rowHeight < 40)
{
dr.rowHeight = dr.rowHeight + 5;
await Task.Delay(5);
}
}
}
});
尝试了很多网上找到的示例,但似乎没有一个。
仍处于学习阶段,如果您在看到代码中有任何可以做得更好的地方时提供反馈,将会很有帮助。
非常感谢!
【问题讨论】:
-
使用 CollectionView。它应该能够比 ListView 更好地处理动态大小
-
如果将 hasunevenrows 设置为 true 会发生什么?
-
@RodrigoJuarez 什么都没有。设置为“true”或“false”都没有关系。
-
@Jason 我对 CollectionView 有过一些不好的经历。但如果有机会,我会在未来的项目中尝试。谢谢!
标签: xamarin.forms xamarin.forms.listview