如何将 Java 和 XML 代码(从 API 获取)运行到另一个片段中



好吧,我会定义我到底想要什么。我从 api 获得一个 java 代码(用于文本视图、按钮等(,如下所示 - 此处>图像描述和 Xml 代码(用于文本视图、按钮等(来自 api 像这样 - 此处>图像描述。我想要的是运行这些Java和XML并像这样显示它 ->在这里输入图像描述。我真的不知道该怎么做。谁能帮忙..我将在下面发布java/xml代码及其适配器,请看..我没有在演示片段中做任何事情。我应该在演示片段中做什么???

.java:

public class JavaFragment extends Fragment {
private RecyclerView recyclerView;
private NextSLJavaAdapter adapter;
private NextSLModel DescriptList;
ProgressDialog progressDialog;
public JavaFragment() {
// Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.nextsl_layout, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Toolbar toolbar = (Toolbar) getView().findViewById(R.id. toolbar );
// setSupportActionBar( toolbar );
//if (getSupportActionBar() != null) {
//  getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// getSupportActionBar().setDisplayShowHomeEnabled(true);
//}
progressDialog = new ProgressDialog(getContext());
progressDialog.setMessage("Loading....");
progressDialog.show();
Intent intent = getActivity().getIntent();
String title = intent.getStringExtra("title");
//getSupportActionBar().setTitle(title);
String id = intent.getStringExtra("idSLnext");
Log.e("ashwini", String.valueOf(id));

/*Create handle for the RetrofitInstance interface*/
SLApiSevice service = SLApiClient.getRetrofitInstance().create(SLApiSevice.class);
Call<NextSLModel> call = service.getnextslmodel(id);
call.enqueue(new Callback<NextSLModel>() {
@Override
public void onResponse(Call<NextSLModel> call, Response<NextSLModel> response) {
progressDialog.dismiss();
DescriptList=response.body();
generateDataList(DescriptList);
}
@Override
public void onFailure(Call<NextSLModel> call, Throwable t) {
progressDialog.dismiss();
Toast.makeText(getContext(), "Something went wrong...Please try later!", Toast.LENGTH_SHORT).show();
}
});
}
private void generateDataList(NextSLModel photoList) {
recyclerView = getView().findViewById(R.id.nextSLrecycle);
LinearLayoutManager manager = new LinearLayoutManager(getContext());
recyclerView.setLayoutManager(manager);
recyclerView.setHasFixedSize(true);
adapter = new NextSLJavaAdapter(getContext(),photoList);
recyclerView.setAdapter(adapter);
}
}

Java 适配器:

public class NextSLJavaAdapter extends RecyclerView.Adapter<NextSLJavaAdapter.CustomViewHolder> {
NextSLModel Slmdel;
Context context;
public NextSLJavaAdapter(Context context, NextSLModel employees) {
this.Slmdel = employees;
this.context = context;
}
@Override
public CustomViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.nextsl_item, parent, false);
return new CustomViewHolder(itemView);
}
@Override
public void onBindViewHolder(CustomViewHolder holder, int position) {
holder.employeeName.setText(Slmdel.getJava());
Log.e("sl",Slmdel.getJava());

}
@Override
public int getItemCount() {
return 1;
//return (employees == null) ? 0 : employees.size();
}
public class CustomViewHolder extends RecyclerView.ViewHolder {
public TextView employeeName;
TextView textView;
public CustomViewHolder(View view) {
super(view);
employeeName = (TextView) view.findViewById(R.id.detailsStartLearning);
textView = view.findViewById(R.id.Sl1);
}
}
}

XML代码:

public class XMLFragmet extends Fragment {
private RecyclerView recyclerView;
private NextSLXmlAdapter adapter;
private NextSLModel DescriptList;
ProgressDialog progressDialog;
public XMLFragmet() {
// Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.nextsl_layout, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
/*Create handle for the RetrofitInstance interface*/
progressDialog = new ProgressDialog(getContext());
progressDialog.setMessage("Loading....");
progressDialog.show();
Intent intent = getActivity().getIntent();
String id = intent.getStringExtra("idSLnext");
Log.e("ashwini", String.valueOf(id));
SLApiSevice service = SLApiClient.getRetrofitInstance().create(SLApiSevice.class);
Call<NextSLModel> call = service.getnextslmodel(id);
call.enqueue(new Callback<NextSLModel>() {
@Override
public void onResponse(Call<NextSLModel> call, Response<NextSLModel> response) {
progressDialog.dismiss();
DescriptList = response.body();
generateDataList(DescriptList);
}
@Override
public void onFailure(Call<NextSLModel> call, Throwable t) {
progressDialog.dismiss();
Toast.makeText(getContext(), "Something went wrong...Please try later!", Toast.LENGTH_SHORT).show();
}
});
}
private void generateDataList(NextSLModel photoList) {
recyclerView = getView().findViewById(R.id.nextSLrecycle);
LinearLayoutManager manager = new LinearLayoutManager(getContext());
recyclerView.setLayoutManager(manager);
recyclerView.setHasFixedSize(true);
adapter = new NextSLXmlAdapter(getContext(),photoList);
recyclerView.setAdapter(adapter);
}
}

XML 适配器:

public class NextSLXmlAdapter extends RecyclerView.Adapter<NextSLXmlAdapter.CustomViewHolder> {
NextSLModel Slmdel;
Context context;
public NextSLXmlAdapter(Context context, NextSLModel employees) {
this.Slmdel = employees;
this.context = context;
}
@Override
public CustomViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.nextsl_item, parent, false);
return new CustomViewHolder(itemView);
}
@Override
public void onBindViewHolder(CustomViewHolder holder, int position) {
holder.employeeName.setText(Slmdel.getXml());

}
@Override
public int getItemCount() {
return 1;
//return (employees == null) ? 0 : employees.size();
}
public class CustomViewHolder extends RecyclerView.ViewHolder {
public TextView employeeName;
TextView textView;
public CustomViewHolder(View view) {
super(view);
employeeName = (TextView) view.findViewById(R.id.detailsStartLearning);
textView = view.findViewById(R.id.Sl1);
}
}
}

