3

How to fix a ScrollPane scroll bar position at a specific position so that it displays for example the last item of it's child Table instead of the first item Table(default)? I try setScrollX and setScrollY but it doesn't work.

I have a game level menu(with many levels) and i would like to display the last unlocked level to the user when opening that menu.

Neerkoli
  • 2,423
  • 3
  • 16
  • 21
Mak
  • 123
  • 1
  • 9

6 Answers6

5

You might have to call layout() on the ScrollPane yourself manually before trying to call setScrollX or setScrollY

codeulike
  • 22,514
  • 29
  • 120
  • 167
3

When you want to avoid the ScrollPanes fling effect, use the following code in your show() method, so the ScrollPane immediately shows the exact position you specified.

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

Instead of setting the scrollPercent, you could also use

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

or

scrollPane.setScrollY(y);
mtosch
  • 351
  • 4
  • 18
2

This works for me if you've got a vertically stacked pane:

setScrollPercentY(100);
freddy.smith
  • 451
  • 4
  • 9
2

This worked for me:

scrollPane.layout();
scrollPane.setScrollPercentY(100);
Julien
  • 1,028
  • 9
  • 18
2

For me, all the previous answers did not work: Even after calling scrollPane.layout() multiple times, the scroll pane still had an areaHeight (scrollHeight) of -2, 0, or sth else. Only later during subsequent layout calls the correct height would be set, causing an animation where the content would slide in from the top.

The only option that fixed it for my use case (scrolling to bottom initially after adding all the initial content) was to override the ScrollPane.layout() and scroll to bottom after each layout change:

open class BottomScrollingScrollPane(actor: Actor?, skin: Skin, style: String): ScrollPane(actor, skin, style) {

    override fun layout() {
        super.layout()
        scrollTo(0f, 0f, 0f, 0f)
        updateVisualScroll()
    }
}

... and optionally, if you use 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
  }
}

One side effect is scrolling down again after each layout change, resize etc., but that's fine for me as the layout is very short-lived anyway.

tommi
  • 84
  • 3
1

this code is set the position, width and height of ScrollPane.

scroll.setBounds(0, 60, Gdx.graphics.getWidth(), 200);
Abhinav singh
  • 1,448
  • 1
  • 14
  • 31