Xamarin:具有回收元素和动态行高的Listview

时间:2019-07-07 15:43:00

标签: listview xamarin caching

我目前正在开发类似whatsapp的聊天应用。

除缓存策略外,一切都正常。我想使用recycle元素,因为它在我的场景中看起来最流畅。

我使用日期模板选择器,并且列表视图设置为hasunevenrows = true(由于更改了消息长度)。

问题在于,当我向上滚动列表视图然后再次向下滚动列表时,较长的多行消息会被切断。我认为这与我的viewcell(回收元素)的重用有关,但我只是不明白为什么listview不重新计算高度以适合消息内容。

我正在使用绑定,这是我的列表视图:

<ListView CachingStrategy="RetainElement" Grid.Row="0" Grid.Column="0" x:Name="ChatListView" IsVisible="{Binding ChatVisible}" ItemsSource="{Binding ChatList, Mode=TwoWay}" ItemTemplate="{StaticResource ChatSelectorTemplate}" HasUnevenRows="True" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" SeparatorVisibility="None">
                    <ListView.Behaviors>
                        <behavior:EventToCommandBehavior EventName="ItemTapped" Command="{Binding ChatTap}" Converter="{StaticResource TappedItemConverter}" />
                        <behavior:EventToCommandBehavior EventName="ItemAppearing" Command="{Binding ItemAppearingEvent}" Converter="{StaticResource ItemAppearingConverter} " />
                    </ListView.Behaviors>
                </ListView>

有问题的DataTemplate:

<DataTemplate x:Key="OtherTemplate">
            <local:CustomViewCell>
                <Grid ColumnSpacing="5" Padding="0,5,0,5" HorizontalOptions="FillAndExpand">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="*" />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="50" />
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="40" />
                    </Grid.ColumnDefinitions>
                    <ff:CachedImage LoadingPlaceholder="nopic.png" Grid.Row="0" Grid.Column="0" DownsampleToViewSize="True" WidthRequest="50" HeightRequest="50" Aspect="AspectFit" Source="{Binding ProfilePic}" VerticalOptions="StartAndExpand">
                        <ff:CachedImage.Transformations>
                            <ffTransformations:CircleTransformation />
                        </ff:CachedImage.Transformations>
                    </ff:CachedImage>
                    <Frame Grid.Row="0" Grid.Column="1" HorizontalOptions="FillAndExpand" Padding="15,10,15,10" BackgroundColor="#eaeaea" CornerRadius="20" HasShadow="False">
                        <StackLayout Orientation="Vertical" HorizontalOptions="FillAndExpand" Spacing="3">
                            <Label TextColor="#616161" FontSize="14" Text="{Binding Name}">
                                <Label.FontFamily>
                                    <OnPlatform x:TypeArguments="x:String">
                                        <On Platform="iOS" Value="SourceSansPro-Semibold"/>
                                        <On Platform="Android" Value="Fonts/SourceSansPro-Semibold.ttf#SourceSansPro-Semibold"/>
                                    </OnPlatform>
                                </Label.FontFamily>
                            </Label>
                            <Label LineBreakMode="WordWrap" TextColor="#828282" FontSize="14" Text="{Binding Text}">
                                <Label.FontFamily>
                                    <OnPlatform x:TypeArguments="x:String">
                                        <On Platform="iOS" Value="SourceSansPro-Regular"/>
                                        <On Platform="Android" Value="Fonts/SourceSansPro-Regular.ttf#SourceSansPro-Regular"/>
                                    </OnPlatform>
                                </Label.FontFamily>
                            </Label>
                        </StackLayout>
                    </Frame>
                    <StackLayout Grid.Row="0" Grid.Column="2" Orientation="Vertical" Spacing="3" WidthRequest="40" VerticalOptions="CenterAndExpand" HorizontalOptions="EndAndExpand">
                        <Label TextColor="#828282" FontSize="14" Text="{Binding Time}" HorizontalOptions="EndAndExpand" HorizontalTextAlignment="End">
                            <Label.FontFamily>
                                <OnPlatform x:TypeArguments="x:String">
                                    <On Platform="iOS" Value="SouceSansPro-Regular"/>
                                    <On Platform="Android" Value="Fonts/SourceSansPro-Regular.ttf#SourceSansPro-Regular"/>
                                </OnPlatform>
                            </Label.FontFamily>
                        </Label>
                    </StackLayout>
                </Grid>
            </local:CustomViewCell>
        </DataTemplate>

感谢任何帮助,因为我真的不想坚持使用“ RetainElement”。

PS:我在Android上进行了测试

谢谢:)

0 个答案:

没有答案