Search code examples
libgdxscrollpane

LibGDX scrollpane disable scrolling x


I have a scrollpane in libgdx. Scrolling up and down is enabled(with touches), and i want to scroll it to left and right with a button, but disable scrolling with touches.

'rightButton.addListener(new ChangeListener() {
 public void changed (ChangeListener.ChangeEvent event, Actor actor) {
 scrollPane.layout();
 scrollPane.setScrollingDisabled(false, false);
 scrollPane.setScrollX(scrollPane.getMaxX());
 scrollPane.setScrollingDisabled(false, false);
 scrolled = true;
 }
 });
'

Here is my RightButton listener, but it doesnt scroll the X.


Solution

  • scrollTo() is overloaded method of ScrollPane, you can use this method and scroll your pane to specified offset. Call this method in your Listener's method.

    I have tested, you can test my code.

    public class MyGdxGame extends ApplicationAdapter {
    
        Texture img;
        Stage stage;
        ScrollPane scrollPane;
        Group group;
    
        @Override
        public void create () {
    
            img = new Texture("badlogic.jpg");
            stage=new Stage();
            Gdx.input.setInputProcessor(stage);
    
            final float w=Gdx.graphics.getWidth();
            final float h=Gdx.graphics.getHeight();
    
            group=new Group();
            group.setSize(2*w,h);
            scrollPane=new ScrollPane(group);
            scrollPane.setSize(w,h);
            scrollPane.setFlickScroll(false);
    
            stage.addActor(scrollPane);
    
            Image image1=new Image(img);
            image1.setPosition(w*.5f, h/2f);
            image1.setScale(.5f);
            group.addActor(image1);
    
            Image image2=new Image(img);
            image2.setPosition(w*.75f, h/3f);
            image2.setScale(.5f);
            group.addActor(image2);
    
            stage.addListener(new ClickListener(){
    
                @Override
                public void clicked(InputEvent event, float x, float y) {
    
                    float offsetPositionX=w*.5f;
                    float offsetPositionY=0;
    
                    scrollPane.scrollTo(offsetPositionX,offsetPositionY,w,h);
    
                    super.clicked(event, x, y);
                }
            });
        }
    
        @Override
        public void render () {
            Gdx.gl.glClearColor(1, 0, 0, 1);
            Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
            stage.act();
            stage.draw();
        }
    
        @Override
        public void dispose () {
            img.dispose();
            stage.dispose();
        }
    }