pub struct ListState(/* private fields */);Expand description
重导出所有内置元素类型 列表视图必须为列表元素持有的状态。
Implementations§
Source§impl ListState
impl ListState
Sourcepub fn new(
item_count: usize,
alignment: ListAlignment,
overdraw: Pixels,
) -> Self
pub fn new( item_count: usize, alignment: ListAlignment, overdraw: Pixels, ) -> Self
构造一个新的列表状态,用于存储在视图上。
overdraw 参数控制可见区域上方和下方渲染的额外空间量。 此区域内的元素即使不可见也会被测量。这有助于确保 列表在滚动时不会闪烁或突然出现。
Sourcepub fn measure_all(self) -> Self
pub fn measure_all(self) -> Self
设置列表在第一布局阶段测量所有项。
这对于确保滚动条大小正确(而不是仅基于已渲染元素)很有用。
Sourcepub fn with_uniform_item_height(self, height: Pixels) -> Self
pub fn with_uniform_item_height(self, height: Pixels) -> Self
为每个未测量的项预填充统一的高度提示,使滚动条滑块 从第一帧开始就能正确调整大小,无需预先测量所有项。
当项实际渲染时,它们的真实高度会替换提示,因此滚动条
随时间收敛到精确大小。这是 Self::measure_all 的更廉价替代方案,
适用于项高度大致统一的列表(如表格行)。
Sourcepub fn reset_with_uniform_height(&self, element_count: usize, height: Pixels)
pub fn reset_with_uniform_height(&self, element_count: usize, height: Pixels)
重置列表为 element_count 项,为每个项预填充统一的高度提示,
使滚动条滑块从第一帧开始就能正确调整大小,即使对于屏幕外的项也是如此。
Sourcepub fn remeasure_items(&self, range: Range<usize>)
pub fn remeasure_items(&self, range: Range<usize>)
将 range 中的项标记为需要重新测量,同时保持
当前滚动位置。与 Self::splice 不同,此方法不会
更改项的数量或清除 logical_scroll_top。
当项的内容已更改且其渲染高度可能不同时使用此方法 (如流式文本、工具结果加载),但项本身仍在同一索引处。
Sourcepub fn item_count(&self) -> usize
pub fn item_count(&self) -> usize
列表中的项数量。
Sourcepub fn is_scrolled_to_end(&self) -> Option<bool>
pub fn is_scrolled_to_end(&self) -> Option<bool>
列表是否已滚动到末尾,如果列表不可滚动或总内容高度尚不确定则返回 None。
Sourcepub fn splice(&self, old_range: Range<usize>, count: usize)
pub fn splice(&self, old_range: Range<usize>, count: usize)
通知列表状态 old_range 中的项已被
count 个需要重新计算的新项替换。
Sourcepub fn splice_focusable(
&self,
old_range: Range<usize>,
focus_handles: impl IntoIterator<Item = Option<FocusHandle>>,
)
pub fn splice_focusable( &self, old_range: Range<usize>, focus_handles: impl IntoIterator<Item = Option<FocusHandle>>, )
向列表状态注册 old_range 中的项已被新项替换。
与 Self::splice 不同,此方法允许提供可选焦点句柄的迭代器
以正确集成列表中可聚焦的项。如果聚焦的项滚动到视图外,
列表将继续渲染它以允许键盘交互。
Sourcepub fn set_scroll_handler(
&self,
handler: impl FnMut(&ListScrollEvent, &mut Window, &mut App) + 'static,
)
pub fn set_scroll_handler( &self, handler: impl FnMut(&ListScrollEvent, &mut Window, &mut App) + 'static, )
设置列表滚动时调用的处理程序。
Sourcepub fn logical_scroll_top(&self) -> ListOffset
pub fn logical_scroll_top(&self) -> ListOffset
获取当前滚动偏移量,以列表项为单位。
Sourcepub fn scroll_to_end(&self)
pub fn scroll_to_end(&self)
将列表滚动到最末尾(超过最后一项)。
与 [scroll_to_reveal_item] 不同,此方法使用总项数作为锚点,
因此列表的布局遍历会从末尾反向进行,始终显示最后一项的底部——
即使该项仍在增长(例如流式传输期间)。
Sourcepub fn set_follow_mode(&self, mode: FollowMode)
pub fn set_follow_mode(&self, mode: FollowMode)
设置列表的跟随模式。在 Tail 模式下,列表
将自动滚动到末尾,并在用户滚动回底部时重新激活。
在 Normal 模式下,不发生自动跟随。
Sourcepub fn is_following_tail(&self) -> bool
pub fn is_following_tail(&self) -> bool
返回列表当前是否正在主动跟随尾部(在每次布局时吸附到末尾)。
Sourcepub fn scroll_to(&self, scroll_top: ListOffset)
pub fn scroll_to(&self, scroll_top: ListOffset)
将列表滚动到指定偏移量
Sourcepub fn scroll_to_reveal_item(&self, ix: usize)
pub fn scroll_to_reveal_item(&self, ix: usize)
滚动列表以显示指定项,使其完全可见。
Sourcepub fn scrollbar_drag_started(&self)
pub fn scrollbar_drag_started(&self)
当用户开始拖动滚动条时调用此方法。
这将防止报告给滚动条的高度在拖动期间发生变化, 因为 overdraw 中的项会被测量,并帮助相应地偏移滚动位置更改。
Sourcepub fn scrollbar_drag_ended(&self)
pub fn scrollbar_drag_ended(&self)
当用户停止拖动滚动条时调用。
参见 scrollbar_drag_started。
Sourcepub fn is_scrollbar_dragging(&self) -> bool
pub fn is_scrollbar_dragging(&self) -> bool
如果滚动条当前正在被拖动则返回 true。
在 scrollbar_drag_started
和 scrollbar_drag_ended 调用之间设置。
对于需要区分滚动条拖动和滚轮/触控板滚动的消费者很有用,
例如在手动定位期间抑制自动滚动行为。
Sourcepub fn set_offset_from_scrollbar(&self, point: Point<Pixels>)
pub fn set_offset_from_scrollbar(&self, point: Point<Pixels>)
设置来自滚动条的偏移量
Sourcepub fn max_offset_for_scrollbar(&self) -> Point<Pixels>
pub fn max_offset_for_scrollbar(&self) -> Point<Pixels>
返回根据已测量项计算的最大滚动偏移量。 拖动期间此值保持不变,以防止滚动条意外移动。
Sourcepub fn scroll_px_offset_for_scrollbar(&self) -> Point<Pixels>
pub fn scroll_px_offset_for_scrollbar(&self) -> Point<Pixels>
返回经滚动条调整后的当前滚动偏移量。
返回的偏移量具有负 y 分量,表示
内容已滚动的距离。
Sourcepub fn viewport_bounds(&self) -> Bounds<Pixels>
pub fn viewport_bounds(&self) -> Bounds<Pixels>
返回视口的像素边界。
Sourcepub fn item_is_above_viewport(&self, ix: usize) -> Option<bool>
pub fn item_is_above_viewport(&self, ix: usize) -> Option<bool>
返回项是否完全在视口上方,如果列表尚未测量足够的布局则返回 None。
零高度视口仍能给出确定答案:调用方可能根据此查询调整
兄弟 UI 的尺寸(可能将列表本身压缩为零高度),
因此在此情况下返回 None 会导致答案在帧间振荡。
Sourcepub fn item_is_below_viewport(&self, ix: usize) -> Option<bool>
pub fn item_is_below_viewport(&self, ix: usize) -> Option<bool>
返回项是否完全在视口下方,如果列表尚未测量足够的布局则返回 None。
参见 Self::item_is_above_viewport 了解为何零高度视口
仍能给出确定答案。
Trait Implementations§
Auto Trait Implementations§
impl !RefUnwindSafe for ListState
impl !Send for ListState
impl !Sync for ListState
impl !UnwindSafe for ListState
impl Freeze for ListState
impl Unpin for ListState
impl UnsafeUnpin for ListState
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more