当我滚动到底部时,libGDX ScrollPane会自动稍微向后滚动

时间:2014-11-25 23:40:02

标签: java libgdx scrollpane

我想在里面创建一个带有几个按钮的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      
}

非常感谢你的帮助!

1 个答案:

答案 0 :(得分:0)

试试这个。

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); 
}

你说什么不回来,呃从未看过,因为我喜欢回来。