将 ArrayList 从 android 模块传递到 libgdx 中的核心

pass ArrayList from android module to core in libgdx

我正在使用 libgdx。我需要将 TaskSet 的 ArrayList 从 android 传递到核心。问题是 TaskSet 位于 android 模块中。我可以通过这种方式传递一些标准对象,例如字符串:

public class DragAndDropTest extends ApplicationAdapter {
......
    public DragAndDropTest(String value){
        this.value=value;
    }
......
}

在 AndroidLauncher 中:

AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
                LinearLayout lg=(LinearLayout) findViewById(R.id.game);
                lg.addView(initializeForView(new DragAndDropTest("Some String"), config));

它工作正常,但我需要传递 TaskSet 的 ArrayList,TaskSet 在 android 模块中

我知道不好的解决方案是将 TaskSet 放在 "core" 模块中,但无论如何我需要一些方法来与 android 部分

进行交互

如果按照您要求的方式执行此操作,您将无法维护多平台功能。这也意味着您将无法在桌面上进行测试。这将花费您大量时间编译和加载 Android APK 到设备上。

但是您应该能够通过将 android 块中的所有内容剪切并粘贴到项目 build.gradle 文件中的 core 块来实现。它看起来像这样:

project(":core") {
    apply plugin: "java"
    apply plugin: "android"

    configurations { natives }

    dependencies {
        compile "com.badlogicgames.gdx:gdx:$gdxVersion"
        compile "com.badlogicgames.gdx:gdx-backend-android:$gdxVersion"        
        natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-x86"
        natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-armeabi"
        natives "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-armeabi-v7a"
    }
}

但正如我所说,这可能不是您想要做的。我建议使用一个接口,以便处理 TaskSet 的所有代码都保留在 Android 模块中。像这样:

public interface PlatformResolver {
    public void handleTasks();
}

-

public class MyGame extends ApplicationAdapter {
    //......

    PlatformResolver platformResolver;

    public MyGame (PlatformResolver platformResolver){
        this.platformResolver = platformResolver;
    }

    //.....
    public void render(){
        //...

        if (shouldHandleTasks) platformResolver.handleTasks();

        //...
}

-

public class AndroidLauncher extends AndroidApplication implements PlatformResolver {

    public void handleTasks(){
        //Do stuff with TaskSets
    }

    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        someDataType SomeData;

        AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
        // config stuff
        initialize(new MyGame(this), config);
    }

}

-

public class DesktopLauncher  implements PlatformResolver{

    public void handleTasks(){
        Gdx.app.log("Desktop", "Would handle tasks now.");
    } 

    public static void main (String[] arg) {
        LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
        config.title = "My GDX Game";
        config.width = 480;
        config.height = 800;
        new LwjglApplication(new MyGame(this), config);
    }
}