I have a list of details (Basket) and in each of those details, is another list (Fruits). I want to display these details and the very first thing I thought of, was a ListView inside a ListView. But when looking through suggestions, gave me results such as this and this which mostly suggests that it is not a good idea to implement in Xamarin Forms.
At the moment, I'm using FreshMvvM as my MvvM Framework. As for the data that I want to display, I have a set of baskets and each basket has several fruits. I want the images of those fruits to be displayed as well, which belongs to a particular basket. Please refer the image.
I would like to know if there are improvements to this or else, any other ideas of layouts as to how to implement my list or any other way to implement the above behaviour. Thank you.
My code so far:
XAML:
<ListView ItemsSource="{Binding Baskets}" HasUnevenRows="True">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout>
<Label Text="{Binding BasketID}" />
<ImageCell
Text="{Binding FruitID}"
Detail="{Binding FruitName}"
ImageSource="{Binding ImageURL}">
</ImageCell>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Classes:
public class Basket
{
public string BasketID{ get; set; }
public string BasketName{ get; set; }
}
public class Fruit
{
public string FruitID{ get; set; }
public string FruitName{ get; set; }
public string ImageURL{ get; set; }
}
You can use
ListView
to iterate through baskets collection, while using a custom control likeItemsControl
to iterate through fruits collection.It basically is a custom control that allows you to add dynamic children support to a
StackLayout
throughItemsSource
andItemTemplate
properties. You can use the control as it is - except you will need to set theOrientation
property toHorizontal
on its StackLayout at this line.Sample Usage: