public void OnItemTapped(MultiLevelItemBase item) { // Check if item is on expand state if (item.IsExpand) { // Set collapse item.Collapse(); var allChildren = item.Children.Concat(item.Children.SelectMany(p => p.Children)); // Remove children foreach (var child in allChildren) { ItemsSource.Remove(child); } } else { // get item index var i = ItemsSource.IndexOf(item) + 1; // insert item under the parent foreach (var child in item.Children) { ItemsSource.Insert(i++, child); } item.Expand(); } }
private void InitAsync() { _listView = new MultiLevelListView(); _source = new ObservableCollection<MultiLevelItemBase>(); _listView.ItemTemplate = new DataTemplate(typeof(MrGestureTestCell)); _listView.ItemTemplate.SetBinding(MrGestureTestCell.ItemProperty, "Item"); // The root page of your application Content = new ContentView() { Content = _listView }; for (int i = 0; i < 3; i++) { var root = new MultiLevelItemBase() { Name = "Root " + i }; _source.Add(root); for (int j = 0; j < 3; j++) { var child1 = new MultiLevelItemBase() { Name = $"\tChild {i}-{j}" }; root.Children.Add(child1); for (int k = 0; k < 3; k++) { var child2 = new MultiLevelItemBase() { Name = $"\t\tChild {i}-{j}-{k}" }; child1.Children.Add(child2); } } } _listView.ItemsSource = _source; }