Robin
Robin

Reputation: 1587

Tapped gesture in Xamarin.forms

So I have to make my layout for the first time in C#...

Now I want to be able to tab on a listview item and then go to a detail page with the data of te tapped item.. How can I do this?

I currently have this code;

public MainPage()
{
    GeneratePage();
    InitializeComponent();
}

private async Task GeneratePage()
{
    List<Folder> folders = await ApiManager.GetFoldersAsync();

    foreach (var f in folders)
    {
        List<Lists> lists = new List<Lists>();
        foreach (int id in f.list_ids)
        {
            lists.Add(await ApiManager.GetSpecificListAsync(id));
            // Debug.WriteLine("ID for list '"+ f.title +"' : " + id);
        }
        this.Children.Add(new ContentPage
        {
            Title = f.title,
            Content = new ListView
            {
                ItemsSource = lists
            }
        });
    }
}

Upvotes: 1

Views: 348

Answers (2)

you have a list of ContentPage?? It's very strange. But, normally, for add a tap-event for a ListView item you subscribe to the ItemTapped event.

var list = new ListView();
list.ItemsSource = myItems;
list.ItemTapped += myEventTapped();
Content = list;`

Upvotes: 2

Tobias Theel
Tobias Theel

Reputation: 3217

Normally I use XAML instead of CodeBehind to create UIs, but the following snippet should do the trick, but I have not tested it.

Just attach to the ItemTapped event. Alternatively you could also add an TapGestureRecognizer onto your ListView.

private async Task GeneratePage() 
{
    List<Folder> folders = await ApiManager.GetFoldersAsync();

    foreach (var f in folders) 
    {
        List<Lists> lists = new List<Lists>();

        foreach (int id in f.list_ids) 
        {
            lists.Add(await ApiManager.GetSpecificListAsync(id));
            // Debug.WriteLine("ID for list '"+ f.title +"' : " + id);
        }

        ListView listView = new ListView { ItemsSource = lists };

        listView.ItemTapped += ListViewOnItemTapped;

        this.Children.Add(new ContentPage 
        {
            Title = f.title,
            Content = listView
        });
    }
}

void ListViewOnItemTapped(object sender, ItemTappedEventArgs itemTappedEventArgs) { throw new NotImplementedException(); }

Upvotes: 2

Related Questions