【问题标题】:Xamarin Forms ListView, When TranslateTo Animation Hide By other ItemsXamarin Forms ListView,当TranslateTo动画被其他项目隐藏时
【发布时间】:2020-09-04 08:17:10
【问题描述】:

我打算在 Listview 项目出现时编码“项目出现”项目从 topbottom 出现。 (使用 TranslateTo)

但是当 Listview Viewcell Appearing 或 itemsAppearing 出现时,我尝试应用动画。 但是,较高的项目索引隐藏了较低的项目动画索引。 因此,较低索引项的动画仅在短期内显示。 (被其他布局裁剪)

Please Check Image Description _ Animation Cropped by items

在 Xamarin 表单中(C#)

    private async void ItemsListView_ItemAppearing_1(object sender, ItemVisibilityEventArgs e)
    {
    
        int i = e.ItemIndex;
        int y;

        y = 400;
        for (int d = 0; d < i; d++)
        {
            int z = d + 1;
            y = y + (400 / (z * z));
        }

        uint x = uint.Parse(y.ToString());

        IEnumerable<PropertyInfo> pInfos = (ItemsListView as ItemsView<Cell>).GetType().GetRuntimeProperties();
        var templatedItems = pInfos.FirstOrDefault(info => info.Name == "TemplatedItems");
        if (templatedItems != null)
        {
            var cells = templatedItems.GetValue(ItemsListView);
            var cell = (cells as Xamarin.Forms.ITemplatedItemsList<Xamarin.Forms.Cell>)[e.ItemIndex] as ViewCell;

          //cell.View.Opacity = 0;
            cell.View.TranslationY = Var.Device_Height / Var.Device_Scale;
          await  cell.View.TranslateTo(0, 0, x, Easing.CubicOut);


        }
    }

在 XAML 中

     <ListView x:Name="ItemsListView" BackgroundColor="Transparent" VerticalOptions="Fill"         ItemsSource="{Binding Items}"  Margin="0" SeparatorVisibility="None" RefreshCommand="{Binding LoadItemsCommand}" 
                              IsPullToRefreshEnabled="True" HasUnevenRows="True"  IsRefreshing="{Binding IsBusy, Mode=OneWay}" ItemAppearing="ItemsListView_ItemAppearing_1"
                             >
                        <ListView.ItemTemplate>
                            <DataTemplate>
                                <ViewCell Appearing="ViewCell_Appearing">       
                                    <ViewCell.View>

                                        <Frame IsClippedToBounds="False" 
                                               Margin="0,5" 
                                               CornerRadius="10" 
                                               Padding="0" 
                                               HasShadow="False" 
                                               BorderColor="#f0f0f0" 
                                               BackgroundColor="Transparent" >
 
                                    <StackLayout Padding="0" Spacing="0" MinimumHeightRequest="0">
                                                <StackLayout Orientation="Horizontal">
                                                    <Label Text="{Binding Name}" Margin="13,12,5,0" FontAttributes="Bold" FontSize="18" TextColor="#222222">
                                                       
                                                    </Label>
                                                    <Frame MinimumHeightRequest="0" VerticalOptions="EndAndExpand" Padding="7,2,7,2" CornerRadius="100" BackgroundColor="Accent" HasShadow="False">
                                                     
                                                        <Label Text="{Binding Sequence}" TextColor="White" FontSize="Micro"/>
                                                    </Frame>
                                                </StackLayout>
                                                <Label Text="{Binding Mail}" Margin="13,6,0,12">                        
                                                </Label>
                                            </StackLayout>
                                    </Frame>
                                                                        </ViewCell.View>
                                </ViewCell>
                            </DataTemplate>
                        </ListView.ItemTemplate>
</ListView>

ViewCell_appearingitems_Appearing

时,结果相同

【问题讨论】:

  • 我放慢了你的动画,这里正在运行 GIF。 imgur.com/a/KFQIZ6Y,我在ViewCell_Appearing中设置了你的代码,高索引项不隐藏低索引项动画
  • 我不知道您为Var.Device_Height / Var.Device_Scale; 设置的值是多少所以,我设置了测试值。 public static double Device_Height = DeviceDisplay.MainDisplayInfo.Height;public static double Device_Scale = 6;

标签: c# android xaml xamarin


【解决方案1】:

这是我的 TranslateTo Animation Hide By other Items 的背景代码。

 public static double Device_Height = DeviceDisplay.MainDisplayInfo.Height;

        public static double Device_Scale = 6;
        int i;
        private async void mylistview_ItemAppearing(object sender, ItemVisibilityEventArgs e)
        {
             i = e.ItemIndex;
           
        }

        private async void ViewCell_Appearing(object sender, EventArgs e)
        {
          //  int i = e.ItemIndex;
            int y;

            y = 4000;
            for (int d = 0; d < i; d++)
            {
                int z = d + 1;
                y = y + (400 / (z * z));
            }

            uint x = uint.Parse(y.ToString());
            var cell = sender as ViewCell;
            cell.View.TranslationY = Device_Height / Device_Scale;
            await cell.View.TranslateTo(0, 0, x, Easing.CubicOut);
        }

这里正在运行 GIF(我放慢了你的动画速度)。

我把我的demo上传给你,你可以测试一下。

https://github.com/851265601/Xamarin.Android_ListviewSelect/blob/master/MyCusListview.zip

【讨论】:

  • 如果以上回答对您有帮助,请不要忘记采纳为回答(点击此回答左上角的“✔”),对有类似问题的其他人会有所帮助
  • 真诚地感谢您的回答。因为放假,刚查了一下。
  • 我测试了你的代码,示例和我的代码一样。
  • 我希望动画 (TranslationY) 不会被其他布局项目剪辑。如果 ScrollDown,项目出现在顶部。并且向上滚动,从向下出现,用户可以从布局的(Listview)底部或顶部看到项目。但演示只是从它的单元格中出现
  • 因为我们将动画添加到了viewcell,所以item从它的cell中出现,所以你想从顶部制作消失的item的一部分。这种方式是达不到的。
【解决方案2】:

回复@Leon Lu:是的,有办法。 您可以检查整数 i == 0。

public static double Device_Height = DeviceDisplay.MainDisplayInfo.Height;

    public static double Device_Scale = 6;
    int i;
    private async void mylistview_ItemAppearing(object sender, ItemVisibilityEventArgs e)
    {
         i = e.ItemIndex;
    }

    private async void ViewCell_Appearing(object sender, EventArgs e)
    {
        if(i == 0)
        {
            int y;
            y = 4000;
            for (int d = 0; d < i; d++)
            {
                int z = d + 1;
                y = y + (400 / (z * z));
            }

            uint x = uint.Parse(y.ToString());
            var cell = sender as ViewCell;
            cell.View.TranslationY = Device_Height / Device_Scale;
            await cell.View.TranslateTo(0, 0, x, Easing.CubicOut);
        }
    }

但不幸的是,现在它只为第二行设置动画。但我正在寻找解决方案! 我认为,就我而言,它与通过 Binding 的 ListView 人口有关..

【讨论】:

    猜你喜欢
    • 2016-12-29
    • 1970-01-01
    • 2017-01-30
    • 1970-01-01
    • 2014-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多