美文网首页
Fragment的嵌套和复用

Fragment的嵌套和复用

作者: HOLLE_karry | 来源:发表于2020-04-02 09:50 被阅读0次

    ProjectFragment

    private void initData() {
         new Thread(new Runnable() {
                @Override
                public void run() {
                //获取数据
                    try {
                        URL url = new URL("https://www.wanandroid.com/project/tree/json");
                        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                        if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                            StringBuffer buffer = new StringBuffer();
                            String len = null;
                            while ((len = reader.readLine()) != null) {
                                buffer.append(len);
                            }
                            String string = buffer.toString();
                             //解析
                            final ProjectBean projectBean = new Gson().fromJson(string, ProjectBean.class);
                            getActivity().runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                //创建Fragment
                                    ArrayList<Fragment> fragments = new ArrayList<>();
                                    for (int i = 0; i < projectBean.getData().size(); i++) {
                                        fragments.add(new PublicFragment(projectBean.getData().get(i).getId()));
                                    }
                                     //创建适配器
                                    PagerAdapter adapter = new PagerAdapter(getChildFragmentManager(), fragments);
                                    mVp.setAdapter(adapter);
                                    mTab.setupWithViewPager(mVp);//联动
                                    //添加tab
                                    for (int i = 0; i < projectBean.getData().size(); i++) {
                                        mTab.getTabAt(i).setText(projectBean.getData().get(i).getName());
                                    }
                                }
                            });
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }
    

    PublicFragment

    public PublicFragment(int id) {
            this.id = id;//不写id会报错
    }
    
    private void initView(@NonNull final View view) {
            mRv = (RecyclerView) view.findViewById(R.id.rv);
            mRv.setLayoutManager(new LinearLayoutManager(getActivity()));//布局管理器
            list = new ArrayList<>();
            adapter = new PublicAdapter(getActivity(), list);//创建适配器
            mRv.setAdapter(adapter);//设置适配器
            initData();//解析
        }
    

    相关文章

      网友评论

          本文标题:Fragment的嵌套和复用

          本文链接:https://www.haomeiwen.com/subject/zvptphtx.html