【声明:版权全部,欢迎转载。请勿用于商业用途。
联系信箱:feixiaoxing @163.com】
在開始今天的list主题之前,先看一下icon的执行效果。
今天说的list事实上和这个icon几乎相同。大家仅仅须要把这些icon想像成一条一条的item就能够了。相同,list的demo代码中也会有一个more的button,每次单击一下。就会添加几个item。闲话不多说,先看一下demo代码是怎么样的。
#include "ftk.h" #include "ftk_list_view.h"
#include "ftk_list_render_default.h"
#include "ftk_list_model_default.h" #define IDC_TEST_BUTTON 1000
int g_index = 0;
FtkBitmap* left_icon = NULL;
FtkBitmap* right_icon = NULL; static Ret button_quit_clicked(void* ctx, void* obj)
{
ftk_quit(); return RET_OK;
} static Ret button_more_clicked(void* ctx, void* obj)
{
int i = 0;
char text[32] = {0};
FtkListItemInfo info = {0};
FtkListModel* model = ctx; for(i = 0; i < 4; i++)
{
g_index++;
ftk_snprintf(text, sizeof(text), "item%04d", g_index);
info.text = (text);
info.left_icon = left_icon;
info.right_icon = right_icon;
info.type = g_index%4;
ftk_list_model_add(model, &info);
} return RET_OK;
} Ret on_item_clicked(void* ctx, void* list)
{
FtkListItemInfo* info = NULL;
FtkListModel* model = ftk_list_view_get_model(list);
int i = ftk_list_view_get_selected(list); ftk_list_model_get_data(model, i, (void**)&info);
if(info != NULL)
{
info->state = !info->state;
}
ftk_logd("%s: %d/%d\n", __func__,
ftk_list_view_get_selected(list),
ftk_list_model_get_total(model)); return RET_OK;
} int FTK_MAIN(int argc, char* argv[])
{
int width = 0;
int height = 0;
char text[32] = {0};
FtkWidget* win = NULL;
FtkWidget* button = NULL;
FtkWidget* list = NULL;
FtkListModel* model = NULL;
FtkListRender* render = NULL;
FtkListItemInfo info = {0};
char filename[FTK_MAX_PATH+1] = {0};
ftk_init(argc, argv); win = ftk_app_window_create();
width = ftk_widget_width(win);
height = ftk_widget_height(win); list = ftk_list_view_create(win, 10, 5, width - 20, 3 * height/4-5);
ftk_list_view_set_clicked_listener(list, on_item_clicked, NULL); model = ftk_list_model_default_create(10);
render = ftk_list_render_default_create();
ftk_snprintf(filename, FTK_MAX_PATH, "%s/alarm/%s",TESTDATA_DIR, FTK_STOCK_IMG_SUFFIX);
left_icon = ftk_bitmap_factory_load(ftk_default_bitmap_factory(), filename);
ftk_snprintf(filename, FTK_MAX_PATH, "%s/search/%s", TESTDATA_DIR, FTK_STOCK_IMG_SUFFIX);
right_icon = ftk_bitmap_factory_load(ftk_default_bitmap_factory(), filename); g_index = 0;
for(g_index = 0; g_index < 4; g_index++)
{
ftk_snprintf(text, sizeof(text), "item%04d", g_index);
info.text = (text);
info.left_icon = left_icon;
info.right_icon = right_icon;
info.type = g_index%4;
ftk_list_model_add(model, &info);
} ftk_list_view_init(list, model, render, 40);
ftk_list_model_unref(model); button = ftk_button_create(win, width/4, 3 * height/4 + 5, width/4, 60);
ftk_widget_set_text(button, "more");
ftk_button_set_clicked_listener(button, button_more_clicked, model); button = ftk_button_create(win, width/2, 3 * height/4 + 5, width/4, 60);
ftk_widget_set_text(button, "quit");
ftk_button_set_clicked_listener(button, button_quit_clicked, win);
ftk_window_set_focus(win, button); ftk_widget_set_text(win, "list view demo");
ftk_widget_show_all(win, 1);
ftk_widget_set_attr(win, FTK_ATTR_QUIT_WHEN_CLOSE); ftk_run(); return 0;
}
细细看来,代码的流程很眼熟。
大家能够把注意力集中到list、model、render和info这几个变量上。当中,尤其须要注意的是list和info。list提供了一个显示空间,而info则用来表示每个list的item。当然,每个list item被选中的时候,on_item_clicked函数都会被调用。当然。除此之外系统也创建了两个button,两个callback函数各自是button_quit_clicked和button_more_clicked。详细的细节分析一下代码就可以。
好了。说了这么多,还是等着看下次的执行效果吧。