演示活动:

public class DemoFragment extends Fragment {

public DemoFragment() {
// Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_demo, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Toast.makeText(getContext(),"hi Edit",Toast.LENGTH_LONG).show();
Intent intent = getActivity().getIntent();

String id = intent.getStringExtra("idSLnext");
Log.e("demo", id);
if(id.matches("11"))
{
///can't I put logic over here which im getting from api(java and xml)
Toast.makeText(getContext(),"hi textview",Toast.LENGTH_LONG).show();;
}
else if(id.matches("10"))
{
Toast.makeText(getContext(),"hi Edit",Toast.LENGTH_LONG).show();
}
else if(id.matches("9"))
{
Toast.makeText(getContext(),"hi Imageview",Toast.LENGTH_LONG).show();
}
else if(id.matches("8"))
{
Toast.makeText(getContext(),"hi Button",Toast.LENGTH_LONG).show();
}
else if(id.matches("7"))
{
Toast.makeText(getContext(),"hi CheckBox",Toast.LENGTH_LONG).show();
}
else if(id.matches("6"))
{
Toast.makeText(getContext(),"hi RadioButton & RadioGroup",Toast.LENGTH_LONG).show();
}
else if(id.matches("5"))
{
Toast.makeText(getContext(),"hi DatePicker",Toast.LENGTH_LONG).show();
}
else if(id.matches("4"))
{
Toast.makeText(getContext(),"hi TimePicker",Toast.LENGTH_LONG).show();
}
else if(id.matches("3"))
{
Toast.makeText(getContext(),"hi DatePicker",Toast.LENGTH_LONG).show();
}
else if(id.matches("2"))
{
Toast.makeText(getContext(),"hi Switch",Toast.LENGTH_LONG).show();
}
else if(id.matches("1"))
{
Toast.makeText(getContext(),"hi Simple & custom Toast",Toast.LENGTH_LONG).show();
}
}
}

有两种方法可以动态添加视图。

  1. 您创建xml文件 .then 使用LayoutInflater将该 XML 更新到您的view

  2. 或者,您可以像 vartextview=TextView()一样动态创建视图 然后 textview.text="xyz">

您需要相对布局或线性布局,其中将使用addView();方法添加这些内容。

但是我条件是您已声明您的视图,然后您可以在运行时进行自定义。

您可以查看示例

Android 从一开始就不支持远程代码注入。框架开发人员可能已经从安全角度以及手机具有的有限计算角度进行了考虑。请注意,android工作室构建过程将这些java,xml文件转换为dex文件,这些文件无法在运行时完成,因此不受支持。

重要提示 出于性能原因,视图膨胀在很大程度上依赖于在生成时完成的 XML 文件的预处理。因此,目前无法在运行时将 LayoutInflater 与 XmlPullParser 一起使用,而不是纯 XML 文件。

另一种方法是对视图和属性使用一些 JSON 格式,它将在空白画布上动态绘制小部件,将其添加到布局父级。

例如。

{
"type": "LinearLayout",
"orientation": "vertical",
"padding": "16dp",
"children": [{
"layout_width": "200dp",
"gravity": "center",
"type": "TextView",
"text": "@{user.profile.name}"
}, {
"type": "HorizontalProgressBar",
"layout_width": "200dp",
"layout_marginTop": "8dp",
"max": 6000,
"progress": "@{user.profile.experience}"
}]
}

这将添加一个带有Childerns的LinearLayout作为TextView,HorizontalScrollBar。数据也可以像下面这样插入。

{
"user": {
"profile": {
"name": "John Doe",
"experience": 4192
}
}
}

关于如何使用此SDK,请点击以下链接。 https://github.com/flipkart-incubator/proteus

我亲爱的朋友, 不幸的是,这是不可能的。 正如你所说的 来自服务器端的 XML 和 Java 代码。要运行java代码,我们需要对其进行编译,这在移动设备上是不可能的。

希望你能理解。但是,您可以做的是创建一个可以解析 JSON 的帮助程序类并返回从 JSON 生成的视图,并且 JSON 包含元素描述,如元素类型、宽度、高度,可以在服务器中预定义。

或者,您可以使用从服务器加载网页的Webview。 并在应用程序中使用它。 或者对于某些操作,请使用拦截JavaScript。

最新更新