title: Xamarin Forms dynamic list view
body: <p>I am trying to create a list in xamarin.forms that is a bit complex. The user should be able to click on an item in the list and it should expand into something somewhat bigger. The bigger item should now display some additional UI components that are associated specifically with this view.</p>
  
  <p>I wonder how I should approach this problem. It's about a listview that has items of dynamic size. Upon click the item will display additional views related to the item. Should this be done in Xamarin.ios and Xamarin.droid, or is it recommended to try and achieve this in xamarin.forms?</p>
  
  <p>I'll post a picture, it is not good and might need some magnification but it shows 4 items. The 3rd one is expanded and therefore you can see the spinner and button on it.</p>
  
  <p>Only one item can be expanded at a time(I might have to handle that in the ViewModel) and upon pressing another item the old one should be hidden.</p>
  
  <p><a href="https://i.stack.imgur.com/JYxP6.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/JYxP6.jpg" alt="The list"></a></p>
  
  <p>Edit:</p>
  
  <p>Thanks to Rohit I started implementing a solution in Xamarin.Forms but it doesn't really work still, just some small problems in how the row is expanded. See picture below. I'm skipping the spinner and just using a button for simplicity. The expanded row overlaps the row below itself. First picture is before click, second is after clicking on the item called "Two".</p>
  
  <p>View</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="simpletest.PlayGroundPage"&gt;
      &lt;ContentPage.Content&gt;
          &lt;StackLayout&gt;
              &lt;ListView VerticalOptions="FillAndExpand" HasUnevenRows="True" ItemsSource="{Binding AllItems}" SelectedItem="{Binding MySelectedItem}"&gt;
              &lt;ListView.ItemTemplate&gt;
                  &lt;DataTemplate&gt;
                      &lt;ViewCell&gt;
                          &lt;StackLayout VerticalOptions="FillAndExpand"&gt;
                              &lt;StackLayout VerticalOptions="FillAndExpand" Orientation="Horizontal"&gt;
                                  &lt;Label Text="{Binding MyText}" /&gt;
                                  &lt;Image Source="{Binding MyImage}" /&gt;
                              &lt;/StackLayout&gt;
                              &lt;Button Text="button1" IsVisible="{Binding IsExtraControlsVisible}" /&gt;  
                          &lt;/StackLayout&gt;
                      &lt;/ViewCell&gt;
                  &lt;/DataTemplate&gt;
                  &lt;/ListView.ItemTemplate&gt;
              &lt;/ListView&gt;
          &lt;/StackLayout&gt;
      &lt;/ContentPage.Content&gt;
  &lt;/ContentPage&gt;
  </code></pre>
  
  <p>Item</p>
  
  <pre><code>    public class Item : INotifyPropertyChanged
      {
  
  
          public Item(string text, string image, int id)
          {
              _myText = text;
              _myImage = image;
              _id = id;
              _isExtraControlsVisible = false;
  
          }
  
          private string _myText;
          private string _myImage;
          private bool _isExtraControlsVisible;
          private int _id;
  
          public event PropertyChangedEventHandler PropertyChanged;
  
          public int Id { get { return _id; } set { _id = value; } }
  
          public string MyText
          {
              get { return _myText; }
              set { _myText = value; OnPropertyChanged("MyText"); }
          }
          public string MyImage
          {
              get { return _myImage; }
              set { _myImage = value; OnPropertyChanged("MyImage"); }
          }
          public bool IsExtraControlsVisible
          {
              get { return _isExtraControlsVisible; }
              set { _isExtraControlsVisible = value; OnPropertyChanged("IsExtraControlsVisible"); }
          }
  
          private void OnPropertyChanged(string property)
          {
              if (PropertyChanged != null)
              {
                  PropertyChanged(this, new PropertyChangedEventArgs(property));
              }
          }
      }
  </code></pre>
  
  <p>ViewModel:</p>
  
  <pre><code>    class PlayGroundViewModel : INotifyPropertyChanged
      {
          private Item _mySelectedItem;
  
  
  
          public PlayGroundViewModel(ObservableCollection&lt;Item&gt; allItems)
          {
              AllItems = allItems;
              _mySelectedItem = allItems.First();
          }
          public ObservableCollection&lt;Item&gt; AllItems { get; set; }
  
          public Item MySelectedItem
          {
              get { return _mySelectedItem; } //Added a field for this one, mainly for debugging.
              set
              {
  
                  foreach (Item x in AllItems) //Changed to non-linq since it is not a list.
                  {
                      x.IsExtraControlsVisible = false;
                  }
  
                  if (value != null)
                  {
                      foreach (Item x in AllItems)
                      {
                          if (x.Id.Equals(value.Id))
                          {
                              x.IsExtraControlsVisible = true;
                              _mySelectedItem = x;
                          }
                      }
                  }
  
                  SetChangedProperty("MySelectedItem");
              }
          }
  
          public event PropertyChangedEventHandler PropertyChanged;
          private void SetChangedProperty(string property)
          {
              if (PropertyChanged != null)
              {
                  PropertyChanged(this, new PropertyChangedEventArgs(property));
              }
          }
  
      }
  </code></pre>
  
  <p>CodeBehind:</p>
  
  <pre><code>    public partial class PlayGroundPage : ContentPage
      {
          public PlayGroundPage()
          {
              InitializeComponent();
              ObservableCollection&lt;Item&gt; items = new ObservableCollection&lt;Item&gt;();
              items.Add(new Item("One", "", 0));
              items.Add(new Item("Two", "", 1));
              items.Add(new Item("Three", "", 2));
              PlayGroundViewModel weekViewModel = new PlayGroundViewModel(items);
              BindingContext = weekViewModel;
          }
      }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/454wu.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/454wu.png" alt="Before clicking on a row"></a></p>
  
  <p><a href="https://i.stack.imgur.com/ZwxKx.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ZwxKx.png" alt="After having clicked"></a></p>