Skip to main content

list

Function list 

Source
pub fn list(
    state: ListState,
    render_item: impl FnMut(usize, &mut Window, &mut App) -> AnyElement + 'static,
) -> List
Expand description

Creates a list element that renders rows of any height, using the state in state.

render_item receives the index of a row and returns its element. It runs only for rows in the viewport or the overdraw, unless the state uses ListState::measure_all, which renders every row. Keep your data on your view and index into it from render_item, because row elements are dropped as they scroll away.

The element sizes itself from its style by default. Use List::with_sizing_behavior to size it from its rows instead. For rows that are all the same height, use crate::uniform_list.

ยงExamples

struct Feed {
    state: ListState,
    rendered: Rc<Cell<usize>>,
}

impl Render for Feed {
    fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
        let rendered = self.rendered.clone();
        div().h(px(200.)).child(
            list(self.state.clone(), move |ix, _window, _cx| {
                rendered.set(rendered.get() + 1);
                div().h(px(50.)).child(format!("Row {ix}")).into_any_element()
            })
            .size_full(),
        )
    }
}
window.draw();
// A 200px viewport holds four 50px rows, so only about four rows were rendered.
assert!(rendered.get() > 0 && rendered.get() < 20);

window.update(|feed, _, _| feed.state.splice(1_000..1_000, 1));
window.draw();
window.read(|feed, _| assert_eq!(feed.state.item_count(), 1_001));