2016-07-27 43 views
3

我使用StackLayout和ListView来显示视图的某些部分,但ListView需要更多的空间比我需要,并留下一个空格之间的列表的最后一行和配置文件的继续。看来我的ListView具有比实际列表的长度更多的线,或者它有一个固定的高度,我不知道......这是我的XAML:有更多的空间比我在ListView需要

<ScrollView> 
    <StackLayout Orientation="Vertical" Spacing="10"> 

    <Label Text="{Binding Establishment.Name}" TextColor="Black" HorizontalOptions="StartAndExpand" Style="{DynamicResource TitleStyle}" FontAttributes="Bold"/> 

    <Label Text="{Binding Establishment.Category.Name, StringFormat='Categoria: {0}'}" TextColor="Black" HorizontalOptions="StartAndExpand" FontAttributes="Bold"/> 

    <Label Text="{Binding Establishment.Description}" TextColor="Black" HorizontalOptions="StartAndExpand" LineBreakMode="WordWrap" XAlign="Start"/> 

    <Label Text="Contatos" TextColor="Black" HorizontalOptions="StartAndExpand" FontAttributes="Bold"/> 

    <ListView ItemsSource="{Binding Establishment.Contacts}" VerticalOptions="Start" HasUnevenRows="True"> 
     <ListView.ItemTemplate> 
     <DataTemplate> 
      <TextCell Text="{Binding Value}" Detail="{Binding StringType}" DetailColor="Gray"/> 
     </DataTemplate> 
     </ListView.ItemTemplate> 
    </ListView> 

    <Label Text="Endereço" TextColor="Black" HorizontalOptions="StartAndExpand" FontAttributes="Bold"/> 

    <Label Text="{Binding Establishment.FullAddress}" TextColor="Black" HorizontalOptions="StartAndExpand" /> 

    <Button Text="Ver no mapa"/> 

    </StackLayout> 
</ScrollView> 

我怎样才能解决呢?我看到有人使用HasUnevenRows="True",但它对我没有用。

+0

你试过在ListView控件设置HeightRequest价值? – Jason

回答

1

谢谢你们,但没有为我工作。我认为我自己的解决方案肯定是不是最好的,但它为我的作品.....

在XAML我留下了一个空StackLayout这样的:

<StackLayout x:Name="ContactsList" Orientation="Vertical" Spacing="10" Padding="8,0"> 
     <!-- Empty because it will be filled in code mode --> 
    </StackLayout> 

然后,在cs文件,我打电话后InitializeComponent()方法这个方法:

private void setUpContacts(Establishment establishment) 
    { 
     foreach(Contact contact in establishment.Contacts) 
     { 
      StackLayout row = new StackLayout 
      { 
       Orientation = StackOrientation.Vertical 
      }; 

      row.Children.Add(new Label 
      { 
       TextColor = Color.Gray, 
       Text = string.Format("{0}:", contact.StringType) 
      }); 

      row.Children.Add(new Label 
      { 
       TextColor = Color.Black, 
       FontAttributes = FontAttributes.Bold, 
       Text = contact.Value, 
      }); 

      row.GestureRecognizers.Add(new TapGestureRecognizer { 
       Command = new Command(() => OnContactTapped(row, contact)) 
      }); 

      ContactsList.Children.Add(row); 
     } 
    } 

我做了这一点,因为名单是不是一个真正的ListView,因为我并不需要直接在列表滚动功能,它只是另一部分观点。我认为这不会导致任何性能问题,因为它会有少量的项目(最多5个)。我将这个标记为帮助其他人解决这个问题的正确答案。

但请如果这是一个大NOOB的解决方案,没有人能做到,回答我,我会找到另一种方式来做到这一点

+1

如果列表中只有一些东西,我认为你的解决方案是很好,特别是如果它适合你。我在代码的不同位置用'Grid'做了一些类似的事情。感谢您发布您如何解决它。 – hvaughan3

+0

感谢您的反馈! –

2

首先,Xamarin建议不要把ListView置于ScrollView之内,因为他们都提供滚动和互相争斗。我亲自尝试过这一点,它通常会引起怪异的行为,特别是在Android上。

相反,我所做的是使用ListView.HeaderListView.HeaderTemplateListView.FooterListView.FooterTemplate。 Xamarin链接here这允许你的确切场景,但没有奇怪的行为问题。如果你想看到一个例子,请告诉我。

如果必须必须必须与当前的布局走了,你可以尝试看看this后谈论如何ListView需要与特定HeightWidthLayoutOptions的大小,因为它是不应该以适合的尺寸它的内容。在这篇文章中有几个解决方案,我还没有尝试过。

this张贴给出的解决方法之一,说对了ListView添加到StackLayout并添加到另一个StackLayout。然后将内部StackLayoutVerticalOptions设置为LayoutOptions.FillAndExpand

因此,它可能是这样的:

<ScrollView> 
    <StackLayout Orientation="Vertical" Spacing="10"> 

    .... 

    <StackLayout VerticalOptions="FillAndExpand"> 
     <ListView ItemsSource="{Binding Establishment.Contacts}" VerticalOptions="Start" HasUnevenRows="True"> 
     <ListView.ItemTemplate> 
      <DataTemplate> 
      <TextCell Text="{Binding Value}" Detail="{Binding StringType}" DetailColor="Gray"/> 
      </DataTemplate> 
     </ListView.ItemTemplate> 
     </ListView> 
    </StackLayout> 

    .... 

    </StackLayout> 
</ScrollView> 
+0

您如何看待我的解决方案? –

2

由于hvaughan3说,这真的不是一个好的做法。但我有同样的情况,我用的解决方法与行为:

public class ListViewHeightBehavior : Behavior<ListView> 
{ 
    private ListView _listView; 

    public static readonly BindableProperty ExtraSpaceProperty = 
     BindableProperty.Create(nameof(ExtraSpace), 
           typeof(double), 
           typeof(ListViewHeightBehavior), 
           0d); 

    public double ExtraSpace 
    { 
     get { return (double)GetValue(ExtraSpaceProperty); } 
     set { SetValue(ExtraSpaceProperty, value); } 
    } 

    protected override void OnAttachedTo(ListView bindable) 
    { 
     base.OnAttachedTo(bindable); 

     _listView = bindable; 
     _listView.PropertyChanged += (s, args) => 
     { 
      var count = _listView.ItemsSource?.Count(); 
      if (args.PropertyName == nameof(_listView.ItemsSource) 
        && count.HasValue 
        && count.Value > 0) 
      { 
       _listView.HeightRequest = _listView.RowHeight * count.Value + ExtraSpace; 
      } 
     }; 
    } 
} 

XAML:

 <ListView ItemsSource="{Binding Items}" 
       HasUnevenRows="True"> 
     <ListView.Behaviors> 
     <behaviors:ListViewHeightBehavior ExtraSpace="180"/> 
     </ListView.Behaviors> 
     <ListView.ItemTemplate> 
     <DataTemplate> 
      <ViewCell> 
      // Your template 
      </ViewCell> 
     </DataTemplate> 
     </ListView.ItemTemplate> 
    </ListView> 
+0

您对我的解决方案有何看法? –

+0

如果只有5件商品,那么我希望这不是什么大问题。但我认为,我的行为正在我身边工作,你在实施中错过了一些东西。 –

+0

是的可以。当我调试你的解决方案时,它对我很有帮助,但我不知道为什么RowHeight有时以-1为单位:/ –

相关问题