Tabview: add same obj (container) to different pages

Description

I would like to create an object, that for “save memory resources”, I would like to share between multiple tabview pages.

So I have ObjectContainer that will be displayed on PageA, PageB, PageC, etc…

What MCU/Processor/Board and compiler are you using?

What LVGL version are you using?

v8.2.0

What do you want to achieve?

Assign the same created screen to multiple pages on tabview without re create the screen.

What have you tried so far?

I used the lv_obj_set_parent( , ) and it works, but it moves from one tab to another and removing the previous parent.

It can work for me, is it possible to set the parent before the page need to be draw?

Hi @KammutierSpule,

Here is one way you could do this if it is helpful:

/**********************
 *      VARIABLES
 **********************/
static lv_obj_t		*shared_tab_content;
static lv_obj_t 	*tabview;
static lv_obj_t		*tab1;
static lv_obj_t		*tab2;
static lv_obj_t		*tab3;
/**********************
 *  STATIC PROTOTYPES
 **********************/
static void tv_callback(lv_event_t * e) {

	lv_event_code_t 	code = lv_event_get_code(e);
	lv_obj_t			*obj = lv_event_get_current_target(e);
	uint16_t			tab;

	tab = lv_tabview_get_tab_act(obj);

    if(code == LV_EVENT_VALUE_CHANGED) {
        switch( tab ) {

        	case 0:
        		lv_obj_set_parent( shared_tab_content, tab1 );
        		break;
        	case 1:
        		lv_obj_set_parent( shared_tab_content, tab2 );
        		break;
        	case 2:
        		lv_obj_set_parent( shared_tab_content, tab3 );
        		break;
        }
    }
}

static void test( void ) {

	/*Create a Tab view object*/
	tabview = lv_tabview_create(lv_scr_act(), LV_DIR_TOP, 30);
	lv_obj_add_event_cb(tabview, tv_callback, LV_EVENT_VALUE_CHANGED, NULL);

	lv_obj_set_style_bg_color(tabview, lv_palette_lighten(LV_PALETTE_BLUE, 2), 0);

	lv_obj_t * tab_btns = lv_tabview_get_tab_btns(tabview);
	lv_obj_set_style_bg_color(tab_btns, lv_palette_darken(LV_PALETTE_GREY, 3), 0);
	lv_obj_set_style_text_color(tab_btns, lv_palette_lighten(LV_PALETTE_GREY, 5), 0);

	tab1 = lv_tabview_add_tab(tabview, "Tab 1");
	tab2 = lv_tabview_add_tab(tabview, "Tab 2");
	tab3 = lv_tabview_add_tab(tabview, "Tab 3");

	/*Add content to the tabs*/
	shared_tab_content = lv_obj_create(tab1);
	lv_obj_set_size( shared_tab_content, 400, 400 );
	lv_obj_align(shared_tab_content, LV_ALIGN_CENTER, 0, 0);

	lv_obj_t * label = lv_label_create(shared_tab_content);
	lv_label_set_text(label, "Label Text");
	lv_obj_align( label, LV_ALIGN_CENTER, 0, 0);

	lv_obj_t * ta = lv_textarea_create(shared_tab_content);
	lv_textarea_set_text(ta, "TA Text");
	lv_obj_align( ta, LV_ALIGN_BOTTOM_MID, 0, 0);

}

If you have the simulator set up you can paste this straight in and call test() to see it working.

Kind Regards,

Pete

1 Like

Thanks! I was trying something like that but with the LV_EVENT_SCROLL_END, that was not a good option as it was redrawing. So your suggestion to use LV_EVENT_VALUE_CHANGED looks better.

1 Like