2021-09-05 20:47:55 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
2021-08-24 14:30:38 +02:00
|
|
|
|
2021-08-18 11:32:10 +02:00
|
|
|
#include "../../lv_examples.h"
|
|
|
|
#if LV_USE_LIST && LV_BUILD_EXAMPLES
|
|
|
|
|
2021-08-18 14:28:02 +02:00
|
|
|
static lv_obj_t* list1;
|
2021-08-18 11:32:10 +02:00
|
|
|
static lv_obj_t* list2;
|
|
|
|
|
|
|
|
static lv_obj_t* currentButton = NULL;
|
|
|
|
|
|
|
|
static void event_handler(lv_event_t* e)
|
|
|
|
{
|
|
|
|
lv_event_code_t code = lv_event_get_code(e);
|
|
|
|
lv_obj_t* obj = lv_event_get_target(e);
|
|
|
|
if (code == LV_EVENT_CLICKED)
|
|
|
|
{
|
2021-08-18 14:28:02 +02:00
|
|
|
LV_LOG_USER("Clicked: %s", lv_list_get_btn_text(list1, obj));
|
2021-08-18 11:32:10 +02:00
|
|
|
|
|
|
|
if (currentButton == obj)
|
|
|
|
{
|
|
|
|
currentButton = NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
currentButton = obj;
|
|
|
|
}
|
|
|
|
lv_obj_t* parent = lv_obj_get_parent(obj);
|
|
|
|
uint32_t i;
|
|
|
|
for (i = 0; i < lv_obj_get_child_cnt(parent); i++)
|
|
|
|
{
|
|
|
|
lv_obj_t* child = lv_obj_get_child(parent, i);
|
|
|
|
if (child == currentButton)
|
|
|
|
{
|
|
|
|
lv_obj_add_state(child, LV_STATE_CHECKED);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
lv_obj_clear_state(child, LV_STATE_CHECKED);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-24 14:30:38 +02:00
|
|
|
static void event_handler_top(lv_event_t* e)
|
|
|
|
{
|
|
|
|
lv_event_code_t code = lv_event_get_code(e);
|
|
|
|
if (code == LV_EVENT_CLICKED)
|
|
|
|
{
|
|
|
|
if (currentButton == NULL) return;
|
|
|
|
lv_obj_move_background(currentButton);
|
|
|
|
lv_obj_scroll_to_view(currentButton, LV_ANIM_ON);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void event_handler_up(lv_event_t* e)
|
2021-08-18 11:32:10 +02:00
|
|
|
{
|
|
|
|
lv_event_code_t code = lv_event_get_code(e);
|
|
|
|
if ((code == LV_EVENT_CLICKED) || (code == LV_EVENT_LONG_PRESSED_REPEAT))
|
|
|
|
{
|
|
|
|
if (currentButton == NULL) return;
|
2021-09-05 20:47:55 +02:00
|
|
|
uint32_t index = lv_obj_get_index(currentButton);
|
|
|
|
if (index <= 0) return;
|
|
|
|
lv_obj_move_to_index(currentButton, index - 1);
|
|
|
|
lv_obj_scroll_to_view(currentButton, LV_ANIM_ON);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void event_handler_center(lv_event_t* e)
|
|
|
|
{
|
|
|
|
const lv_event_code_t code = lv_event_get_code(e);
|
|
|
|
if ((code == LV_EVENT_CLICKED) || (code == LV_EVENT_LONG_PRESSED_REPEAT))
|
|
|
|
{
|
|
|
|
if (currentButton == NULL) return;
|
|
|
|
|
|
|
|
lv_obj_t* parent = lv_obj_get_parent(currentButton);
|
|
|
|
const uint32_t pos = lv_obj_get_child_cnt(parent) / 2;
|
|
|
|
|
|
|
|
lv_obj_move_to_index(currentButton, pos);
|
|
|
|
|
2021-08-24 14:30:38 +02:00
|
|
|
lv_obj_scroll_to_view(currentButton, LV_ANIM_ON);
|
2021-08-18 11:32:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void event_handler_dn(lv_event_t* e)
|
|
|
|
{
|
2021-09-05 20:47:55 +02:00
|
|
|
const lv_event_code_t code = lv_event_get_code(e);
|
2021-08-18 11:32:10 +02:00
|
|
|
if ((code == LV_EVENT_CLICKED) || (code == LV_EVENT_LONG_PRESSED_REPEAT))
|
|
|
|
{
|
|
|
|
if (currentButton == NULL) return;
|
2021-09-05 20:47:55 +02:00
|
|
|
const uint32_t index = lv_obj_get_index(currentButton);
|
|
|
|
|
|
|
|
lv_obj_move_to_index(currentButton, index + 1);
|
2021-08-24 14:30:38 +02:00
|
|
|
lv_obj_scroll_to_view(currentButton, LV_ANIM_ON);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void event_handler_bottom(lv_event_t* e)
|
|
|
|
{
|
2021-09-05 20:47:55 +02:00
|
|
|
const lv_event_code_t code = lv_event_get_code(e);
|
2021-08-24 14:30:38 +02:00
|
|
|
if (code == LV_EVENT_CLICKED)
|
|
|
|
{
|
|
|
|
if (currentButton == NULL) return;
|
|
|
|
lv_obj_move_foreground(currentButton);
|
|
|
|
lv_obj_scroll_to_view(currentButton, LV_ANIM_ON);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void event_handler_swap(lv_event_t* e)
|
|
|
|
{
|
2021-09-05 20:47:55 +02:00
|
|
|
const lv_event_code_t code = lv_event_get_code(e);
|
2021-08-24 14:30:38 +02:00
|
|
|
// lv_obj_t* obj = lv_event_get_target(e);
|
|
|
|
if ((code == LV_EVENT_CLICKED) || (code == LV_EVENT_LONG_PRESSED_REPEAT))
|
|
|
|
{
|
2021-09-05 20:47:55 +02:00
|
|
|
uint32_t cnt = lv_obj_get_child_cnt(list1);
|
|
|
|
for (int i = 0; i < 100; i++)
|
|
|
|
if (cnt > 1)
|
2021-08-24 14:30:38 +02:00
|
|
|
{
|
2021-09-05 20:47:55 +02:00
|
|
|
lv_obj_t* obj = lv_obj_get_child(list1, rand() % cnt);
|
|
|
|
lv_obj_move_to_index(obj, rand() % cnt);
|
|
|
|
if (currentButton != NULL)
|
|
|
|
{
|
|
|
|
lv_obj_scroll_to_view(currentButton, LV_ANIM_ON);
|
|
|
|
}
|
2021-08-24 14:30:38 +02:00
|
|
|
}
|
2021-08-18 11:32:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void lv_example_list_2(void)
|
|
|
|
{
|
|
|
|
/*Create a list*/
|
2021-08-18 14:28:02 +02:00
|
|
|
list1 = lv_list_create(lv_scr_act());
|
2021-08-19 21:11:57 +02:00
|
|
|
lv_obj_set_size(list1, lv_pct(60), lv_pct(100));
|
2021-08-18 14:28:02 +02:00
|
|
|
lv_obj_set_style_pad_row(list1, 5, 0);
|
2021-08-18 11:32:10 +02:00
|
|
|
|
|
|
|
/*Add buttons to the list*/
|
|
|
|
lv_obj_t* btn;
|
2021-08-18 14:28:02 +02:00
|
|
|
int i;
|
2021-09-05 20:47:55 +02:00
|
|
|
for (i = 0; i < 15; i++) {
|
2021-08-18 14:28:02 +02:00
|
|
|
btn = lv_btn_create(list1);
|
|
|
|
lv_obj_set_width(btn, lv_pct(50));
|
|
|
|
lv_obj_add_event_cb(btn, event_handler, LV_EVENT_CLICKED, NULL);
|
2021-08-18 11:32:10 +02:00
|
|
|
|
|
|
|
lv_obj_t* lab = lv_label_create(btn);
|
2021-08-18 14:28:02 +02:00
|
|
|
lv_label_set_text_fmt(lab, "Item %d", i);
|
2021-08-18 11:32:10 +02:00
|
|
|
}
|
|
|
|
|
2021-08-18 14:28:02 +02:00
|
|
|
/*Select the first button by default*/
|
|
|
|
currentButton = lv_obj_get_child(list1, 0);
|
|
|
|
lv_obj_add_state(currentButton, LV_STATE_CHECKED);
|
|
|
|
|
|
|
|
/*Create a second list with up and down buttons*/
|
|
|
|
list2 = lv_list_create(lv_scr_act());
|
2021-08-19 21:11:57 +02:00
|
|
|
lv_obj_set_size(list2, lv_pct(40), lv_pct(100));
|
2021-08-18 14:28:02 +02:00
|
|
|
lv_obj_align(list2, LV_ALIGN_TOP_RIGHT, 0, 0);
|
|
|
|
lv_obj_set_flex_flow(list2, LV_FLEX_FLOW_COLUMN);
|
2021-08-18 11:32:10 +02:00
|
|
|
|
2021-08-24 14:30:38 +02:00
|
|
|
btn = lv_list_add_btn(list2, NULL, "Top");
|
|
|
|
lv_obj_add_event_cb(btn, event_handler_top, LV_EVENT_ALL, NULL);
|
|
|
|
lv_group_remove_obj(btn);
|
|
|
|
|
2021-08-18 14:28:02 +02:00
|
|
|
btn = lv_list_add_btn(list2, LV_SYMBOL_UP, "Up");
|
2021-08-24 14:30:38 +02:00
|
|
|
lv_obj_add_event_cb(btn, event_handler_up, LV_EVENT_ALL, NULL);
|
2021-08-19 21:11:57 +02:00
|
|
|
lv_group_remove_obj(btn);
|
2021-08-18 14:28:02 +02:00
|
|
|
|
2021-09-05 20:47:55 +02:00
|
|
|
btn = lv_list_add_btn(list2, LV_SYMBOL_LEFT, "Center");
|
|
|
|
lv_obj_add_event_cb(btn, event_handler_center, LV_EVENT_ALL, NULL);
|
|
|
|
lv_group_remove_obj(btn);
|
|
|
|
|
2021-08-18 14:28:02 +02:00
|
|
|
btn = lv_list_add_btn(list2, LV_SYMBOL_DOWN, "Down");
|
2021-08-18 11:32:10 +02:00
|
|
|
lv_obj_add_event_cb(btn, event_handler_dn, LV_EVENT_ALL, NULL);
|
2021-08-19 21:11:57 +02:00
|
|
|
lv_group_remove_obj(btn);
|
2021-08-24 14:30:38 +02:00
|
|
|
|
|
|
|
btn = lv_list_add_btn(list2, NULL, "Bottom");
|
|
|
|
lv_obj_add_event_cb(btn, event_handler_bottom, LV_EVENT_ALL, NULL);
|
|
|
|
lv_group_remove_obj(btn);
|
|
|
|
|
|
|
|
btn = lv_list_add_btn(list2, LV_SYMBOL_SHUFFLE, "Shuffle");
|
|
|
|
lv_obj_add_event_cb(btn, event_handler_swap, LV_EVENT_ALL, NULL);
|
|
|
|
lv_group_remove_obj(btn);
|
2021-08-18 11:32:10 +02:00
|
|
|
}
|
|
|
|
|
2021-08-18 14:28:02 +02:00
|
|
|
#endif
|