Xamarin Forms:如何根据日期删除列表视图项之间的空 space?
Xamarin Forms: How to remove empty space between listview items based on date?
我正在使用 listview 来显示项目列表。这些项目是根据他们的日期列出的。如果一天中有多个事件,我需要删除事件之间的 space 并隐藏第一个项目以外的项目的日期。现在我使用以下代码使用日可见性变量隐藏日期:
foreach (var eventshb in eventsHBList)
{
bool isFirstItem = true;
foreach (var events in eventshb.eventsList)
{
if (isFirstItem)
{
eventshb.dayVisibility = true;
isFirstItem = false;
}
else
{
eventshb.dayVisibility = false;
}
eventshb.eventsListTO = events;
EventAllItems.Add(eventshb);
}
}
我正在尝试删除项目之间的空白 space。对于参考,我添加了当前的 UI 和所需的 UI Screenshots.
使用列表视图显示事件的demo项目已上传到驱动器中。
我认为使用 Grouping 是最好的方法,因为你可以为你的头定义一个 DataTemplate
。
如果你不想做大的改变你可以尝试使用iValueConverter
,根据dayVisibility
改变Margin
的值,但这似乎只是减少了间距比较.
创建 IsHasSpaceConvert :
public class IsHasSpaceConvert : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if ((bool)value)
{
return new Thickness(5,10,5,0);
}
else
{
return new Thickness(5,-5, 5,0);
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
然后在你的 xaml:
<ContentPage.Resources>
<ResourceDictionary>
<local:IsHasSpaceConvert x:Key="isHasSpace" />
</ResourceDictionary>
</ContentPage.Resources>
<StackLayout>
<ListView
x:Name="EventsListview"
IsVisible="true"
SeparatorVisibility="Default"
ItemsSource="{Binding EventAllItems, Mode=TwoWay}"
HasUnevenRows="True">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.View>
<StackLayout
Margin="{Binding dayVisibility,
Converter={StaticResource isHasSpace}}"
Padding="5"
Orientation="Vertical">
<Frame>
<StackLayout
Orientation="Vertical">
<Label
Text="{Binding finalDay}"
IsVisible="{Binding dayVisibility}"
TextColor="Black"
HorizontalOptions="Start"
HorizontalTextAlignment="Start"
VerticalTextAlignment="Center"
VerticalOptions="CenterAndExpand">
<Label.FontSize>
<OnIdiom x:TypeArguments="x:Double">
<OnIdiom.Phone>15</OnIdiom.Phone>
<OnIdiom.Tablet>22</OnIdiom.Tablet>
<OnIdiom.Desktop>15</OnIdiom.Desktop>
</OnIdiom>
</Label.FontSize>
</Label>
<Label
HorizontalOptions="StartAndExpand"
Text="{Binding eventsListTO.title}"
VerticalOptions="CenterAndExpand"
TextColor="Black">
<Label.FontSize>
<OnIdiom x:TypeArguments="x:Double">
<OnIdiom.Phone>20</OnIdiom.Phone>
<OnIdiom.Tablet>30</OnIdiom.Tablet>
<OnIdiom.Desktop>20</OnIdiom.Desktop>
</OnIdiom>
</Label.FontSize>
</Label>
</StackLayout>
</Frame>
</StackLayout>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
<ListView.Footer>
<Label/>
</ListView.Footer>
</ListView>
</StackLayout>
我正在使用 listview 来显示项目列表。这些项目是根据他们的日期列出的。如果一天中有多个事件,我需要删除事件之间的 space 并隐藏第一个项目以外的项目的日期。现在我使用以下代码使用日可见性变量隐藏日期:
foreach (var eventshb in eventsHBList)
{
bool isFirstItem = true;
foreach (var events in eventshb.eventsList)
{
if (isFirstItem)
{
eventshb.dayVisibility = true;
isFirstItem = false;
}
else
{
eventshb.dayVisibility = false;
}
eventshb.eventsListTO = events;
EventAllItems.Add(eventshb);
}
}
我正在尝试删除项目之间的空白 space。对于参考,我添加了当前的 UI 和所需的 UI Screenshots.
使用列表视图显示事件的demo项目已上传到驱动器中。
我认为使用 Grouping 是最好的方法,因为你可以为你的头定义一个 DataTemplate
。
如果你不想做大的改变你可以尝试使用iValueConverter
,根据dayVisibility
改变Margin
的值,但这似乎只是减少了间距比较.
创建 IsHasSpaceConvert :
public class IsHasSpaceConvert : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if ((bool)value)
{
return new Thickness(5,10,5,0);
}
else
{
return new Thickness(5,-5, 5,0);
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
然后在你的 xaml:
<ContentPage.Resources>
<ResourceDictionary>
<local:IsHasSpaceConvert x:Key="isHasSpace" />
</ResourceDictionary>
</ContentPage.Resources>
<StackLayout>
<ListView
x:Name="EventsListview"
IsVisible="true"
SeparatorVisibility="Default"
ItemsSource="{Binding EventAllItems, Mode=TwoWay}"
HasUnevenRows="True">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.View>
<StackLayout
Margin="{Binding dayVisibility,
Converter={StaticResource isHasSpace}}"
Padding="5"
Orientation="Vertical">
<Frame>
<StackLayout
Orientation="Vertical">
<Label
Text="{Binding finalDay}"
IsVisible="{Binding dayVisibility}"
TextColor="Black"
HorizontalOptions="Start"
HorizontalTextAlignment="Start"
VerticalTextAlignment="Center"
VerticalOptions="CenterAndExpand">
<Label.FontSize>
<OnIdiom x:TypeArguments="x:Double">
<OnIdiom.Phone>15</OnIdiom.Phone>
<OnIdiom.Tablet>22</OnIdiom.Tablet>
<OnIdiom.Desktop>15</OnIdiom.Desktop>
</OnIdiom>
</Label.FontSize>
</Label>
<Label
HorizontalOptions="StartAndExpand"
Text="{Binding eventsListTO.title}"
VerticalOptions="CenterAndExpand"
TextColor="Black">
<Label.FontSize>
<OnIdiom x:TypeArguments="x:Double">
<OnIdiom.Phone>20</OnIdiom.Phone>
<OnIdiom.Tablet>30</OnIdiom.Tablet>
<OnIdiom.Desktop>20</OnIdiom.Desktop>
</OnIdiom>
</Label.FontSize>
</Label>
</StackLayout>
</Frame>
</StackLayout>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
<ListView.Footer>
<Label/>
</ListView.Footer>
</ListView>
</StackLayout>