LibGdx如何将ScrollPane滚动条位置固定在特定位置?

3

如何将ScrollPane的滚动条位置固定在特定位置,使其显示例如其子表的最后一个项目而不是表的第一个项目(默认)?我尝试使用setScrollX和setScrollY但它不起作用。

我有一个包含许多级别的游戏等级菜单,并且当打开该菜单时,我希望向用户显示上次解锁的最后一个级别。

6个回答

5

在尝试调用setScrollXsetScrollY之前,您可能需要手动调用layout()来设置ScrollPane。


3

如果你想要避免滚动窗格的飞快滚动效果,可以在show()方法中使用以下代码,这样滚动窗格会立即显示你指定的确切位置。

scrollPane.layout(); 
scrollPane.setScrollPercentY(.5f);
scrollPane.updateVisualScroll();

除了设置scrollPercent之外,您还可以使用

scrollPane.scrollTo(x, y, width, height);

或者

scrollPane.setScrollY(y);

2
这对我有用:

这对我有用:

scrollPane.layout();
scrollPane.setScrollPercentY(100);

2
如果您的面板是垂直排列的,那么这对我很有用:
setScrollPercentY(100);

2
对于我来说,之前的所有答案都没有起作用: 即使多次调用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
  }
}

一个副作用是在每次布局更改、调整大小等操作后再次向下滚动,但对我来说这没关系,因为布局的寿命非常短暂。


1

这段代码设置了ScrollPane的位置、宽度和高度。

scroll.setBounds(0, 60, Gdx.graphics.getWidth(), 200);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接