Fragment 的传值

package news1510a.bawei.com.a11_drawer_layout_demo;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.google.gson.Gson;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * @author Dash
 * @date 2017/10/12
 * @description:
 */
public class NewsFragment extends Fragment {

    private ListView listView;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_layout,container,false);

        //找到id
        listView = view.findViewById(R.id.frament_list_view);

        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        //获取数据
        String name =  getArguments().getString("name", "");

        //判断
        if (name.equals("头条")){
            name = "top";
        }else if (name.equals("军事")){
            name = "junshi";
        }else if (name.equals("社会")){
            name = "shehui";
        }else if (name.equals("财经")){
            name = "caijing";
        }else if (name.equals("国内")){
            name = "guonei";
        }

        getDataFromNet(name);

    }

    private void getDataFromNet(final String name) {
        AsyncTask asyncTask = new AsyncTask() {
            @Override
            protected String doInBackground(Void... voids) {
                try {
                    URL url = new URL("http://v.juhe.cn/toutiao/index?type="+name+"&key=597b4f9dcb50e051fd725a9ec54d6653");

                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();

                    connection.setRequestMethod("GET");
                    connection.setReadTimeout(5000);
                    connection.setConnectTimeout(5000);

                    //获取
                    int responseCode = connection.getResponseCode();
                    if (responseCode == 200){
                        InputStream inputStream = connection.getInputStream();

                        String json = streamToString(inputStream,"utf-8");

                        //打印字符串
                        Log.i("---",json);

                        return json;
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }


                return null;
            }

            @Override
            protected void onPostExecute(String s) {

                Gson gson = new Gson();
                DataDataBean dataDataBean = gson.fromJson(s, DataDataBean.class);

                MyAdapter myAdapter = new MyAdapter(getActivity(), dataDataBean.getResult().getData());

                listView.setAdapter(myAdapter);
            }
        };

        asyncTask.execute();
    }


    private String streamToString(InputStream inputStream,String charset) {
        try {
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream,charset);

            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String s = null;
            StringBuilder builder = new StringBuilder();
            while ((s = bufferedReader.readLine()) != null){
                builder.append(s);
            }

            bufferedReader.close();
            return builder.toString();

        } catch (Exception e) {
            e.printStackTrace();
        }

        return  null;
    }

}



————————————————————————————————————————————————————



——————————————————————————————————————————————————————

package bwie.com.yuekaolianxi2.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

import bwie.com.yuekaolianxi2.R;

/**
 * Created by lenovo on 2017/10/23.
 */
public class FragmentShouYe extends Fragment {


    private TabLayout tlt;
    private ViewPager vp;
    private List list;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_shouye, container, false);
        tlt = view.findViewById(R.id.tab);
        vp = view.findViewById(R.id.vp);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        list = new ArrayList<>();
//        数据新闻 xbsjxw
//        快讯      txs
//        头条      toutiao
//        精编公告 news/mobile/jbgg
//        美股      news/mobile/mgxw
//        港股      news/mobile/ggxw
//        基金      news/mobile/jjxw
//        理财      news/mobile/lcxw

        list.add("数据新闻");
        list.add("快讯");
        list.add("头条");
        list.add("精编公告");
        list.add("美股");
        list.add("港股");
        list.add("基金");
        list.add("理财");;
        vp.setOffscreenPageLimit(list.size());

        vp.setAdapter(new FragmentPagerAdapter(getChildFragmentManager()) {
            @Override
            public CharSequence getPageTitle(int position) {
                return list.get(position);
            }

            @Override
            public Fragment getItem(int position) {
                FragmentNews fragmentNews = new FragmentNews();
                Bundle bundle = new Bundle();
                if (list.get(position).equals("数据新闻")) {
                    bundle.putString("name", "xbsjxw");
                }else if (list.get(position).equals("快讯")){
                    bundle.putString("name","txs");
                }else if (list.get(position).equals("头条")){
                    bundle.putString("name","toutiao");
                }else if (list.get(position).equals("精编公告")){
                    bundle.putString("name","news/mobile/jbgg");
                }else if (list.get(position).equals("美股")){
                    bundle.putString("name","news/mobile/mgxw");
                }else if (list.get(position).equals("港股")){
                    bundle.putString("name","news/mobile/ggxw");
                }else if (list.get(position).equals("基金")){
                    bundle.putString("name","news/mobile/jjxw");
                }else if (list.get(position).equals("理财")){
                    bundle.putString("name","news/mobile/lcxw");
                }
                fragmentNews.setArguments(bundle);
                return fragmentNews;
            }

            @Override
            public int getCount() {
                return list.size();
            }
        });
        tlt.setupWithViewPager(vp);
    }
}

