2024-01-04 07:25:13 +01:00
|
|
|
.. _fragment:
|
|
|
|
|
2023-04-27 06:42:02 -06:00
|
|
|
========
|
|
|
|
Fragment
|
|
|
|
========
|
|
|
|
|
|
|
|
Fragment is a concept copied from
|
|
|
|
`Android <https://developer.android.com/guide/fragments>`__.
|
|
|
|
|
2023-04-27 11:47:13 -06:00
|
|
|
It represents a reusable portion of your app's UI. A fragment defines
|
2023-04-27 06:42:02 -06:00
|
|
|
and manages its own layout, has its own lifecycle, and can handle its
|
2023-04-27 11:47:13 -06:00
|
|
|
own events. Like Android's Fragment that must be hosted by an activity
|
2023-04-27 06:42:02 -06:00
|
|
|
or another fragment, Fragment in LVGL needs to be hosted by an object,
|
2023-04-27 11:47:13 -06:00
|
|
|
or another fragment. The fragment's view hierarchy becomes part of, or
|
|
|
|
attaches to, the host's view hierarchy.
|
2023-04-27 06:42:02 -06:00
|
|
|
|
|
|
|
Such concept also has some similarities to `UiViewController on
|
|
|
|
iOS <https://developer.apple.com/documentation/uikit/uiviewcontroller>`__.
|
|
|
|
|
|
|
|
Fragment Manager is a manager holding references to fragments attached
|
|
|
|
to it, and has an internal stack to achieve navigation. You can use
|
|
|
|
fragment manager to build navigation stack, or multi pane application
|
|
|
|
easily.
|
|
|
|
|
2024-01-04 07:25:13 +01:00
|
|
|
.. _fragment_usage:
|
|
|
|
|
2023-04-27 06:42:02 -06:00
|
|
|
Usage
|
|
|
|
-----
|
|
|
|
|
|
|
|
Enable :c:macro:`LV_USE_FRAGMENT` in ``lv_conf.h``.
|
|
|
|
|
|
|
|
Create Fragment Class
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
.. code:: c
|
|
|
|
|
|
|
|
struct sample_fragment_t {
|
|
|
|
/* IMPORTANT: don't miss this part */
|
|
|
|
lv_fragment_t base;
|
|
|
|
/* States, object references and data fields for this fragment */
|
|
|
|
const char *title;
|
|
|
|
};
|
|
|
|
|
|
|
|
const lv_fragment_class_t sample_cls = {
|
|
|
|
/* Initialize something needed */
|
|
|
|
.constructor_cb = sample_fragment_ctor,
|
|
|
|
/* Create view objects */
|
|
|
|
.create_obj_cb = sample_fragment_create_obj,
|
|
|
|
/* IMPORTANT: size of your fragment struct */
|
|
|
|
.instance_size = sizeof(struct sample_fragment_t)
|
|
|
|
};
|
|
|
|
|
|
|
|
Use ``lv_fragment_manager``
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
.. code:: c
|
|
|
|
|
|
|
|
/* Create fragment instance, and objects will be added to container */
|
|
|
|
lv_fragment_manager_t *manager = lv_fragment_manager_create(container, NULL);
|
|
|
|
/* Replace current fragment with instance of sample_cls, and init_argument is user defined pointer */
|
|
|
|
lv_fragment_manager_replace(manager, &sample_cls, init_argument);
|
|
|
|
|
|
|
|
Fragment Based Navigation
|
|
|
|
~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
.. code:: c
|
|
|
|
|
|
|
|
/* Add one instance into manager stack. View object of current fragment will be destroyed,
|
|
|
|
* but instances created in class constructor will be kept.
|
|
|
|
*/
|
|
|
|
lv_fragment_manager_push(manager, &sample_cls, NULL);
|
|
|
|
|
|
|
|
/* Remove the top most fragment from the stack, and bring back previous one. */
|
|
|
|
lv_fragment_manager_pop(manager);
|
|
|
|
|
2024-01-04 07:25:13 +01:00
|
|
|
.. _fragment_example:
|
|
|
|
|
2023-04-27 06:42:02 -06:00
|
|
|
Example
|
|
|
|
-------
|
|
|
|
|
|
|
|
.. include:: ../examples/others/fragment/index.rst
|
|
|
|
|
2024-01-04 07:25:13 +01:00
|
|
|
.. _fragment_api:
|
|
|
|
|
2023-04-27 06:42:02 -06:00
|
|
|
API
|
|
|
|
---
|
|
|
|
|