【问题标题】:Xamarin.Forms ListView. Can I know the height of each row?Xamarin.Forms 列表视图。我可以知道每行的高度吗?
【发布时间】:2019-07-09 18:35:52
【问题描述】:

我有一个 HasUnevenRows = true 的 ListView。

我将 ItemSource 绑定到 ObservableCollection。

这个集合用一些行填充 ListViews。每一行都有不同的高度,因为每一行的文本都不同。

我想知道我是否可以知道每行的高度。

谢谢 亚历山德罗

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

                    if (cell.BindingContext != null)
                    {

                        System.Diagnostics.Debug.WriteLine("CellHeight = " + cell.Height + " - " + cell.RenderHeight + " " + cell.View.Height + " - " + cell.View.HeightRequest + " - " + cell.View.MinimumHeightRequest + " - " );
                        Grid grid = (Grid)cell.View;
                        System.Diagnostics.Debug.WriteLine("Height = " +  grid.Height.ToString() + " - " + grid.HeightRequest + " - " + grid.MinimumHeightRequest);


                    }
                }
            }

我尝试过使用这样的代码,但我在每个属性中总是“-1”

【问题讨论】:

    标签: listview xamarin.forms


    【解决方案1】:

    好的,我找到了这个解决方案。

                <ViewCell>
                    <Grid
                        SizeChanged="ViewCell_SizeChanged"
    

    这是 ViewCell_SizeChanged:

        double totalHeight = 0;
        int counter = 0;
    
        private void ViewCell_SizeChanged(object sender, EventArgs e)
        {
    
            if(sender is Grid)
            {
                Grid grid = (Grid)sender;
    
                totalHeight += grid.Height;
                totalHeight += grid.Margin.Top;
                totalHeight += grid.Margin.Bottom;
    
                if(++counter == ((MyViewModel)this.BindingContext).MyObservableCollection.Count)
                    Device.BeginInvokeOnMainThread(() => MyListView.HeightRequest = totalHeight);
            }
        }
    

    当 ViewCell 被数据填充时,Grid 会调整大小,因此会引发 ViewCell_SizeChanged 事件。我有当前的 Grid.Height 和 Margin valorized。当我达到项目总数时,我有 ListView 中所有网格的确切高度

    【讨论】:

      【解决方案2】:

      我想根据 Alessandro 的回答分享我的解决方案,但使用的是 FlowListView。我需要设置FlowListView HeightRequest,因为列表后面有一个巨大的空白。

      XAML

      <flv:FlowListView
           FlowColumnCount="1"
           FlowItemsSource="{Binding Coverages}"
           SeparatorVisibility="None"
           VerticalScrollBarVisibility="Never"
           x:Name="listConverages"
           HasUnevenRows="True">
           <flv:FlowListView.FlowColumnTemplate>
               <DataTemplate>
                   <StackLayout
                        SizeChanged="StackLayout_SizeChanged">
                   </StackLayout>
               </DataTemplate>
           </flv:FlowListView.FlowColumnTemplate>
      </flv:FlowListView>
      

      后面的代码

      private Dictionary<Guid, double> itemsHeight = new Dictionary<Guid, double>();
      private void StackLayout_SizeChanged(object sender, EventArgs e)
      {
          StackLayout stackLayout = (StackLayout)sender;
      
          if (!itemsHeight.ContainsKey(stackLayout.Id))
              itemsHeight.Add(stackLayout.Id, stackLayout.Height);
          else
              itemsHeight[stackLayout.Id] = stackLayout.Height;
      
          listConverages.HeightRequest = itemsHeight.Sum(x => x.Value);
      }
      

      注意:如果您有底部或顶部边距,请在将项目添加到字典之前将它们添加到值中

      【讨论】:

        猜你喜欢
        • 2019-07-20
        • 2015-05-22
        • 1970-01-01
        • 2011-10-10
        • 1970-01-01
        • 2022-11-18
        • 2016-05-15
        • 2016-12-31
        • 1970-01-01
        相关资源
        最近更新 更多