首页 > 解决方案 > 带有 HasUnevenRows 的 Listview 不会自动更改单元格高度?

问题描述

带有 HasUnevenRows 的 Listview 不会自动更改单元格高度,我的 listview 包含图像和标签,listview 行高不会随其内容而变化。我想用标签内容更改行高。

这是我的设计

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

    <Image Source="waterMark" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand" 
           AbsoluteLayout.LayoutBounds="0,1,1,1" AbsoluteLayout.LayoutFlags="All" Margin="0,0,0,40"/>

    <ListView x:Name="lv_List"  IsPullToRefreshEnabled="True" 
        HasUnevenRows="True" BackgroundColor="Transparent" SeparatorVisibility="None"
        VerticalOptions="FillAndExpand"
        HorizontalOptions="FillAndExpand" 
        AbsoluteLayout.LayoutBounds="0,0,1,1" AbsoluteLayout.LayoutFlags="All">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <AbsoluteLayout HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" 
                               Padding="0" Margin="0" BackgroundColor="WhiteSmoke">

                        <ci:CircleImage Source="profile"  BorderColor="WhiteSmoke" AbsoluteLayout.LayoutBounds="0,0,0.3,1"
                                 AbsoluteLayout.LayoutFlags="All" WidthRequest="65" HeightRequest="65"
                                 HorizontalOptions="StartAndExpand"
                                 VerticalOptions="CenterAndExpand" Aspect="AspectFill">
                        </ci:CircleImage>

                        <StackLayout Orientation="Vertical" HorizontalOptions="StartAndExpand" VerticalOptions="CenterAndExpand"
                                 AbsoluteLayout.LayoutBounds="1,0,0.75,1" AbsoluteLayout.LayoutFlags="All">
                            <StackLayout.Margin>
                                <OnIdiom Phone="0,0,0,0"
                                             Tablet="-90,0,0,0"/>
                            </StackLayout.Margin>
                            <Label Text="{Binding Name}" HorizontalOptions="FillAndExpand" 
                              FontSize="17.5" TextColor="#555454"/>

                            <Label Text="{Binding Description}" HorizontalOptions="FillAndExpand" 
                                      FontSize="20" TextColor="#555454"/>

                        </StackLayout>

                    </AbsoluteLayout>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</AbsoluteLayout>

标签: c#listviewxamarin.forms

解决方案


您可以Grid在 listview 的 DataTemplate 中使用来实现这一点。有我的运行截图。

在此处输入图像描述

有布局。

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         xmlns:local="clr-namespace:App4"
         xmlns:ci="clr-namespace:ImageCircle.Forms.Plugin.Abstractions;assembly=ImageCircle.Forms.Plugin"
         x:Class="App4.MainPage">

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



    <ListView x:Name="lv_List"  IsPullToRefreshEnabled="True" 
    HasUnevenRows="True" BackgroundColor="Transparent" SeparatorVisibility="Default"
    VerticalOptions="FillAndExpand"
    HorizontalOptions="FillAndExpand" 
    AbsoluteLayout.LayoutBounds="0,0,1,1" AbsoluteLayout.LayoutFlags="All">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>

                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="*" />
                            <RowDefinition Height="*" />
                        </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="3*" />
                        </Grid.ColumnDefinitions>
                        <ci:CircleImage Source="profile"  BorderColor="WhiteSmoke"  Grid.Row="0" Grid.Column="0"  Grid.RowSpan="2"
                             AbsoluteLayout.LayoutFlags="All" WidthRequest="65" HeightRequest="65"
                             HorizontalOptions="StartAndExpand"
                             VerticalOptions="CenterAndExpand" Aspect="AspectFill">
                        </ci:CircleImage>
                        <Label Text="{Binding Name}" HorizontalOptions="FillAndExpand"  Grid.Row="0" Grid.Column="1"
                          FontSize="17.5" TextColor="#555454"/>

                        <Label Text="{Binding Description}" HorizontalOptions="FillAndExpand"  Grid.Row="1" Grid.Column="1"
                                  FontSize="20" TextColor="#555454"/>

                    </Grid>

                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</AbsoluteLayout>

有listview的数据源。

    public partial class MainPage : ContentPage
{
    ObservableCollection<Employee> employees = new ObservableCollection<Employee>();
    public MainPage()
    {
        InitializeComponent();

        employees.Add(new Employee { Name = "Rob Finnerty", Description = "Rob FinnertyRob FinnertyRob FinnertyRob FinnertyRob FinnertyRob FinnertyRob FinnertyRob FinnertyRob Finnerty" });
        employees.Add(new Employee { Name = "Bill Wrestler", Description = "Bill WrestlerBill Wrestler" });
        employees.Add(new Employee { Name = "Dr. Geri-Beth Hooper", Description = "Dr. Geri-Beth Hooper" });
        employees.Add(new Employee { Name = "Dr. Keith Joyce-Purdy", Description = "Dr. Keith Joyce-PurdyDr. Keith Joyce-PurdyDr. Keith Joyce-Purdy" });
        employees.Add(new Employee { Name = "Sheri Spruce", Description = "Sheri SpruceSheri Spruce" });
        employees.Add(new Employee { Name = "Burt Indybrick", Description = "Burt IndybrickBurt Indybrick" });
        lv_List.ItemsSource = employees;
    }
}

有型号。

    public class Employee
{
    public string Name { get; set; }
    public string Description { get; set; }
}

推荐阅读