欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2024-02-11 18:03:22
详解android libgdx中scrollpane和actor事件冲突问题的解决办法 在libgdx的使用过程中,经常会用到scrollpane这个widget,来实...

详解android libgdx中scrollpane和actor事件冲突问题的解决办法

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

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

但是如果想在上面的效果上添加一点扩展,比如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();
  }

}

实现效果如下:

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

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