1

我正在尝试制作简单的 android 应用程序,该应用程序使用 proteus 插件从 json 文件创建动态视图。我在 proteus 充气机中传递布局和数据时遇到问题。如果有人可以帮助我了解如何将数据传递给 proteus 充气机?

4

2 回答 2

0

布局和数据都作为 JSON 对象传递给 proteus 充气机。因此,如果您使用任何 Web 服务,则必须将布局和数据作为 JSON 对象接收,然后使用创建视图proteusLayoutInflater.inflate(<layout>, <data>),然后将视图添加到 ViewGroup。

于 2019-06-28T11:55:20.317 回答
0

试试下面的onCreate代码Activity

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);  // A FrameLayout

final String LAYOUT = "{\n" +
"  \"type\": \"TextView\",\n" +
"  \"textSize\": \"28sp\",\n" +
"  \"text\": \"Hello World!\"\n" +
"}";
final String DATA = "{}";

ViewGroup container = findViewById(R.id.container); // container is the FrameLayout

// create a new instance of proteus
Proteus proteus = new ProteusBuilder().build();

// register proteus with a ProteusTypeAdapterFactory to deserialize proteus jsons
ProteusTypeAdapterFactory adapter = new ProteusTypeAdapterFactory(this);
ProteusTypeAdapterFactory.PROTEUS_INSTANCE_HOLDER.setProteus(proteus);

// deserialize layout and data
Layout layout;
ObjectValue data;
try {
  layout = adapter.LAYOUT_TYPE_ADAPTER.read(new JsonReader(new StringReader(LAYOUT)));
  data = adapter.OBJECT_TYPE_ADAPTER.read(new JsonReader(new StringReader(DATA)));
} catch (IOException e) {
  throw new RuntimeException(e);
}

// create a new ProteusLayoutInflater
ProteusContext context = proteus.createContextBuilder(this).build();
ProteusLayoutInflater inflater = context.getInflater();

// Inflate the layout
ProteusView view = inflater.inflate(layout, data, container, 0);

// Add the inflated layout into the container
container.addView(view.getAsView());

查看simple-proteus-demo

于 2019-07-02T15:37:39.157 回答