简体   繁体   English

Xamarin形式的可扩展ListView

[英]Expandable ListView in Xamarin Forms

I have the following code to display a ListView using Xamarin Forms: 我有以下代码使用Xamarin Forms显示ListView:

App.cs App.cs

public App ()
{
    MainPage = GetMainPage();
}

public static Page GetMainPage()
{
    return new NavigationPage(new DrilldownListViewByItem());
}

DrilldownListViewByItem: DrilldownListViewByItem:

public class DrilldownListViewByItem : ContentPage
{
    public DrilldownListViewByItem()
    {
        Title = "Drilldown List Using ListView";
        var listView = new ListView();
        listView.ItemsSource = new ListItem[] {
            new ListItem {Title = "First", Description="1st item"},
            new ListItem {Title = "Second", Description="2nd item"},
            new ListItem {Title = "Third", Description="3rd item"}
            };
        listView.ItemTemplate = new DataTemplate(typeof(TextCell));
        listView.ItemTemplate.SetBinding(TextCell.TextProperty, "Title");

        listView.ItemTapped += async (sender, args) =>
        {
            var item = args.Item as ListItem;
            //if (item == null) return;
            //await Navigation.PushAsync(new DetailPage(item));
            //listView.SelectedItem = null;
        };
        Content = listView;
    }
}

ListItem has these properties: ListItem具有以下属性:

public string Title { get; set; }
public string Description { get; set; }

What I want to do is to show subitems when I tab on one of the main items. 我要做的是在我选择其中一个主要项目时显示子项目。 These subitems should be hidden at the beggining, but they should be there, I don't want to load them every time I tab on an item. 这些子项目应该在开始时就隐藏了,但是它们应该在那儿,我不想每次我在一个项目上打标签时都加载它们。 Any idea? 任何想法? Thanks! 谢谢!

Try changing the visibility of the items you want to appear and disappear based on the click event of the list item. 尝试根据列表项的click事件更改要显示和消失的项的可见性。

On iOS you may also have to change the cell height (using HeightRequest), because it is likely that it won't change on it's own. 在iOS上,您可能还必须更改单元格高度(使用HeightRequest),因为它很可能不会自行更改。 I am guessing that android will work just fine. 我猜想android可以正常工作。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM