如何在 iOS 中使分组的 ListView 标题正确浮动(而不是粘性)?

问题描述 投票:0回答:3

我有一个分组的ListView(Xamarin Forms)。我需要使组标题浮动(不粘)。在 Android 上,一切都按照我想要的方式运行,但在 iOS 上却出现了问题。

我尝试使用这个: https://forums.xamarin.com/discussion/34696/listview-grouped-style-on-ios

在这种情况下,标题是浮动的,但看起来它创建了新的控件,并且前一个控件并没有消失。我的意思是我看到一个列表与另一个列表重叠。

我可以将顶部列表的背景设置为某种颜色。那么后面的列表将不可见,但这不是这个问题的解决方案。有人可以解释一下如何解决这个问题吗?

ios listview xamarin xamarin.forms header
3个回答
1
投票

此问题的一个简单解决方案是使用 Xamarin.Forms 的 iOS 平台特定功能,该功能允许您在 XAML(或隐藏代码)中轻松地在一行中设置 ListView 组标题样式,如下所示:

<ContentPage ...
             xmlns:ios="clr-namespace:Xamarin.Forms.PlatformConfiguration.iOSSpecific;assembly=Xamarin.Forms.Core">
    <StackLayout Margin="20">
        <ListView ios:ListView.GroupHeaderStyle="Grouped">
            ...
        </ListView>
    </StackLayout>
</ContentPage>

“Grouped”枚举表示当 ListView 滚动时标题单元格不会浮动。有关更多详细信息,请参阅 https://learn.microsoft.com/en-us/xamarin/xamarin-forms/platform/ios/listview-group-header-style


0
投票

您是否从这里引用了该文档?

如果您想让组标题浮动,只需将 IsGroupingEnabled 设置为 true 即可。

在xaml中

<ListView x:Name ="listView" IsGroupingEnabled="true" GroupDisplayBinding="{Binding LongName}" >
   <ListView.ItemTemplate>
    <DataTemplate>
      <TextCell Text="{Binding Name}" Detail = "{Binding Comment}" />
    </DataTemplate>
   </ListView.ItemTemplate>
</ListView>

在代码后面

public partial class GroupedListXaml : ContentPage
{
    private ObservableCollection<GroupedVeggieModel> grouped { get; set; }
    public GroupedListXaml ()
    {
     InitializeComponent ();

     grouped = new ObservableCollection<GroupedVeggieModel> ();
     var veggieGroup = new GroupedVeggieModel () { LongName = "vegetables" };
     var fruitGroup = new GroupedVeggieModel () { LongName = "fruit" };

     veggieGroup.Add (new VeggieModel () { Name = "celery", Comment = "try ants on a log" });
     veggieGroup.Add (new VeggieModel () { Name = "tomato",  Comment = "pairs well with basil" });
     veggieGroup.Add (new VeggieModel () { Name = "zucchini", Comment = "zucchini bread > bannana bread" });
     veggieGroup.Add (new VeggieModel () { Name = "peas",  Comment = "like peas in a pod" });
     veggieGroup.Add (new VeggieModel () { Name = "celery", Comment = "try ants on a log" });
     veggieGroup.Add (new VeggieModel () { Name = "tomato",  Comment = "pairs well with basil" });
     veggieGroup.Add (new VeggieModel () { Name = "zucchini", Comment = "zucchini bread > bannana bread" });
     veggieGroup.Add (new VeggieModel () { Name = "peas",  Comment = "like peas in a pod" });
     veggieGroup.Add (new VeggieModel () { Name = "celery", Comment = "try ants on a log" });
     veggieGroup.Add (new VeggieModel () { Name = "tomato",  Comment = "pairs well with basil" });
     veggieGroup.Add (new VeggieModel () { Name = "zucchini", Comment = "zucchini bread > bannana bread" });
     veggieGroup.Add (new VeggieModel () { Name = "peas",  Comment = "like peas in a pod" });

     fruitGroup.Add (new VeggieModel () {Name = "banana", Comment = "available in chip form factor"});
     fruitGroup.Add (new VeggieModel () {Name = "strawberry", Comment = "spring plant"});
     fruitGroup.Add (new VeggieModel () {Name = "cherry",Comment = "topper for icecream"});
     fruitGroup.Add (new VeggieModel () {Name = "banana", Comment = "available in chip form factor"});
     fruitGroup.Add (new VeggieModel () {Name = "strawberry", Comment = "spring plant"});
     fruitGroup.Add (new VeggieModel () {Name = "cherry",Comment = "topper for icecream"});
     fruitGroup.Add (new VeggieModel () {Name = "banana", Comment = "available in chip form factor"});
     fruitGroup.Add (new VeggieModel () {Name = "strawberry", Comment = "spring plant"});
     fruitGroup.Add (new VeggieModel () {Name = "cherry",Comment = "topper for icecream"});


     grouped.Add (veggieGroup);
     grouped.Add (fruitGroup);
     listView.ItemsSource = grouped;
  }
}


在视图模型中

 public class VeggieModel
 {
  public string Name { get; set; }
  public string Comment { get; set; }       
  public VeggieModel ()
  {
  }
 }

  public class GroupedVeggieModel : ObservableCollection<VeggieModel>
  {
    public string LongName { get; set; }
  }

如果我误解了您的问题。您可以提供包含您的问题的屏幕截图或gif。这有助于解决您的问题。


-1
投票

我发现这个问题已经存在很长时间了,但我需要帮助来解决这个问题。所以我看到 iOS 使用普通标头,我需要执行与 Xamarin for iOS 中默认情况相同的操作,我想要在 Android 中执行。 这就是 iOS 上的 Plain 的样子 enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.