我有一个列表视图,其中包含三个不同的单元格内容,其中只有一个通过绑定检查在该单元格上显示。我没有设置视图单元格的高度,但是将其堆栈布局子项的垂直选项设置为“填充”#39;。现在,这适用于android,但神秘不在iOs上。我已经将hasuneven行设置为true但是在点击每个单元格时,我注意到高度是相同的。我将展示xaml代码和项目源代码:
<ListView
x:Name = "lvConversations"
VerticalOptions = "FillAndExpand"
HorizontalOptions = "FillAndExpand"
IsGroupingEnabled = "false"
SeparatorVisibility = "None"
HasUnevenRows = "true"
ItemSelected = "messageSelected"
BackgroundColor = "#F8F8F8"
>
<ListView.ItemTemplate>
<DataTemplate>
<local:ScaledViewCell>
<local:ScaledStackLayout
VerticalOptions = "Fill"
HorizontalOptions = "FillAndExpand"
Orientation = "Vertical"
Spacing = "0">
<local:ScaledStackLayout
VerticalOptions = "Fill"
HorizontalOptions = "FillAndExpand"
Orientation = "Vertical"
Spacing = "0"
IsVisible = "{Binding isGroupHeader, Converter = {StaticResource inverter}}"
BackgroundColor = "Transparent">
<!-- OUTGOING -->
<local:ScaledStackLayout
HorizontalOptions = "FillAndExpand" Orientation = "Horizontal"
IsVisible = "{Binding isOutgoing}">
<local:ScaledStackLayout
VerticalOptions = "Fill"
HorizontalOptions = "EndAndExpand"
BackgroundColor = "Transparent"
Orientation = "Vertical"
Margin = "21,14,15,0"
Spacing = "0">
<local:ScaledStackLayout
Orientation = "Horizontal"
VerticalOptions = "Fill"
HorizontalOptions = "EndAndExpand"
Spacing = "0"
>
<local:CorneredView
CornerRadius = "15"
BackgroundColor = "#d3eaf6"
VerticalOptions = "Fill"
HorizontalOptions = "End"
>
<Label
VerticalOptions = "Fill"
HorizontalOptions = "Fill"
Text = "{Binding message}"
FontSize = "10"
TextColor = "Black"
Margin = "7,10,7,10"
LineBreakMode = "WordWrap"
/>
</local:CorneredView>
<Image
Margin = "0,11,7,0"
Source = "ic_msg_arrow_outgoing"
WidthRequest = "5"
HeightRequest = "10"
Aspect = "AspectFit"
BackgroundColor = "Transparent"
VerticalOptions = "Start"
/>
<ffimageloading:CachedImage
WidthRequest = "37"
HeightRequest = "37"
BackgroundColor = "Transparent"
VerticalOptions = "Start"
Aspect = "AspectFill"
Source = "{Binding sender.imageUrl}"
DownsampleToViewSize = "true"
DownsampleUseDipUnits = "true"
>
<ffimageloading:CachedImage.Transformations>
<fftransformations:CircleTransformation>
<x:Arguments>
<x:Double>2</x:Double>
<x:String>#FF8A07</x:String>
</x:Arguments>
</fftransformations:CircleTransformation>
</ffimageloading:CachedImage.Transformations>
</ffimageloading:CachedImage>
</local:ScaledStackLayout>
<Label
Margin = "0,3,0,0"
VerticalOptions = "Center"
HorizontalOptions = "Start"
Text = "{Binding formattedDateSent}"
HeightRequest = "10"
FontSize = "8"
TextColor = "#4D212121"/>
</local:ScaledStackLayout>
</local:ScaledStackLayout>
<!--INCOMING-->
<local:ScaledStackLayout
WidthRequest = "284"
VerticalOptions = "Fill"
BackgroundColor = "Transparent"
Orientation = "Vertical"
Margin = "15,14,21,0"
Spacing = "0"
IsVisible = "{Binding isOutgoing, Converter = {StaticResource inverter}}">
<local:ScaledStackLayout
Orientation = "Horizontal"
WidthRequest = "284"
VerticalOptions = "Fill"
Spacing = "0"
>
<ffimageloading:CachedImage
WidthRequest = "37"
HeightRequest = "37"
BackgroundColor = "Transparent"
VerticalOptions = "Start"
Aspect = "AspectFill"
Source = "{Binding sender.imageUrl}"
DownsampleToViewSize = "true"
DownsampleUseDipUnits = "true"
>
<ffimageloading:CachedImage.Transformations>
<fftransformations:CircleTransformation>
<x:Arguments>
<x:Double>2</x:Double>
<x:String>#FF8A07</x:String>
</x:Arguments>
</fftransformations:CircleTransformation>
</ffimageloading:CachedImage.Transformations>
</ffimageloading:CachedImage>
<Image
Margin = "7,15,0,0"
Source = "ic_msg_arrow_incoming"
WidthRequest = "5"
HeightRequest = "10"
Aspect = "AspectFit"
BackgroundColor = "Transparent"
VerticalOptions = "Start"
/>
<local:CorneredView
WidthRequest = "235"
VerticalOptions = "Fill"
CornerRadius = "15"
BackgroundColor = "#ECECEC"
IsClippedToBounds = "true"
>
<Label
VerticalOptions = "Fill"
HorizontalOptions = "FillAndExpand"
Text = "{Binding message}"
FontSize = "10"
TextColor = "Black"
Margin = "7,10,7,10"
LineBreakMode = "WordWrap"
/>
</local:CorneredView>
</local:ScaledStackLayout>
<Label
Margin = "0,3,0,0"
VerticalOptions = "Center"
HorizontalOptions = "End"
Text = "{Binding formattedDateSent}"
HeightRequest = "10"
FontSize = "8"
TextColor = "#4D212121"/>
</local:ScaledStackLayout>
</local:ScaledStackLayout>
<!--Message Group-->
<local:ScaledStackLayout
Margin = "0"
Padding = "0"
Spacing = "0"
Orientation = "Vertical"
VerticalOptions = "Fill"
IsVisible = "{Binding isGroupHeader}"
BackgroundColor = "Transparent"
>
<!--label and button-->
<local:ScaledStackLayout
Margin = "9,0,9,0"
HeightRequest = "19"
Orientation = "Horizontal"
Padding = "0,5,7,0"
BackgroundColor = "Transparent">
<Label
Text = "{Binding groupHeaderTitle}"
HorizontalOptions = "FillAndExpand"
HeightRequest = "8"
FontSize = "8"
TextColor = "#212121"
VerticalTextAlignment = "Center"
HorizontalTextAlignment = "Center"
BackgroundColor = "Transparent"/>
</local:ScaledStackLayout>
<BoxView
Margin = "9,0,9,0"
HorizontalOptions = "FillAndExpand"
HeightRequest = "1"
BackgroundColor = "#4D212121"/>
</local:ScaledStackLayout>
</local:ScaledStackLayout>
</local:ScaledViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
如您所见,细胞重叠。
我用来填充列表视图的代码是:
public void OnSuccess(JObject response, BaseAPI caller)
{
if (Constants.isDebug)
{
JObject json = TestData.getTestData("conversation_messages_listed.json");
response = json;
}
ObservableCollection<Message> arrMessages = JsonConvert.DeserializeObject<ObservableCollection<Message>>(response["messages"].ToString(), new JsonSerializerSettings()
{
NullValueHandling = NullValueHandling.Ignore,
MissingMemberHandling = MissingMemberHandling.Ignore
});
lvConversations.ItemsSource = arrMessages;
}
请帮忙,我是xamarin的新手。
答案 0 :(得分:3)
HasUnevenRows默认在iOS上无法正常工作。见comment on a bug report:
iOS不支持自动调整行单元格大小,您必须设置ListView.RowHeight或Cell.Height。不幸的是,我们没有特别禁止在Android或Windows Phone上工作(在没有我们干预的情况下它可以正常工作),并且让一些人相信iOS在这方面已经被打破。
不幸的是,这是iOS平台的限制,因此是预期的行为。
查看docs for HasUnevenRows property可能也会有所帮助。
当app开发人员将ListView.HasUnevenRows属性设置为true时,列表视图的行为仍取决于ListView.RowHeight属性。首先,如果开发人员未设置ListView.RowHeight属性或将其设置为-1,则会自动调整列表视图项以适合其内容。如上所述,这是ListView.HasUnevenRows值为true的预期行为和预期用例。其次,如果开发人员将ListView.RowHeight属性设置为0或设置为-1以外的负值,则ListView中的所有行(无论其内容的高度如何)都将具有系统的默认高度。第三,最后,如果开发人员将ListView.RowHeight设置为正值,那么ListView中的所有行都将与ListView.RowHeight一样高,无论其内容的高度如何,就像ListView.HasUnevenRows已设置为假的。
答案 1 :(得分:-1)
点击后,您可以致电ForceUpdateSize()
,此问题已解决:
var clicked = (StackLayout)sender;
((ViewCell)((Grid)((StackLayout)((PancakeView)((StackLayout)clicked.Parent).Parent).Parent).Parent).Parent).ForceUpdateSize();