Использовал Proteus во фрагменте Android, но получал ошибку при наложении инфлятора - Proteus Android Layout Engine
Пожалуйста, проверьте мой код для этого:
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();
}
Я хочу использовать библиотеку Proteus, чтобы создать представление и отобразить его как фрагмент.
Подскажите, пожалуйста, что я сделал не так? С этим кодом я получаю следующую ошибку:
com.android.internal.policy.PhoneLayoutInflater не может быть приведен к com.flipkart.android.proteus.ProteusLayoutInflater
1 ответ
Вот пример кода для Fragment
это должно начать вас:
не забудьте добавить
gson-adapter
кbuild.gradle
файл вашего приложения.
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();
}
По сути, это рабочий пример с большим пространством для улучшений и улучшений. Вы должны отправиться в репозиторий git, клонировать его, поиграться с демо-приложением и проверить код ProteusActivity для эталонной реализации.