我正在尝试使用GTK在vala中创建一个Calendar Widget。我有一个其构造函数用42个标签填充Gtk.Grid的类。但是,当调用负责迭代标签列表的方法fill_grid_days()时(根据最大天数隐藏或显示标签)首次运行时,直到在外部类中使用相同方法时,才隐藏标签按下按钮即可填充fill_grid_days()。
CalendarView类-构造函数
public CalendarView () {
this.orientation = Gtk.Orientation.VERTICAL;
int max_labels = 42;
var days_header = new DaysRow();
/*
* Grid that contains any number of days
* day_of_week is the start day(m,t,w,t,f,s,s) of month
* end_day is the final number day
*/
day_grid = new Grid ();
var col = 0;
var row = 0;
for (int i = 0; i < max_labels; i++) {
var label_day = new Label("");
label_day.get_style_context ().add_class ("label-day");
label_day.expand = true;
label_day.halign = Align.CENTER;
label_day.valign = Align.START;
day_grid.attach (label_day, col, row, 1, 1);
col++;
if (col != 0 && col % 7 == 0) {
row++;
col = 0;
}
labels.append(label_day);
}
this.pack_start (days_header);
this.pack_end (day_grid);
}
CalendarView类-fill_grid_days方法
public void fill_grid_days (int start_day, int max_day, int current_day) {
/*
* All days in interation to add a new Label
*/
var day_number = 1;
for (int i = 0; i < 42; i++) {
Label label = labels.nth_data (i);
label.get_style_context ().remove_class ("label-today");
label.visible = true;
/*
* max_day + start_day, it is necessary to
* find the correct label in list
*/
if (i < start_day || i >= max_day + start_day) {
print ("\nMe pongo invisible");
label.visible = false;
} else {
/*
* current_day + start_day, it is necessary to
* find the correct label in list
*/
if ( current_day != -1 && (i+1) == current_day + start_day ) {
label.get_style_context ().add_class ("label-today");
}
//label.set_label (day_number.to_string());
label.label = day_number.to_string();
day_number++;
}
}
}
完整代码 https://github.com/calo001/luna/blob/master/src/views/CalendarView.vala