如何将ScrollPane的滚动条位置固定在特定位置,使其显示例如其子表的最后一个项目而不是表的第一个项目(默认)?我尝试使用setScrollX和setScrollY但它不起作用。
我有一个包含许多级别的游戏等级菜单,并且当打开该菜单时,我希望向用户显示上次解锁的最后一个级别。
如何将ScrollPane的滚动条位置固定在特定位置,使其显示例如其子表的最后一个项目而不是表的第一个项目(默认)?我尝试使用setScrollX和setScrollY但它不起作用。
我有一个包含许多级别的游戏等级菜单,并且当打开该菜单时,我希望向用户显示上次解锁的最后一个级别。
在尝试调用setScrollX
或setScrollY
之前,您可能需要手动调用layout()
来设置ScrollPane。
如果你想要避免滚动窗格的飞快滚动效果,可以在show()方法中使用以下代码,这样滚动窗格会立即显示你指定的确切位置。
scrollPane.layout();
scrollPane.setScrollPercentY(.5f);
scrollPane.updateVisualScroll();
除了设置scrollPercent之外,您还可以使用
scrollPane.scrollTo(x, y, width, height);
或者
scrollPane.setScrollY(y);
这对我有用:
scrollPane.layout();
scrollPane.setScrollPercentY(100);
setScrollPercentY(100);
scrollPane.layout()
,滚动窗格仍然具有-2、0或其他高度(scrollHeight
)。只有在后续布局调用期间才会设置正确的高度,从而导致内容从顶部滑动的动画。ScrollPane.layout()
并在每次布局更改后向底部滚动:open class BottomScrollingScrollPane(actor: Actor?, skin: Skin, style: String): ScrollPane(actor, skin, style) {
override fun layout() {
super.layout()
scrollTo(0f, 0f, 0f, 0f)
updateVisualScroll()
}
}
...并且如果您使用libktx:
@Scene2dDsl
@OptIn(ExperimentalContracts::class)
inline fun <S> KWidget<S>.bottomScrollingScrollPane(
style: String = defaultStyle,
skin: Skin = Scene2DSkin.defaultSkin,
init: KBottomScrollingScrollPane.(S) -> Unit = {}
): KBottomScrollingScrollPane {
contract { callsInPlace(init, InvocationKind.EXACTLY_ONCE) }
return actor(KBottomScrollingScrollPane(skin, style), init)
}
class KBottomScrollingScrollPane(skin: Skin, style: String) : BottomScrollingScrollPane(null, skin, style), KGroup {
override fun addActor(actor: Actor?) {
this.actor == null || throw IllegalStateException("ScrollPane may store only a single child.")
this.actor = actor
}
}
一个副作用是在每次布局更改、调整大小等操作后再次向下滚动,但对我来说这没关系,因为布局的寿命非常短暂。
这段代码设置了ScrollPane的位置、宽度和高度。
scroll.setBounds(0, 60, Gdx.graphics.getWidth(), 200);