首页 > 解决方案 > Xamarin.Forms ListView 在集合更改之前滚动后在 Android 中呈现错误

问题描述

如果我只是在不滚动的情况下修改 ListView 的数据,一切都会正确显示:

https://gfycat.com/illustriousnastyconey

但是,如果我在更改数据之前滚动,则在数据更改后一切看起来都坏了:

https://gfycat.com/abandonedvelvetyaudad

这是我的 ListView XAML

<ListView ItemsSource="{Binding network.Predictions}" x:Name="ShowsList" IsGroupingEnabled="True" HasUnevenRows="True" ItemTapped="ShowsList_ItemTapped">
        <ListView.GroupHeaderTemplate>
            <DataTemplate>
                <ViewCell Height="25">
                    <Grid BackgroundColor="#CC778899">
                        <Label Text="{Binding Category}" FontSize="Small" TextColor="White" VerticalOptions="Center" Margin="5,0,0,0"/>
                    </Grid>                        
                </ViewCell>
            </DataTemplate>
        </ListView.GroupHeaderTemplate>

        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <Grid BackgroundColor="White">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="Auto"/>
                        </Grid.ColumnDefinitions>

                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                        </Grid.RowDefinitions>

                        <Label Text="{Binding Name}" FontSize="Medium" Margin="5"/>
                        <Label Grid.Column="1" Text="{Binding Status}" FontSize="Small" TextColor="{Binding StatusIndex, Converter={StaticResource StatusColor}}" Margin="5" VerticalOptions="Center/>
                    </Grid>

                </ViewCell>                    
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

此外,在发生这种情况之后,如果我滚动以使混乱的元素离开屏幕,然后再滚动回来,这些元素看起来就好了。它似乎确实正确地分配了空间,并且它们在滚动后正确显示,因此它没有错误地读取数据或类似的东西,它只是没有正确显示。

我有一个想法并将列表视图的背景颜色更改为红色,以查看文本是否呈现为白色,但不,那些缺失的元素是完全透明的。正确的元素都有背景色,但缺失的元素只显示为红色。

标签: c#androidxamlxamarin.forms

解决方案


好像是listview的bug,3.6的初始版本就有这个问题,3.6.0.344457已经修复了,很多开发者都遇到过这个问题,如果你有类似的问题,看看:<a href="https ://github.com/xamarin/Xamarin.Forms/pull/6390" rel="nofollow noreferrer">https://github.com/xamarin/Xamarin.Forms/pull/6390 和最快最简单的解决方法它正在滚动 Xamarin.Forms 版本


推荐阅读