Android

关注公众号 jb51net

关闭
首页 > 软件编程 > Android > Libgdx ScrollPane

详解Android Libgdx中ScrollPane和Actor事件冲突问题的解决办法

投稿:lqh

这篇文章主要介绍了详解Android Libgdx中ScrollPane和Actor事件冲突问题的解决办法的相关资料,希望通过本文能帮助到大家,需要的朋友可以参考下

详解Android Libgdx中ScrollPane和Actor事件冲突问题的解决办法

在Libgdx的使用过程中,经常会用到ScrollPane这个widget,来实现滑动效果, 如下所示:

但是如果想在上面的效果上添加一点扩展,比如ScrollPane中的Actor可以从ScrollPane中移出来,并添加到Stage中,则需要添加额外的逻辑

具体代码参考如下:

/**
 * Created by Danny.姜 on 17/7/26.
 */

public class TestAdapter extends ApplicationAdapter {

  Stage stage;

  ScrollPane scrollPane;

  Table table;

  private float lastStageX;
  private float lastStageY;

  ActorGestureListener actorGestureListener;

  private InputListener inputListener = new InputListener() {
    @Override
    public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
      Log.e("DANNY", "actor touch down");
      if (pointer == 0) {
        lastStageX = event.getStageX();
        lastStageY = event.getStageY();

        inStage = false;
      }

      return true;
    }

    @Override
    public void touchDragged(InputEvent event, float x, float y, int pointer) {
      Log.e("DANNY", "actor touch dragging");

      Actor actor = event.getListenerActor();

      if (cancelTouchFocus) {

        float currentX = event.getStageX();
        float currentY = event.getStageY();

        actor.moveBy(currentX - lastStageX, currentY - lastStageY);

        lastStageX = currentX;
        lastStageY = currentY;

        if (!inStage) {
          Vector2 vector2 = actor.localToStageCoordinates(new Vector2());

          actor.setPosition(vector2.x, vector2.y);

          stage.addActor(actor);

          stage.cancelTouchFocusExcept(this, actor);

          inStage = true;
        }
      }
    }

    @Override
    public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
      Log.e("DANNY", "actor touch up");

      cancelTouchFocus = false;
    }
  };

  @Override
  public void create() {
    super.create();

    stage = new Stage();
    stage.setDebugAll(true);
    Gdx.input.setInputProcessor(stage);

    actorGestureListener = new ActorGestureListener() {
      @Override
      public boolean longPress(Actor actor, float x, float y) {
        Log.e("DANNY", "actor gesture listener long pressed");

        cancelTouchFocus = true;

        return super.longPress(actor, x, y);
      }
    };

    table = new Table();

    scrollPane = new ScrollPane(table, new ScrollPane.ScrollPaneStyle());
    scrollPane.setSize(stage.getWidth(), 500);
    scrollPane.setScrollingDisabled(false, true);
    scrollPane.setCancelTouchFocus(false);
    scrollPane.setSmoothScrolling(true);
    scrollPane.setFlingTime(0);

    stage.addActor(scrollPane);

    initTable();
  }

  private boolean cancelTouchFocus = false;

  private boolean inStage = false;

  private void initTable() {
    for (int i = 0; i < 10; i++) {
      BaseGdxActor actor = new BaseGdxActor(new TextureRegion(
          new Texture("badlogic.jpg")));
      table.add(actor).width(actor.getWidth()).pad(10);

      actor.addListener(actorGestureListener);
      actor.addListener(inputListener);
    }
  }

  @Override
  public void render() {
    super.render();

    Gdx.gl.glClearColor(1, 1, 1, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

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

}

实现效果如下:

如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

您可能感兴趣的文章:
阅读全文