使用TranslateTo动画在ListView中快速滚动会导致空白行

时间:2018-10-19 08:01:09

标签: xaml listview xamarin.forms

目前,我在跨平台Xamarin Forms应用程序中具有带有行项的listView。列表视图绑定到具有IsAnimated变量的ObservableCollection,该变量指示应打开还是关闭抽屉(即,视图是向左还是向右转换)。

为了在滚动时在正确的位置打开或关闭它,我创建了一个自定义ViewCell,用于检查何时显示ViewCell。出现时,它会检查IsAnimated变量,然后相应地打开或关闭抽屉。

现在,当我进行负面测试并快速上下滚动时,它会隐藏我的视图,并在列表中留下空白。

如何防止这些空白取代行项目?

我的Viewcell适配器:

class DirectoryListViewCell : ViewCell
{
    protected override void OnAppearing()
    {
        var view = this.View.Parent;
        var binding = (this.BindingContext as DirectoryModel);

        var meh = (view as ViewCell).View;

        var abs = (meh as AbsoluteLayout).Children.FirstOrDefault();


        var width = Application.Current.MainPage.Width;
        var trans = (((-1 * width) / 10) * 4);

        switch (binding.IsAnimated)
        {
            case false:
                abs.TranslateTo(0, 0, 0, Easing.Linear);
                break;
            case true:
                abs.TranslateTo(200, 0, 0, Easing.Linear);
                break;
        }


        base.OnAppearing();
    }

}

我的Xaml listview数据模板看起来像这样

<custom:DirectoryListViewCell>

                                <AbsoluteLayout HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" HeightRequest="80">
                                    <AbsoluteLayout AbsoluteLayout.LayoutBounds="0, 0, 1, 1" AbsoluteLayout.LayoutFlags="All" BackgroundColor="White">


                                        <Image AbsoluteLayout.LayoutBounds="0, 0, 0.2, 1" AbsoluteLayout.LayoutFlags="All"
                                            Source="contact_image.png" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" Margin="10"/>


                                        <StackLayout AbsoluteLayout.LayoutBounds="0.4, 0, 0.5, 0.25" AbsoluteLayout.LayoutFlags="All" Orientation="Horizontal">
                                            <Label Text="{Binding DisplayName, Converter={StaticResource NullToTitleConverter}}" LineBreakMode="CharacterWrap" HorizontalTextAlignment="Start" FontSize="Small" VerticalTextAlignment="Center"></Label>
                                            <Label Text="{Binding DisplayRecommendedTitle}" LineBreakMode="CharacterWrap" HorizontalTextAlignment="Start" FontSize="Micro" TextColor="Gray" VerticalTextAlignment="Center"></Label>
                                        </StackLayout>
                                        <Label Text="{Binding Number}"
                                               AbsoluteLayout.LayoutBounds="0.4, 0.5, 0.5, 0.4" AbsoluteLayout.LayoutFlags="All"  VerticalTextAlignment="Center"></Label>
                                        <Label Text="{Binding CallCount, Converter={StaticResource CallCountConverter}}" FontSize="Micro"
                                               AbsoluteLayout.LayoutBounds="0.4, 0.941, 0.5, 0.15" AbsoluteLayout.LayoutFlags="All" VerticalTextAlignment="Center"></Label>


                                        <Image AbsoluteLayout.LayoutBounds="0.824, 0, 0.15, 1" AbsoluteLayout.LayoutFlags="All"
                                            Source="edit_contact.png" VerticalOptions="Center" HorizontalOptions="CenterAndExpand" Margin="10"></Image>

                                        <Button x:Name="btnExpand" Image="expand_options.png" AbsoluteLayout.LayoutBounds="1, 0, 0.15, 1" AbsoluteLayout.LayoutFlags="All" BackgroundColor="Transparent">
                                            <Button.Triggers>
                                                <EventTrigger Event="Clicked">
                                                    <triggers:ClickTranslation/>
                                                </EventTrigger>
                                            </Button.Triggers>
                                        </Button>

                                    </AbsoluteLayout>


                                    <StackLayout AbsoluteLayout.LayoutBounds="0.75, 0, 0.2, 1" AbsoluteLayout.LayoutFlags="All" BackgroundColor="#00bcae" Padding="25"  IsVisible="{Binding IsAnimated}">
                                        <Image AbsoluteLayout.LayoutBounds="0.8,0.5,1,1" AbsoluteLayout.LayoutFlags="All" BackgroundColor="#00bcae"
                                            Source="business_white.png" VerticalOptions="CenterAndExpand" HorizontalOptions="CenterAndExpand"></Image>
                                    </StackLayout>
                                    <StackLayout AbsoluteLayout.LayoutBounds="1, 0, 0.2, 1" AbsoluteLayout.LayoutFlags="All" BackgroundColor="Gray" Padding="25"  IsVisible="{Binding IsAnimated}">
                                        <Image AbsoluteLayout.LayoutBounds="1,0.5,1,1" AbsoluteLayout.LayoutFlags="All" BackgroundColor="Gray"
                                            Source="personal_white.png" VerticalOptions="CenterAndExpand" HorizontalOptions="CenterAndExpand"></Image>
                                    </StackLayout>

                                </AbsoluteLayout>

                            </custom:DirectoryListViewCell>

0 个答案:

没有答案