【问题标题】:libGDX ScrollPane slightly scrolls back automatically when I scroll to the bottom当我滚动到底部时,libGDX ScrollPane 会自动稍微向后滚动
【发布时间】:2014-11-25 23:40:01
【问题描述】:

我想创建一个里面有几个按钮的 ScrollPane。 ScrollPane 必须垂直滚动。

滚动到底部时遇到 2 个问题: 1)即使我滚动到最大值,最后一个按钮也不会完全显示,底部缺少一小部分。 2) 当我松开鼠标按钮时,滚动到底部后,ScrollPane 会向后滚动一点,最后一个按钮不再显示。

我无法在谷歌上找到与这些问题相关的任何内容。 当我滚动到底部时,如何强制 ScrollPane 完全显示最后一个按钮? 释放鼠标按钮后如何防止 ScrollPan 向后滚动?

这是picture illustrating the problem 的链接。 这是我使用的代码:

public class ShopScreen implements Screen{
final MyGdxGame game;
OrthographicCamera camera;

Stage stage;
ScrollPane scrollPane;
Table table;
Texture texture1, texture2, texture3, texture4, texture5, texture6;
ImageButton button1, button2, button3, button4, button5, button6;

public ShopScreen(final MyGdxGame gam){
    game = gam;
    Constants.ecran = EcranEnum.Shop;
    camera = new OrthographicCamera();
    camera.setToOrtho(false, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());

    stage = new Stage();
    table = new Table();
    scrollPane = new ScrollPane(table);

    texture1 = new Texture(Gdx.files.internal("Images/Option1.png"));
    texture2 = new Texture(Gdx.files.internal("Images/Option2.png"));
    texture3 = new Texture(Gdx.files.internal("Images/Option3.png"));
    texture4 = new Texture(Gdx.files.internal("Images/Option4.png"));
    texture5 = new Texture(Gdx.files.internal("Images/Option5.png"));
    texture6 = new Texture(Gdx.files.internal("Images/Option6.png"));

    button1 = new ImageButton(new Image(texture1).getDrawable());
    button2 = new ImageButton(new Image(texture2).getDrawable());
    button3 = new ImageButton(new Image(texture3).getDrawable());
    button4 = new ImageButton(new Image(texture4).getDrawable());
    button5 = new ImageButton(new Image(texture5).getDrawable());
    button6 = new ImageButton(new Image(texture6).getDrawable());

    table.setFillParent(true);
    table.defaults().width(Gdx.graphics.getWidth()/2.5f).height(Gdx.graphics.getHeight()/(8*Gdx.graphics.getHeight()/Gdx.graphics.getWidth()));
    table.add(button1).row();
    table.add(button2).row();
    table.add(button3).row();
    table.add(button4).row();
    table.add(button5).row();
    table.add(button6).row();

    scrollPane.setX(Gdx.graphics.getWidth()/2-scrollPane.getWidth()/2);
    scrollPane.setY(Gdx.graphics.getHeight()/2-scrollPane.getHeight()/2);


    button1.addListener(new ClickListener(){
         @Override
            public void clicked(InputEvent event, float x, float y) {
             System.out.println("Test");
         }
    });
}

@Override
public void render(float delta) {
    Gdx.gl.glClearColor(0.281f, 0.602f, 0.844f, 0);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);   

    stage.act();
    stage.draw();
}

@Override
public void resize(int width, int height) {
    // TODO Auto-generated method stub  
}

@Override
public void show() {
    stage.addActor(scrollPane);
    Gdx.input.setInputProcessor(stage); 
}

@Override
public void hide() {
    // TODO Auto-generated method stub      
}

@Override
public void pause() {
    // TODO Auto-generated method stub      
}

@Override
public void resume() {
    // TODO Auto-generated method stub      
}

@Override
public void dispose() {
    // TODO Auto-generated method stub      
}

非常感谢您的帮助!

