二手变形杆菌在Android碎片,但得到一个错误,而铸造吹气 - 变形的Android布局引擎

请检查我的代码如下:二手变形杆菌在Android碎片,但得到一个错误,而铸造吹气 - 变形的Android布局引擎

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

JSONObject jsonlayout = null ;

try {

jsonlayout = new JSONObject(loadJSONFromAsset(getContext()));

} catch (JSONException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

ProteusLayoutInflater layoutInflater = (ProteusLayoutInflater)inflater;

ProteusView view = layoutInflater.inflate(jsonlayout.toString(), null, container, 0);

return view.getAsView();

}

我想用变形库创建一个视图,使其作为片段。

你能告诉我我做错了什么吗?有了这个代码,我发现了以下错误:

com.android.internal.policy.PhoneLayoutInflater cannot be cast to com.flipkart.android.proteus.ProteusLayoutInflater

回答:

下面是Fragment一些示例代码,应该让你开始:

remember to add gson-adapter to the build.gradle file of your app.

dependencies { 

compile 'com.github.flipkart-incubator.proteus:gson-adapter:5.0.0-rc12'

}

在您的片段添加,并根据修改如下:

// some private fields in the fragment 

private Proteus proteus;

private ProteusContext context;

private ProteusLayoutInflater layoutInflater;

private Gson gson;

public void onAttach (Activity activity) {

// create a new instance of the proteus type adapter

// for gson and register it with gson.

ProteusTypeAdapterFactory adapter = new ProteusTypeAdapterFactory(activity);

gson = new GsonBuilder().registerTypeAdapterFactory(adapter).create();

// create a new instance of proteus from the builder

proteus = new ProteusBuilder().build();

// get a new context object from proteus

context = proteus.createContextBuilder(activity).build();

// this context object has the proteus layout inflater

layoutInflater = context.getInflater();

// set the instance of proteus

ProteusTypeAdapterFactory.PROTEUS_INSTANCE_HOLDER.setProteus(proteus);

}

public ProteusContext getContext() {

return context;

}

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

String layoutString = loadJSONFromAsset(getContext());

// use gson to deserialize the string into a Layout object.

Layout layout = gson.fromJson(layoutString, Layout.class);

// the use the proteus layout inflater to inflate a new proteus view

ProteusView view = layoutInflater.inflate(layout, new ObjectValue());

return view.getAsView();

}

这实际上是一个工作的例子,有很大的空间用于en灵修和改进。您应该前往git repo,克隆它,使用演示应用程序并查看ProteusActivity的代码以获取参考实现。

以上是 二手变形杆菌在Android碎片,但得到一个错误,而铸造吹气 - 变形的Android布局引擎 的全部内容, 来源链接: utcz.com/qa/262263.html

回到顶部