如何将 java 和 xml 代码(从 api 获取)运行到另一个片段中 [英] how to run java and xml code (fetched from api) into another fragment

查看:26
本文介绍了如何将 java 和 xml 代码(从 api 获取)运行到另一个片段中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

okay I will breifly define what I exactly want.I'm getting an java code(for textview, button,etc) from api like this--> image description here and Xml code(for textview, button,etc)from api like this--> image description here..what I want is to run these java and xml and display it like this--> enter image description here.. I really have no idea how to do that..can anyone help..I will post up java/xml code and its adapters following please have look ..I haven't done anything in demo fragment..what should I do in demo fragment???

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();
        }
    }
}

推荐答案

动态添加视图有两种方式.

There two way to add view dynamically .

  1. 你创建你的 xml 文件.然后使用 LayoutInflater 你将该 xml lnflater 到你的 view .

  1. you create you xml file .then using LayoutInflater you lnflater that xml to your view .

或者你可以像 var textview=TextView() 一样动态创建视图然后 textview.text="xyz"

Or You can create view dynamically like var textview=TextView() then textview.text="xyz"

您需要使用 addView(); 方法添加相关布局或线性布局.

You need relativelayout or linearlayout where will add those will using addView(); method.

但我的条件是你已经声明了你的视图,然后你可以在运行时自定义.

But i condition you have declare your view then you can customize at runtime.

您可以查看示例

这篇关于如何将 java 和 xml 代码(从 api 获取)运行到另一个片段中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