【问题讨论】:

    标签: java libgdx scrollpane


    【解决方案1】:

    试试这个。

    public class ShopScreen implements Screen{
    
    final MyGdxGame game;
    OrthographicCamera camera;
    
    Stage stage;
    
    ScrollPane scrollPane;
    
    //container is new
    Table table, container;
    
    Texture texture1, texture2, texture3, texture4, texture5, texture6;
    ImageButton button1, button2, button3, button4, button5, button6;
    
    public ShopScreen(final MyGdxGame gam){
    
        game = gam;
        Constants.ecran = EcranEnum.Shop;
        camera = new OrthographicCamera();
        camera.setToOrtho(false, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
    
        stage = new Stage();
    
        //Add
    
        container = new Table();
    
        table = new Table();
    
        stage.addActor(container);
    
        container.setFillParent(true);
    
        final ScrollPane scrollPane = new ScrollPane(table);
    
            float myPadTop = Gdx.graphics.getHeight()/2-scrollPane.getHeight()/2;
            float myPadBottom = Gdx.graphics.getHeight()/2-scrollPane.getHeight()/2;
    
        scrollPane.setFlickScroll(true);
    
        container.add(scrollPane).padTop(myPadTop).padBottom(myPadBottom);
    
        table.pad(10).defaults().expandX().space(4);
    
        //
    
        texture1 = new Texture(Gdx.files.internal("Images/Option1.png"));
        texture2 = new Texture(Gdx.files.internal("Images/Option2.png"));
        texture3 = new Texture(Gdx.files.internal("Images/Option3.png"));
        texture4 = new Texture(Gdx.files.internal("Images/Option4.png"));
        texture5 = new Texture(Gdx.files.internal("Images/Option5.png"));
        texture6 = new Texture(Gdx.files.internal("Images/Option6.png"));
    
        button1 = new ImageButton(new Image(texture1).getDrawable());
        button2 = new ImageButton(new Image(texture2).getDrawable());
        button3 = new ImageButton(new Image(texture3).getDrawable());
        button4 = new ImageButton(new Image(texture4).getDrawable());
        button5 = new ImageButton(new Image(texture5).getDrawable());
        button6 = new ImageButton(new Image(texture6).getDrawable());
    
        //table.setFillParent(true);//Remove
             //table.defaults().width(Gdx.graphics.getWidth()/2.5f).height(Gdx.graphics.getHeight()/(8*Gd//x.graphics.getHeight()/Gdx.graphics.getWidth()));//Remove
    
        table.add(button1).row();
        table.add(button2).row();
        table.add(button3).row();
        table.add(button4).row();
        table.add(button5).row();
        table.add(button6).row();
    
        //scrollPane.setX(Gdx.graphics.getWidth()/2-scrollPane.getWidth()/2);//Remove
        //scrollPane.setY(Gdx.graphics.getHeight()/2-scrollPane.getHeight()/2);//Remove
    
    
        button1.addListener(new ClickListener(){
             @Override
                public void clicked(InputEvent event, float x, float y) {
                 System.out.println("Test");
             }
        });
    }
    
    @Override
    public void render(float delta) {
        Gdx.gl.glClearColor(0.281f, 0.602f, 0.844f, 0);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);   
    
        stage.act(Gdx.graphics.getDeltaTime());
        stage.draw();
    }
    
    @Override
    public void resize(int width, int height) {
        // TODO Auto-generated method stub  
    }
    
    @Override
    public void show() {
        //stage.addActor(scrollPane);
        Gdx.input.setInputProcessor(stage); 
    }
    

    你说的不回来,嗯从来没看过,因为我喜欢回来。

    【讨论】:

      猜你喜欢
      • 2016-09-02
      • 1970-01-01
      • 2013-04-22
      • 2013-01-11
      • 2021-05-29
      • 2011-08-17
      • 1970-01-01
      • 2014-12-12
      • 2016-11-13
      相关资源
      最近更新 更多