rainer
rainer

Reputation: 3411

Codename One Transition Form Automatically Scrolls to Top

I have an app with lots of components that use slide transitions.

The components, ScaleImageButtons and -Labels are added to the form directly via a GridLayout. There are also timer events involved, to make the scenario more dynamic and appealing

The form is, of course, scrollable.

Now, every time I call the form and scroll it down, it automatically jumps back to the first item at the top of the form, once that becomes animated.

But the form should stay at the current scroll position, independent of the position of the animated item, being this visible or not.

How can can make the form keep the current scroll position, independent of the position of the currently animated item?

 formApps.setFocusScrolling(false); // does not help

Here is the code:

(THIS CODE WAS EDITED TO REFLECT THE WORKING SOLUTION)

Form formApps = new Form(new GridLayout(6, 2)); 
ArrayList<String> listApps = classStrings.listApps;

for (int i = 0; i < listApps.size(); i++) {

     Container container = new Container();
     ScaleImageButton scaleImageButton = new ScaleImageButton(image);
     SpanButton spanButton = new SpanButton(stringDescrip);

     container.add(scaleImageButton);

     UITimer uit00 = new UITimer(() -> {

        UITimer uit01 = new UITimer(() -> {

            container.replace(spanButton, scaleImageButton,
                    CommonTransitions.createCover(CommonTransitions.SLIDE_HORIZONTAL, true, slideInterval));

        });
        uit01.schedule(startInterval, false, formApps);

        UITimer uit02 = new UITimer(() -> {

            container.replace(scaleImageButton, spanButton,
                    CommonTransitions.createCover(CommonTransitions.SLIDE_HORIZONTAL, true, slideInterval));

        });
        uit02.schedule(repeatIntervalOne, false, formApps);

    });
    uit00.schedule(repeatIntervalOne, true, formApps);
});

form.setFocusScrolling(false);
form.add(container);
container.setScrollableY(true);
formApps.show();

Upvotes: 1

Views: 82

Answers (1)

Shai Almog
Shai Almog

Reputation: 52760

You shouldn't use Timer here directly. That violates the EDT. You should either wrap the timer callback in callSerially or better yet just use a UITimer which is designed exactly with this purpose in mind.

6x2 is a relatively small grid so it's possible that a small movement is enough to send you to the top as elements get resized. The size of the grid is determined by the preferred size of all the elements within. If the scaleImageButton is much larger than spanButton once they are replaced the layout might "jump" and you will end up with something much smaller which feels like scrolling but is just a new layout due to smaller preferred size.

A workaround for this would be:

Component.setSameSize(scaleImageButton, spanButton);

Upvotes: 1

Related Questions