1 #include "../../lv_examples.h"
2 #if LV_USE_LIST && LV_BUILD_EXAMPLES
3 
4 static lv_obj_t * list1;
5 static lv_obj_t * list2;
6 
7 static lv_obj_t * currentButton = NULL;
8 
event_handler(lv_event_t * e)9 static void event_handler(lv_event_t * e)
10 {
11     lv_event_code_t code = lv_event_get_code(e);
12     lv_obj_t * obj = lv_event_get_target(e);
13     if(code == LV_EVENT_CLICKED) {
14         LV_LOG_USER("Clicked: %s", lv_list_get_button_text(list1, obj));
15 
16         if(currentButton == obj) {
17             currentButton = NULL;
18         }
19         else {
20             currentButton = obj;
21         }
22         lv_obj_t * parent = lv_obj_get_parent(obj);
23         uint32_t i;
24         for(i = 0; i < lv_obj_get_child_count(parent); i++) {
25             lv_obj_t * child = lv_obj_get_child(parent, i);
26             if(child == currentButton) {
27                 lv_obj_add_state(child, LV_STATE_CHECKED);
28             }
29             else {
30                 lv_obj_remove_state(child, LV_STATE_CHECKED);
31             }
32         }
33     }
34 }
35 
event_handler_top(lv_event_t * e)36 static void event_handler_top(lv_event_t * e)
37 {
38     lv_event_code_t code = lv_event_get_code(e);
39     if(code == LV_EVENT_CLICKED) {
40         if(currentButton == NULL) return;
41         lv_obj_move_background(currentButton);
42         lv_obj_scroll_to_view(currentButton, LV_ANIM_ON);
43     }
44 }
45 
event_handler_up(lv_event_t * e)46 static void event_handler_up(lv_event_t * e)
47 {
48     lv_event_code_t code = lv_event_get_code(e);
49     if((code == LV_EVENT_CLICKED) || (code == LV_EVENT_LONG_PRESSED_REPEAT)) {
50         if(currentButton == NULL) return;
51         uint32_t index = lv_obj_get_index(currentButton);
52         if(index <= 0) return;
53         lv_obj_move_to_index(currentButton, index - 1);
54         lv_obj_scroll_to_view(currentButton, LV_ANIM_ON);
55     }
56 }
57 
event_handler_center(lv_event_t * e)58 static void event_handler_center(lv_event_t * e)
59 {
60     const lv_event_code_t code = lv_event_get_code(e);
61     if((code == LV_EVENT_CLICKED) || (code == LV_EVENT_LONG_PRESSED_REPEAT)) {
62         if(currentButton == NULL) return;
63 
64         lv_obj_t * parent = lv_obj_get_parent(currentButton);
65         const uint32_t pos = lv_obj_get_child_count(parent) / 2;
66 
67         lv_obj_move_to_index(currentButton, pos);
68 
69         lv_obj_scroll_to_view(currentButton, LV_ANIM_ON);
70     }
71 }
72 
event_handler_dn(lv_event_t * e)73 static void event_handler_dn(lv_event_t * e)
74 {
75     const lv_event_code_t code = lv_event_get_code(e);
76     if((code == LV_EVENT_CLICKED) || (code == LV_EVENT_LONG_PRESSED_REPEAT)) {
77         if(currentButton == NULL) return;
78         const uint32_t index = lv_obj_get_index(currentButton);
79 
80         lv_obj_move_to_index(currentButton, index + 1);
81         lv_obj_scroll_to_view(currentButton, LV_ANIM_ON);
82     }
83 }
84 
event_handler_bottom(lv_event_t * e)85 static void event_handler_bottom(lv_event_t * e)
86 {
87     const lv_event_code_t code = lv_event_get_code(e);
88     if(code == LV_EVENT_CLICKED) {
89         if(currentButton == NULL) return;
90         lv_obj_move_foreground(currentButton);
91         lv_obj_scroll_to_view(currentButton, LV_ANIM_ON);
92     }
93 }
94 
event_handler_swap(lv_event_t * e)95 static void event_handler_swap(lv_event_t * e)
96 {
97     const lv_event_code_t code = lv_event_get_code(e);
98     // lv_obj_t* obj = lv_event_get_target(e);
99     if((code == LV_EVENT_CLICKED) || (code == LV_EVENT_LONG_PRESSED_REPEAT)) {
100         uint32_t cnt = lv_obj_get_child_count(list1);
101         for(int i = 0; i < 100; i++)
102             if(cnt > 1) {
103                 lv_obj_t * obj = lv_obj_get_child(list1, lv_rand(0, cnt));
104                 lv_obj_move_to_index(obj, lv_rand(0, cnt));
105                 if(currentButton != NULL) {
106                     lv_obj_scroll_to_view(currentButton, LV_ANIM_ON);
107                 }
108             }
109     }
110 }
111 
lv_example_list_2(void)112 void lv_example_list_2(void)
113 {
114     /*Create a list*/
115     list1 = lv_list_create(lv_screen_active());
116     lv_obj_set_size(list1, lv_pct(60), lv_pct(100));
117     lv_obj_set_style_pad_row(list1, 5, 0);
118 
119     /*Add buttons to the list*/
120     lv_obj_t * btn;
121     int i;
122     for(i = 0; i < 15; i++) {
123         btn = lv_button_create(list1);
124         lv_obj_set_width(btn, lv_pct(50));
125         lv_obj_add_event_cb(btn, event_handler, LV_EVENT_CLICKED, NULL);
126 
127         lv_obj_t * lab = lv_label_create(btn);
128         lv_label_set_text_fmt(lab, "Item %d", i);
129     }
130 
131     /*Select the first button by default*/
132     currentButton = lv_obj_get_child(list1, 0);
133     lv_obj_add_state(currentButton, LV_STATE_CHECKED);
134 
135     /*Create a second list with up and down buttons*/
136     list2 = lv_list_create(lv_screen_active());
137     lv_obj_set_size(list2, lv_pct(40), lv_pct(100));
138     lv_obj_align(list2, LV_ALIGN_TOP_RIGHT, 0, 0);
139     lv_obj_set_flex_flow(list2, LV_FLEX_FLOW_COLUMN);
140 
141     btn = lv_list_add_button(list2, NULL, "Top");
142     lv_obj_add_event_cb(btn, event_handler_top, LV_EVENT_ALL, NULL);
143     lv_group_remove_obj(btn);
144 
145     btn = lv_list_add_button(list2, LV_SYMBOL_UP, "Up");
146     lv_obj_add_event_cb(btn, event_handler_up, LV_EVENT_ALL, NULL);
147     lv_group_remove_obj(btn);
148 
149     btn = lv_list_add_button(list2, LV_SYMBOL_LEFT, "Center");
150     lv_obj_add_event_cb(btn, event_handler_center, LV_EVENT_ALL, NULL);
151     lv_group_remove_obj(btn);
152 
153     btn = lv_list_add_button(list2, LV_SYMBOL_DOWN, "Down");
154     lv_obj_add_event_cb(btn, event_handler_dn, LV_EVENT_ALL, NULL);
155     lv_group_remove_obj(btn);
156 
157     btn = lv_list_add_button(list2, NULL, "Bottom");
158     lv_obj_add_event_cb(btn, event_handler_bottom, LV_EVENT_ALL, NULL);
159     lv_group_remove_obj(btn);
160 
161     btn = lv_list_add_button(list2, LV_SYMBOL_SHUFFLE, "Shuffle");
162     lv_obj_add_event_cb(btn, event_handler_swap, LV_EVENT_ALL, NULL);
163     lv_group_remove_obj(btn);
164 }
165 
166 #endif
167