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