——————————————————————————————————————————————————

package bwie.com.yuekaolianxi2.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.handmark.pulltorefresh.library.ILoadingLayout;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import java.util.ArrayList;
import java.util.List;

import bwie.com.yuekaolianxi2.NetCaskBall;
import bwie.com.yuekaolianxi2.R;
import bwie.com.yuekaolianxi2.adapter.MyAdapter;
import bwie.com.yuekaolianxi2.bean.DataDataBean;
import bwie.com.yuekaolianxi2.util.GetDataUitl;

/**
 * Created by lenovo on 2017/10/23.
 */
public class FragmentNews extends Fragment{
    private List list1 = new ArrayList<>();
    private MyAdapter adapter;
    private PullToRefreshListView plv;
    private List list=new ArrayList<>();
    private int page_num=1;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_news, container, false);
        plv = view.findViewById(R.id.plv);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Bundle bundle = getArguments();
        final String string = bundle.getString("name", "xbsjxw");
        getNet(string);
//        设置支持上拉下拉模式并分别实现上拉和下拉的接口
        ILoadingLayout startLabels = plv.getLoadingLayoutProxy(true, false);
        startLabels.setPullLabel("下拉刷新");
        startLabels.setRefreshingLabel("正在拉");
        startLabels.setReleaseLabel("放开刷新");
        ILoadingLayout endLabels = plv.getLoadingLayoutProxy(false, true);
        endLabels.setPullLabel("上拉刷新");
        endLabels.setRefreshingLabel("正在载入...");
        endLabels.setReleaseLabel("放开刷新...");
        plv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase refreshView) {
                getNet(string);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase refreshView) {
                page_num++;
                GetDataUitl.getData("http://mnews.gw.com.cn/wap/data/news/"+string+"/page_"+page_num+".json",getActivity(), new NetCaskBall() {
                    @Override
                    public void getSuccess(String s) {
                        Gson gson = new Gson();
                        List data=gson.fromJson(s,new TypeToken>(){}.getType());
                        for(DataDataBean dataDataBean:data){
                            List dataBeen=dataDataBean.getData();
                            if (dataBeen!=null){
                                list.addAll(0,dataBeen);
                                setAdapter();
                                plv.onRefreshComplete();
                            }
                        }
                    }
                });
            }
        });
    }

    private void getNet(final String string) {
        GetDataUitl.getData("http://mnews.gw.com.cn/wap/data/news/"+string+"/page_1.json",getActivity(), new NetCaskBall() {
            @Override
            public void getSuccess(String s) {
                Gson gson = new Gson();
                List data=gson.fromJson(s,new TypeToken>(){}.getType());
                for(DataDataBean dataDataBean:data){
                    List dataBeen=dataDataBean.getData();
                    if (dataBeen!=null){
                        list.addAll(dataBeen);
                        setAdapter();
                        plv.onRefreshComplete();
                    }
                }
            }
        });
    }
    private void setAdapter() {
        if (adapter == null) {
            adapter = new MyAdapter(getActivity(), list);
            plv.setAdapter(adapter);
        } else {
            adapter.notifyDataSetChanged();
        }
    }
}

你可能感兴趣的:(03——实战2)