1 #include "../lv_examples.h"
2 #if LV_BUILD_EXAMPLES && LV_USE_FLEX
3
sw_event_cb(lv_event_t * e)4 static void sw_event_cb(lv_event_t * e)
5 {
6 lv_event_code_t code = lv_event_get_code(e);
7 lv_obj_t * sw = lv_event_get_target(e);
8
9 if(code == LV_EVENT_VALUE_CHANGED) {
10 lv_obj_t * list = lv_event_get_user_data(e);
11
12 if(lv_obj_has_state(sw, LV_STATE_CHECKED)) lv_obj_add_flag(list, LV_OBJ_FLAG_SCROLL_ONE);
13 else lv_obj_clear_flag(list, LV_OBJ_FLAG_SCROLL_ONE);
14 }
15 }
16
17 /**
18 * Show an example to scroll snap
19 */
lv_example_scroll_2(void)20 void lv_example_scroll_2(void)
21 {
22 lv_obj_t * panel = lv_obj_create(lv_scr_act());
23 lv_obj_set_size(panel, 280, 120);
24 lv_obj_set_scroll_snap_x(panel, LV_SCROLL_SNAP_CENTER);
25 lv_obj_set_flex_flow(panel, LV_FLEX_FLOW_ROW);
26 lv_obj_align(panel, LV_ALIGN_CENTER, 0, 20);
27
28 uint32_t i;
29 for(i = 0; i < 10; i++) {
30 lv_obj_t * btn = lv_btn_create(panel);
31 lv_obj_set_size(btn, 150, lv_pct(100));
32
33 lv_obj_t * label = lv_label_create(btn);
34 if(i == 3) {
35 lv_label_set_text_fmt(label, "Panel %"LV_PRIu32"\nno snap", i);
36 lv_obj_clear_flag(btn, LV_OBJ_FLAG_SNAPPABLE);
37 }
38 else {
39 lv_label_set_text_fmt(label, "Panel %"LV_PRIu32, i);
40 }
41
42 lv_obj_center(label);
43 }
44 lv_obj_update_snap(panel, LV_ANIM_ON);
45
46 #if LV_USE_SWITCH
47 /*Switch between "One scroll" and "Normal scroll" mode*/
48 lv_obj_t * sw = lv_switch_create(lv_scr_act());
49 lv_obj_align(sw, LV_ALIGN_TOP_RIGHT, -20, 10);
50 lv_obj_add_event_cb(sw, sw_event_cb, LV_EVENT_ALL, panel);
51 lv_obj_t * label = lv_label_create(lv_scr_act());
52 lv_label_set_text(label, "One scroll");
53 lv_obj_align_to(label, sw, LV_ALIGN_OUT_BOTTOM_MID, 0, 5);
54 #endif
55 }
56
57 #endif
58