Introduction

This article demonstrates how to create an Expandable ListView of items in Xamarin.Forms application. If the user clicks on a product, that product expands and shows items. Then, the user clicks on the same product twice and it will be hidden.
Let's start.
Step 1

Create a new Xamarin.Forms app by going to File >> New -->> Visual C# >> Cross Platform >> Cross Platform App (Xamarin.Native or Xamarin.Forms), give the application name, and press OK.
(Project Name: ExpendableListView)
Step 2

Open Solution Explorer >> ExpendableLIstView (PCL) >> right click and select "New Item".In the popup window, select Cross Platform >>Class.
This way, you can add a new class. Create a new Class named MainListView.cs, double-click to get into its design view, and insert the code given below.
CS code
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Text;
  5. namespace ExpendableListView
  6. {
  7. public class MainListView
  8. {
  9. private Product _oldProduct;
  10. public ObservableCollection<Product> Products { get; set; }
  11. public MainListView()
  12. {
  13. Products = new ObservableCollection<Product>
  14. {
  15. new Product
  16. {
  17. Title = "Microsoft Surface",
  18. Isvisible =false
  19. },
  20. new Product
  21. {
  22. Title = "Microsoft Lumia 535",
  23. Isvisible = false
  24. },
  25. new Product
  26. {
  27. Title = "Microsoft 650",
  28. Isvisible = false
  29. },
  30. new Product
  31. {
  32. Title = "Lenovo Surface",
  33. Isvisible = false
  34. },
  35. new Product
  36. {
  37. Title = "Microsoft edge",
  38. Isvisible = false
  39. }
  40. };
  41. }
  42. public void ShoworHiddenProducts(Product product)
  43. {
  44. if(_oldProduct == product)
  45. {
  46. product.Isvisible = !product.Isvisible;
  47. UpDateProducts(product);
  48. }
  49. else
  50. {
  51. if (_oldProduct != null)
  52. {
  53. _oldProduct.Isvisible = false;
  54. UpDateProducts(_oldProduct);
  55. }
  56. product.Isvisible = true;
  57. UpDateProducts(product);
  58. }
  59. _oldProduct = product;
  60. }
  61. private void UpDateProducts(Product product)
  62. {
  63. var Index = Products.IndexOf(product);
  64. Products.Remove(product);
  65. Products.Insert(Index, product);
  66. }
  67. }
  68. }
Step 3

Similarly, create another class named Product.cs and add the following code.
CS code
  1. namespace ExpandableListView
  2. {
  3. public class Product
  4. {
  5. public string Title{ get; set; }
  6. public bool IsVisible { get; set; }
  7. }
  8. }
Step 4

Afterwards, open MainPage.Xaml page.For that, go to Solution Explorer >> ExpendableListView(PCL) >> MainPage.Xaml and click open MainPage page design view.Here is the code.
Xaml code
  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
  3. xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
  4. xmlns:local="clr-namespace:ExpendableListView"
  5. x:Class="ExpendableListView.MainPage"
  6. Title ="Expendable ListView "
  7. BackgroundColor="Bisque">
  8. <ContentPage.BindingContext>
  9. <local:MainListView/>
  10. </ContentPage.BindingContext>
  11. <ListView Margin="0,80"
  12. ItemTapped="ListViewItem_Tabbed"
  13. ItemsSource="{Binding Products}"
  14. HasUnevenRows="True"
  15. BackgroundColor="Black">
  16. <ListView.ItemTemplate>
  17. <DataTemplate>
  18. <ViewCell>
  19. <StackLayout Padding="20">
  20. <Label Text="{Binding Title}"
  21. FontSize="25"
  22. TextColor="Azure"/>
  23. <StackLayout IsVisible="{Binding Isvisible}"
  24. Orientation="Horizontal"
  25. Margin="0,0,80,0">
  26. <Button Text="Place Order"
  27. WidthRequest="110"
  28. FontSize="15"
  29. BackgroundColor="Chocolate"
  30. TextColor="White"/>
  31. <Button Text="Details"
  32. WidthRequest="110"
  33. FontSize="15"
  34. BackgroundColor="CornflowerBlue"
  35. TextColor="DarkBlue"/>
  36. <Button Text="Edit"
  37. WidthRequest="110"
  38. FontSize="15"
  39. BackgroundColor="LightCoral"
  40. TextColor="Maroon"/>
  41. </StackLayout>
  42. </StackLayout>
  43. </ViewCell>
  44. </DataTemplate>
  45. </ListView.ItemTemplate>
  46. </ListView>
  47. </ContentPage>
Step 5

Now, open Solution Explorer >>ExpendableListView(PCL) >>open MainPage.xaml.cs page. Here is the code for this page.
CS code
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Xamarin.Forms;
  7. namespace ExpendableListView
  8. {
  9. public partial class MainPage : ContentPage
  10. {
  11. public MainPage()
  12. {
  13. InitializeComponent();
  14. }
  15. private void ListViewItem_Tabbed(object sender, ItemTappedEventArgs e)
  16. {
  17. var product = e.Item as Product;
  18. var vm = BindingContext as MainListView;
  19. vm?.ShoworHiddenProducts(product);
  20. }
  21. }
  22. }
Step 6

Click "F5" or "Build " to "Run" your application.Running your project, you will have the result like below.

Finally, we have successfully created a Xamarin.Forms Hierarchical Navigation Application.