Levi
Levi

Reputation: 317

How to get the index or position of an item in Gtk.ListBox in vala?

I want a behavior like if you click on the first listitem open do a thing, if I click on the second listitem do other thing. Same thing for the third and second.

Similar to switchboard but not in iconview (I have the animason and gtk.stack set up): https://www.youtube.com/watch?v=Lj2wKNYVFR8

This is the code:

var listbox = new Gtk.ListBox();
listbox.set_activate_on_single_click(true);

var l = new Watcher.List.ListItem("title", "subtitle");
listbox.insert(l, 0);
var l2 = new Watcher.List.ListItem("title2", "subtitle2");
listbox.insert(l2, 1);

"l" and l2 is a Gtk.ListBoxRow object so basically I add 2 listboxrow item into a listbox.

When I click on an Item do stuff:

listbox.row_selected.connect( ()=>{
    stack.set_visible_child_name("new");
    back.set_child_visible(true);   
});

It will always show the stack child named "new" whether I click the first or second item in the list. I can acces the ListBoxRow index but that is just only for a specific Row.

I need to add custom formated items into a list and I was told that ListBox can do it. (Others can't?) My ListBoxRow is just a grid with two labels in it but I want to add buttons and some more later.

Upvotes: 1

Views: 1473

Answers (1)

Jussi Kukkonen
Jussi Kukkonen

Reputation: 14607

Look at the reference for Listbox.row_selected(): You'll see that it has a ListBoxRow argument. You should update your row_selected closure to have that argument as well -- then you can use the row inside the closure to figure out which child you want to set visible.

If you really need to know the index of the row inside the listbox, you call row.get_index().

Upvotes: 4

Related Questions