Description
I have a screen(or other container) with a bunch of widgets. Some of the widgets are only available under certain conditions. Sometimes when I hide them, the user is stranded because there are no more scrollable widgets on screen.
What MCU/Processor/Board and compiler are you using?
I’m using an ESP32-S3, custom board, Xtensa GCC, but I’ve mocked up the same behaviour in micropython.
What LVGL version are you using?
8.3.11
What do you want to achieve?
I would like to trigger LVGL to scroll a screen or container so that some components are visible, ideally without manually selecting the best widget to scroll to myself.
What have you tried so far?
I’ve used lv_obj_scroll_to_view(top widget)
, but that’s not great if the user was in a place where this scrolling was not required.
Code to reproduce
# Initialize: LVGL v8.3
import display_driver
import lvgl as lv
NUM_BUTTONS = 20
START_HIDE_BUTTONS = 10
#Create a Screen with COLUMN flex direction*/
scr = lv.obj()
scr.set_size(200,150)
scr.set_flex_flow(lv.FLEX_FLOW.COLUMN)
# Event handler function
def btn_event_cb(evt):
code = evt.get_code()
if code == lv.EVENT.CLICKED:
print("Button Clicked: hiding some buttons")
for i in range(START_HIDE_BUTTONS, NUM_BUTTONS):
btns[i].add_flag(lv.obj.FLAG.HIDDEN)
###MY QUESTION###
#What Can I put here to scroll 'scr' so that it contains
#a reasonable number of Items?
# Build a list of buttons
btns = []
for i in range(NUM_BUTTONS):
btn = lv.btn(scr)
btn.set_size(lv.pct(100), lv.SIZE.CONTENT)
btn.align(lv.ALIGN.CENTER, 0, 0)
lbl = lv.label(btn)
lbl.center()
# Add event callback to the last button
if i + 1 == NUM_BUTTONS:
btn.add_event_cb(btn_event_cb, lv.EVENT.ALL, None)
scr.scroll_to_view(lv.ANIM.ON)
lbl.set_text("ClickToHideSome")
else:
lbl.set_text("Item: {:d}".format(i))
btns.append(btn)
lv.scr_load(scr)
Screenshot and/or video
To start off, scroll to the bottom:
After clicking ‘ClickToHideSome’(which hides items 10 and further, but scroll hasn’t been updated, so user is lost):
So at this point, I’m looking for a snippet of code that will cause LVGL to scroll up, or down, until the available widgets are within the screen. For this example, it should scroll to here(if we were scrolled where item -100 should be, function should scroll until Item:0 is at the top instead) :