【问题标题】:Xamarin Listview with grouping won't scroll带有分组的 Xamarin Listview 不会滚动
【发布时间】:2018-10-23 21:58:44
【问题描述】:

我正在开发我的第一个 Xamarin 应用程序。我想用分组制作一个列表视图,它成功了。我唯一的问题是它不会滚动。我在另一个页面上的另一个列表视图滚动没有问题,但我的分组列表视图不会这样做。我在 Android 模拟器和我的 Android 手机上都试过这个(我没有 macbook 或在 iOS 上测试的东西),它不会在其中任何一个上滚动。我查了一下,但是很多人把列表视图放在滚动视图中,而我没有这样做。

这是我的 XAML 代码:

 <StackLayout Margin="10" Orientation="Vertical">

        <Label Text="{Binding Title}" FontAttributes="Bold" 
               FontSize="20" HorizontalOptions="CenterAndExpand" />
        <Label Text="{Binding Subtitle}" FontAttributes="Italic" 
               FontSize="15" HorizontalOptions="CenterAndExpand" />

        <ListView HasUnevenRows="True" SeparatorColor="Accent" 
                  VerticalOptions="FillAndExpand" IsEnabled="False"
                  IsGroupingEnabled="True" GroupDisplayBinding="{Binding Key}" 
                  ItemsSource="{Binding ScansGroup}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout Spacing="4">
                            <StackLayout Orientation="Horizontal" Margin="10,7,10,1">
                                <Label Text="{Binding Location, StringFormat='{0}'}" 
                                       FontAttributes="Bold" FontSize="16" />
                                <Label Text="{Binding DateTime, StringFormat='{0:dd/MM/y HH:mm}'}"
                                        HorizontalOptions="EndAndExpand" />
                            </StackLayout>
                            <StackLayout Orientation="Horizontal">
                                <Label Text="{Binding ElapsedTimeOnLocation}" 
                                       HorizontalOptions="Start"
                                        Margin="10,0,10,7" />
                            </StackLayout>
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackLayout>

我把它放在一个 MVVM 结构中,我使用从这里得到的 MVVM 助手进行分组 https://channel9.msdn.com/Shows/XamarinShow/The-Xamarin-Show-12-MVVM-Helpers 。 我用于分组的代码如下:

     public ObservableRangeCollection<Grouping<string, ScanViewModel>> ScansGroup { get; } = 
new ObservableRangeCollection<Grouping<string, ScanViewModel>>();

    void Group()
    {
         var grouped = from scan in Scans
                       group scan by scan.Day into scanGroup
                       select new Grouping<string, ScanViewModel>(scanGroup.Key, scanGroup);

         ScansGroup.ReplaceRange(grouped);

    }

分组显示完美,列表也显示。唯一的问题是 我无法滚动。有人可以帮我吗?

【问题讨论】:

  • @DiegoRafaelSouza 显然我犯了一个星期一早上的错误并粘贴了错误的代码。感谢您指出。这个问题现在有了正确的代码。
  • 我很清楚这一点。当并非所有项目都适合屏幕时,我希望能够滚动,但事实并非如此。

标签: c# xaml listview scroll xamarin.forms


【解决方案1】:

我想通了。你的分组没有问题。在您的代码中,您将IsEnabled="False" 设置为ListView。它使列表视图的滚动功能仅在您从ListView 内的启用项目中拖动时就可以处理,即使这样,滚动也被贬低了,并且用户体验非常差。

只需像这样设置您的ListView

<ListView HasUnevenRows="True" 
          SeparatorColor="Accent" 
          VerticalOptions="FillAndExpand" 
          IsEnabled="True"
          IsGroupingEnabled="True" 
          GroupDisplayBinding="{Binding Key}" 
          ItemsSource="{Binding ScansGroup}">
    ...
</ListView>

我不能告诉你它是否是这样设计的,或者它是否是一个错误,但它是你问题的原因。

您这样做可能是为了处理一些不受欢迎的行为。如果是这样,请告诉我们您对此 IsEnabled="False" 的具体意图,然后我们将能够为您提供帮助。

希望对你有帮助。

【讨论】:

  • 是的,就是这样!我禁用它是因为我不希望它成为可点击的列表。我没有想到它也会禁用滚动。我认为不会有一个简单的解决方法使字段不可点击但列表保持可滚动?
  • @Marijke 不幸的是,ListView 没有“IsSelectable”属性。如果您订阅 ItemSelected 事件并“忽略”其上的选择(就像设置 ((ListView)sender)?.SelectedItem = null; 总是一样)怎么办?
  • 谢谢!好主意!
【解决方案2】:

您没有为组添加模板。将此添加到&lt;ListView&gt;

<ListView.GroupHeaderTemplate >
    <DataTemplate >
        <ViewCell Height="28" >
        <Label Text="{Binding GroupName}" VerticalTextAlignment="Center" HorizontalOptions="Start" />
        </ViewCell>
    </DataTemplate>
</ListView.GroupHeaderTemplate>

此外,在代码弯曲或 ViewModel 中,您需要绑定到列表中所需项目的组类,例如:

public class GroupDetails : ObservableCollection<SomeItemsToShow>
{
    public string GroupName { get; set; }
}

查看此示例了解更多详情: https://xamarinhelp.com/xamarin-forms-listview-grouping/

【讨论】:

  • 我编辑了我的问题并在后面添加了我的代码。我使用 MVVM 助手进行分组,并根据我现在也包含在我的问题中的视频进行分组。似乎 GroupHeaderTemplate 在该视频中并不是真正需要的?如果我尝试实现您的代码,它会给我错误并且无法使其正常工作。我的问题不是我的分组没有显示,我只是在列表不适合我的屏幕时无法滚动。
  • 您是否查看了提供的链接以获取更多详细信息?这实际上是官方的做事方式。这种方法应该更适合使用,而不是每次都需要更新和支持原始版本的非官方 MVVM 框架。
  • @Marijke ,我没有看你用来创建列表的视频,但我没有看到自定义组的选项,代码看起来很复杂
猜你喜欢
  • 2017-08-16
  • 1970-01-01
  • 2016-09-03
  • 1970-01-01
  • 2015-10-28
  • 2014-11-25
  • 1970-01-01
相关资源
最近更新 更多