awtk控件实现上下滚动效果
#include "awtk.h"
#include "../common/navigator.h"
static widget_t *second_view = NULL;
static widget_t *btn1 = NULL;
static widget_t *btn2 = NULL;
static widget_t *btn3 = NULL;
static widget_t *win_main = NULL;
//创建控件对象
void initBtn()
{
if (!second_view)
{
second_view = view_create(win_main, 20, 20, 100, 200);
btn1 = button_create(second_view, 0, 0, second_view->w, 60);
btn2 = button_create(second_view, 0, 60, second_view->w, 60);
btn3 = button_create(second_view, 0, 120, second_view->w, 60);
widget_set_text_utf8(btn1, "按钮1");
widget_set_text_utf8(btn2, "按钮2");
widget_set_text_utf8(btn3, "按钮3");
}
}
static ret_t on_button_click(void *ctx, event_t *e)
{
for (int i = 0; i < 3; i++)
{
widget_t *label = widget_get_child(second_view, i);
int y = label->y - 60; //减去控件的高度
if (y < 0)
y = 120;
widget_move(label, 0, y);
}
return RET_OK;
}
static ret_t on_button1_click(void *ctx, event_t *e)
{
for (int i = 0; i < 3; i++)
{
widget_t *label = widget_get_child(second_view, i);
int y = label->y + 60;
if (y > 120)
y = 0;
widget_move(label, 0, y);
}
return RET_OK;
}
/**
* 初始化窗口的子控件
*/
static ret_t visit_init_child(void *ctx, const void *iter)
{
widget_t *win = WIDGET(ctx);
widget_t *widget = WIDGET(iter);
const char *name = widget->name;
// 初始化指定名称的控件(设置属性或注册事件),请保证控件名称在窗口上唯一
if (name != NULL && *name != '\0')
{
if (tk_str_eq(name, "button"))
{
widget_on(widget, EVT_CLICK, on_button_click, win);
}
else if (tk_str_eq(name, "button1"))
{
widget_on(widget, EVT_CLICK, on_button1_click, win);
}
}
return RET_OK;
}
/**
* 初始化窗口
*/
ret_t home_page_init(widget_t *win, void *ctx)
{
(void)ctx;
return_value_if_fail(win != NULL, RET_BAD_PARAMS);
win_main = win;
initBtn();
widget_foreach(win, visit_init_child, win);
return RET_OK;
}
注意:控件根据具体情况定义。