LibGDX getYpercent of a child inside scrollpane

So I have this scrollpane with a bunch of labels alfabetic organized, its quite anoying scrolling all the way down to select a label starting with Z letter

So I put the whole alfabet in the right of the screen and my goal is that when users touch the letter 'X' it scrolls all the way to the names starting with 'X'

So I hard coded it like this...

if(letter.equalsIgnoreCase("a"))      setSPy(0);
else if(letter.equalsIgnoreCase("b")) setSPy(0.112212256f);
else if(letter.equalsIgnoreCase("c")) setSPy(0.21482526f);
else if(letter.equalsIgnoreCase("d")) setSPy(0.32874277f);
//etc...
private void setSPy(float percent) {
scrollPane.setScrollPercentY(percent);
}


It works, but 2 problems:

it looks ugly and I refuse update it every time I decide to add another label.

Then since I could not find a function that tells me the percentY of the item I did this:

private void scrollToLetter(String letter) {
//loop tru items inside scrollpane
for (int i = 0; i < spItems.size; i++) {
BrandSelectorItem  item = spItems.get(i);

if(item.getString().substring(0, 1).equals(letter)){
int number = i+1;
//This gets the posY axis-Y starting on top
float posY = (number*item.getHeight())- item.getHeight();
float percentY = posY/(spItems.size*item.getHeight());

scrollPane.setScrollPercentY(percentY);
break;
}
}
}


I thought this was going work, but if I hover the "A" works fine, go to 0%, if I hover the "B" its off by little bit, "C" more off, "D" more more off and etc..

seems like something about scrollpane coordinates that i do not understand.

Thank you!

Solution:

 private void scrollToLetter(String letter) {
//loop tru items inside scrollpane
for (int i = 0; i < spItems.size; i++) {
//This is the label
BrandSelectorItem  item = spItems.get(i);
if(item.getString().substring(0, 1).equals(letter)){
int itemNumber = i+1;
//This gets the posY axis-Y starting on top
float posY = (itemNumber*item.getHeight())- item.getHeight();
scrollPane.setScrollY(posY);
break;
}
}
}


i'd do something like this:

Create a key-value map, where key - is character, supposed to be first letters of labels-leaders (e.g. first occurrence for label text starting with 'b' from the list sorted alphabetically);

and value - is the reference to the label object

private ObjectMap<Character, Label> labels = new ObjectMap<Character, Label>();

private void createLabels() {
labels.put('a', new Label("a_label", new Skin()));
labels.put('b', new Label("b_label", new Skin()));
labels.put('c', new Label("c_label", new Skin()));
labels.put('d', new Label("d_label", new Skin()));
}


* note I'm creating new label objects just to demonstrate, here should be references to earlier created labels.

method getLabelPosition searching up the labels map by letter and returns the label position:

private float getLabelPosition(char letter) {
Label label = labels.get(letter);
return label.getY();
}

public void navigateTo(char inputLetter) {
pane.scrollY(getLabelPosition(inputLetter));
}


method navigateTo expected to be called when user pressed a letter. It does the actual focus move to the coordinates corresponding to the first label with the pressed letter.

• Im sorry but i think i miss the part when you actually set the scrollpane percent wich was the question? Unless navigateToLabel is a a already made method? Dec 13 '16 at 20:23
• Or you forgot to include navigateToLabel() code? Dec 13 '16 at 20:27
• oops, sorry, try something like scrollY(float pixelsY) or scrollTo(float x, float y, float width, float height) method, I'll update the answer Dec 13 '16 at 22:55
• Thanks.. my code with the scrollY(float pixelsY)  did the trick, dont know how I did not see that method. Dec 13 '16 at 23:00