Actions with elements
When the user taps the card, action calls the event specified in url
.
{
"actions": [{
"log_id": "<unique_id>",
"url": "div-action://<action_description>?other/parameters"
}]
}
Parameter |
Description |
|
An event that is triggered by tapping the card. |
|
Logging ID. It must be unique within a single card. |
You can open the menu by tapping using the special menu_items
parameter. If at least one action in the array has a menu_items
field, tapping the element will show a menu, and all other fields will be ignored.
A separate type of action are actions that are triggered only by a long tap (longtap-actions
).
Sets of states
The state element switches the appearance of content. You can use it to add:
- Buttons that change their state (for example, the like icon when tapped).
- Content that can be expanded (for example, a bridge card or a geo block).
One picture changes to another, but the user sees it as an interaction.
To switch to a different state, use url
:
div-action://set_state?state_id=<div_data_state_id/div_id/state_id>&temporary=<bool>
The path is formed from the hierarchy root:
Parameter |
Description |
|
The path of the state inside Can be hierarchical: |
|
Indicates a state change:
|
View an interactive example
Empty state
If you need to hide a block, don't add a div
field, but specify "state_id": "empty"
.
View an interactive example
Nested states
If multiple states are nested in each other, write the full path from the root to switch between them:
div-action://set_state?state_id=[root state id]/[element div_id]/[state state_id]
View an interactive example
Actions when appearing
visibility elements are triggered when an element is shown on the screen.
{
"visibility_actions": [
{
"log_id":"content_01_visibility",
"visibility_percentage": 100,
"visibility_duration": 1000,
"url": "div-action://none"
}
]
}
Parameter |
Description |
|
Percentage of the element's visible area. |
|
The minimum time of its visibility. |
Loading more data
To load more data in the form of a patch and update the current element, specify the download
action:
div-action://download?url=<patch_url>
Parameter |
Description |
|
URL to get the patch. |
Response format:
{
"patch": {
// data in patch format
},
"templates": {
// templates that can be used in the patch section
}
}
Note
To use patches on Android platform you should provide implementation of DivDownloader
to your DivConfiguration
.
Switching elements
To navigate inside gallery, pager, and tabs, specify an action:
set_current_item
: For the current element.set_next_item
: For the next element.set_previous_item
: For the previous element.
div-action://set_current_item?id=<div_id>&item=<item_index>
div-action://set_next_item?id=<div_id>&overflow=<clamp|ring>
div-action://set_previous_item?id=<div_id>&overflow=<clamp|ring>
Parameter |
Description |
|
ID of the element in which the current one must be switched. |
|
The index of the item to be set as current. |
|
Specifies how navigation will occur when the boundary elements are reached:
|
Scroll to the end or to the start of gallery
To navigate to the end or to the start of gallery specify the action:
div-action://scroll_to_start?id=<div_id>
div-action://scroll_to_end?id=<div_id>
Parameter |
Description |
|
ID of the element in which the current one must be switched. |
If the items inside the gallery
are larger than the gallery
itself, the scroll_to_end
action will navigate to the end of the last item.
Scroll of gallery several dp forward or backward, as well as up to a specific value
To scroll gallery several dp forward or backward specify actions:
div-action://scroll_forward?id=<div_id>&step=<dp_count>&overflow=<clamp|ring>
div-action://scroll_backward?id=<div_id>&step=<dp_count>&overflow=<clamp|ring>
To scroll gallery to a specific value specify action:
div-action://scroll_to_position?id=<div_id>&step=<dp_count>
Parameter |
Description |
|
ID of the element in which the current one must be switched. |
|
Position in dp, to which you need to scroll or how much you need to scroll the gallery, depending on the action. |
|
Specifies how navigation will occur when the boundary elements are reached:
|
Managing tooltips
To show or hide tooltips, specify the show_tooltip
or hide_tooltip
action:
div-action://show_tooltip?id=<tooltip_id>
div-action://hide_tooltip?id=<tooltip_id>
Parameter |
Description |
|
Tooltip ID. |
Changing the values of variables
To change the value of variables, specify the set_variable
action:
div-action://set_variable?name=<variable_name>&value=<new_value>
Parameter |
Description |
|
Variable name. |
|
New value. |
Saving variables to persistent variable storage
To save a variable to the persistent storage, use the set_stored_value
action.
div-action://set_stored_value?name=<variable_name>&value=<new_value>&type=<variable_type>&lifetyme=<lifetime_in_sec>
Parameter |
Description |
|
Variable name. |
|
Value to save. |
|
Type of the variable. Should be one of |
|
Lifetime of the variable in seconds. |
To retrieve the value of a saved variable, use a valuable expression.
@{getStoredStringValue(name, default)}
@{getStoredNumberValue(name, default)}
@{getStoredIntegerValue(name, default)}
@{getStoredUrlValue(name, default)}
@{getStoredColorValue(name, default)}
The name
parameter is the name of the stored variable, and default
is the value to return if the variable does not exist or has expired.
Learn more
Follow DivKit news in the Telegram channel: http://t.me/divkit_news
You can also discuss topics of interest in the DivKit user community in Telegram: https://t.me/divkit_community_ru