c# - Xamarin 将 ListView 大小(高度)转换为内容?
问题描述
我正在开发一个移动应用程序,我想在 SrollView 中放置一个 Grid,在其下方放置一个具有其元素高度的 ListView,在 ListView 下方放置另一个具有相同高度的 ListView。我在 StackLayout 中放了两个 Listview。它应该看起来像这样 这是应该在 ScrollView 中滚动的内容
但是有了这个实现(下面的代码),第一个 ListView 占据了整个剩余高度,而第二个 ListView 离它很 远
而我的第二个滚动后的Listview很远,这里 也占据了整个滚动视图的高度
这是代码:
<ScrollView>
<Grid RowSpacing="2">
<Grid.RowDefinitions>
<RowDefinition Height="130"/>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid x:Name="RecommendedCaloriesGrid" Grid.Row="0" RowSpacing="2" ColumnSpacing="1"
BackgroundColor="Violet" Margin="3">
// here is code of my Grid 1
</Grid>
</Grid>
<StackLayout Grid.Row="1"> // here I want to make a listview under Each other
<ListView x:Name="ItemsListView" BackgroundColor="red"
ItemsSource="{Binding Items}"
HasUnevenRows="true"
RefreshCommand="{Binding LoadItemsCommand}"
IsPullToRefreshEnabled="true"
IsRefreshing="{Binding IsBusy, Mode=OneWay}"
CachingStrategy="RecycleElement"
ItemSelected="OnItemSelected">
<d:ListView.ItemsSource>
<x:Array Type="{x:Type x:String}">
<x:String>First Item</x:String>
<x:String>Second Item</x:String>
</x:Array>
</d:ListView.ItemsSource>
<ListView.Header>
<StackLayout Margin="5" Spacing="2">
<StackLayout Orientation="Horizontal">
<Label Text="Завтрак" HorizontalOptions="StartAndExpand"
FontSize="Large" FontAttributes="Bold"/>
<Label Text="744 ккал" HorizontalOptions="EndAndExpand"
FontSize="Large" FontAttributes="Bold"/>
</StackLayout>
<Label Text="Рекомендуется 30% от суточного потребления (n ккал)." FontSize="11"/>
</StackLayout>
</ListView.Header>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Padding="10">
<Label Text="{Binding Text}"
d:Text="{Binding .}"
LineBreakMode="NoWrap"
Style="{DynamicResource ListItemTextStyle}"
FontSize="16" />
<Label Text="{Binding Description}"
d:Text="Item descripton"
LineBreakMode="NoWrap"
Style="{DynamicResource ListItemDetailTextStyle}"
FontSize="13" />
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<ListView x:Name="LunchListView" // second ListView under first
ItemsSource="{Binding Items}"
HasUnevenRows="true"
RefreshCommand="{Binding LoadItemsCommand}"
IsPullToRefreshEnabled="true"
IsRefreshing="{Binding IsBusy, Mode=OneWay}"
CachingStrategy="RecycleElement"
ItemSelected="OnItemSelected">
<d:ListView.ItemsSource>
<x:Array Type="{x:Type x:String}">
<x:String>First Item</x:String>
<x:String>Second Item</x:String>
<x:String>Third Item</x:String>
<x:String>Forth Item</x:String>
</x:Array>
</d:ListView.ItemsSource>
// SAME logic here
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</Grid>
</ScrollView>
我该如何解决?
请帮我找到解决方案。我阅读了很多类似下面的文章,但没有找到答案。我想在不计算代码隐藏高度的情况下做到这一点:
Xamarin.Forms - ListView 在运行时更改高度
解决方案
为什么要用两个listview来做这个,你可以用不同DataTemplate
的来实现这个功能。
您可以参考以下代码:
1.在 XAML 中使用 DataTemplateSelector
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-
namespace:Selector;assembly=Selector" x:Class="Selector.HomePage">
<ContentPage.Resources>
<ResourceDictionary>
<DataTemplate x:Key="validPersonTemplate">
<ViewCell>
...
</ViewCell>
</DataTemplate>
<DataTemplate x:Key="invalidPersonTemplate">
<ViewCell>
...
</ViewCell>
</DataTemplate>
<local:PersonDataTemplateSelector x:Key="personDataTemplateSelector"
ValidTemplate="{StaticResource validPersonTemplate}"
InvalidTemplate="{StaticResource invalidPersonTemplate}" />
</ResourceDictionary>
</ContentPage.Resources>
...
</ContentPage>
2.这样使用:
<ListView x:Name="listView" ItemTemplate="{StaticResource personDataTemplateSelector}"
/>
有关更多详细信息,您可以查看:
此外,您还可以try ListView Grouping
,了解更多详情:
https://docs.microsoft.com/en-ie/xamarin/xamarin-forms/user-interface/listview/
推荐阅读
- algorithm - 分区相等子集和
- javascript - 在 JavaScript 中获取超类
- java - JNI 可以将非常大的字符串传递给 C++ 吗?
- python - sklearn PolynomialFeatures 给了我多个拟合线
- c++ - VC++ 一个解决方案中的多个项目 - 链接器问题 (LNK2019)
- swift - 如何快速从 Promise 返回值
- pandas - 如何使用包含 dtype 列表的熊猫系列为特定条件选择值?
- dns - 如何将特定域/子域指向另一个域
- python-3.x - Discord.py - “discord.errors.ClientException:找不到 ffmpeg。”
- statistics - 矩阵和向量之间的差异/关系