运用复杂的接口

1.写Bean类

package com.bawei.lianxi.Bean;

import java.util.List;

/**
 * @Auther: 不懂
 * @Date: 2019/1/17 16:19:23
 * @Description:
 */
public class Datas {

    /**
     * data : [{"advTypeShare":"","countid":20035,"id":"275200","img":"","otime":"2018-08-31 07:52:32","resType":"","source":"","summary":"截至8月30日记者发稿时,沪深两市3539家上市公司发布了半年报业绩。这些公司上半年共实现营收20.8万亿元,同比增长12.81%;实现净利润1.96万亿元,同","title":"3539家上市公司上半年净利增长14.39%","url":"http://mnews.gw.com.cn/wap/data/news/xbsjxw/2018/08/275200.json","views":""},{"advTypeShare":"","countid":20035,"id":"275228","img":"","otime":"2018-08-31 09:10:34","resType":"","source":"","summary":"截至08月30日,上交所融资余额报5164.92亿元,较前一交易日减少4.12亿元;深交所融资余额报3376.45亿元,较前一交易日减少2.69亿元;两市合计8","title":"两市融资余额减少6.81亿元","url":"http://mnews.gw.com.cn/wap/data/news/xbsjxw/2018/08/275228.json","views":""},{"advTypeShare":"","countid":20035,"id":"275197","img":"","otime":"2018-08-31 07:50:04","resType":"","source":"","summary":"根据数据计算,2015年底A股公司平均资产负债率为43.56%;去年上半年A股平均资产负债率降至41.37%。截至8月30日,已经披露今年半年报的3267家上市","title":"上半年A股公司整体负债率稳步降低","url":"http://mnews.gw.com.cn/wap/data/news/xbsjxw/2018/08/275197.json","views":""},{"advTypeShare":"","countid":20035,"id":"275196","img":"","otime":"2018-08-31 07:49:05","resType":"","source":"","summary":"9月份首发原股东限售股的解禁市值为703亿元,比8月份增加340亿元,增加幅度为93.68%。股改、定向增发等部分的非首发原股东解禁市值为848亿元,比8月份减","title":"9月限售股解禁市值:创业板年内最高 中小板年内最低","url":"http://mnews.gw.com.cn/wap/data/news/xbsjxw/2018/08/275196.json","views":""},{"advTypeShare":"","countid":20035,"id":"275195","img":"","otime":"2018-08-31 07:48:16","resType":"","source":"","summary":"截至8月30日晚记者发稿时,沪深两市已有3541家公司披露半年报,共实现营业收入20万亿元(可比),归属于母公司股东净利润1.93万亿元(可比),同比分别增长1","title":"上市公司半年报业绩平稳增长","url":"http://mnews.gw.com.cn/wap/data/news/xbsjxw/2018/08/275195.json","views":""},{"advTypeShare":"","countid":20035,"id":"275029","img":"","otime":"2018-08-30 08:01:41","resType":"","source":"","summary":" 本周二两市净流出态势再现,并在昨日盘面中延续。周二时两市主力净流出金额为118.91亿元,而昨日这一金额继续维持在百亿元关口上方,达138.49亿元,还有所上","title":"沪深两市主力资金净流出额再超百亿元","url":"http://mnews.gw.com.cn/wap/data/news/xbsjxw/2018/08/275029.json","views":""},{"advTypeShare":"","countid":20035,"id":"275028","img":"","otime":"2018-08-30 08:00:45","resType":"","source":"","summary":"今年8月份以来,由于市场持续低迷,整个A股市场的成交量呈现逐级下降的趋势,大量个股的成交量在低位徘徊,单日成交金额在1000万元以下的个股数量快速增加,\u201c僵尸股","title":"\u201c僵尸股\u201d蔓延外资持续买入 A股呈现多方面底部特征","url":"http://mnews.gw.com.cn/wap/data/news/xbsjxw/2018/08/275028.json","views":""},{"advTypeShare":"","countid":20035,"id":"275027","img":"","otime":"2018-08-30 07:59:55","resType":"","source":"","summary":"截至8月29日收盘,沪港通下港股通和深港通下港股通均呈现净卖出态势,两个渠道合计净卖出14.68亿港元。这是8月份港股通资金流向的一个缩影,事实上,8月份港股通","title":"南下资金8月大幅净流出 部分A+H股获逆势买入","url":"http://mnews.gw.com.cn/wap/data/news/xbsjxw/2018/08/275027.json","views":""},{"advTypeShare":"","countid":20035,"id":"274879","img":"","otime":"2018-08-29 09:29:31","resType":"","source":"","summary":"统计数据显示,截至8月28日记者发稿,沪深两市共计有2500家上市公司公布了2018年半年报。其中,1707家上市公司实现归属母公司股东的净利润增长,占比近七成","title":"逾1700家公司上半年净利实现增长","url":"http://mnews.gw.com.cn/wap/data/news/xbsjxw/2018/08/274879.json","views":""},{"advTypeShare":"","countid":20035,"id":"274878","img":"","otime":"2018-08-29 09:28:33","resType":"","source":"","summary":"昨日沪深市场剧烈波动,短线获利筹码集中兑现,主力资金流向由此再度转入为出。数据显示,昨日共计有118.92亿元资金从场内净流出,而前一日由于两市普涨,当天市场累","title":"热点分化 主力资金净流出118.92亿元","url":"http://mnews.gw.com.cn/wap/data/news/xbsjxw/2018/08/274878.json","views":""},{"advTypeShare":"","countid":20035,"id":"274873","img":"","otime":"2018-08-29 09:17:04","resType":"","source":"","summary":"2018年1月1日至8月27日,沪股通和深股通合计北上资金净流入2162.67亿元。截至2018年8月27日,沪股通和深股通累计北上资金净流入5637.83亿元","title":"北上资金年内涌入逾2000亿元","url":"http://mnews.gw.com.cn/wap/data/news/xbsjxw/2018/08/274873.json","views":""},{"advTypeShare":"","countid":20035,"id":"274872","img":"","otime":"2018-08-29 09:13:30","resType":"","source":"","summary":"截至8月28日,上交所融资余额报5174.11亿元,较前一交易日减少3.22亿元;深交所融资余额报3372.13亿元,较前一交易日减少1.44亿元;两市合计85","title":"两市融资余额减少4.66亿元","url":"http://mnews.gw.com.cn/wap/data/news/xbsjxw/2018/08/274872.json","views":""},{"advTypeShare":"","countid":20035,"id":"274707","img":"","otime":"2018-08-28 09:51:42","resType":"","source":"","summary":"截至8月27日,上交所融资余额报5177.33亿元,较前一交易日增加1.69亿元;深交所融资余额报3373.57亿元,较前一交易日减少0.5亿元;两市合计855","title":"融资余额增加1.19亿元","url":"http://mnews.gw.com.cn/wap/data/news/xbsjxw/2018/08/274707.json","views":""},{"advTypeShare":"","countid":20035,"id":"274686","img":"","otime":"2018-08-28 08:39:53","resType":"","source":"","summary":"截至昨日,共有2123家上市公司发布了上半年业绩,至8月底还将有1400多家公司披露中报。值得注意的是,在已发布业绩预告的公司中,有136家公司业绩首亏,其中7","title":"136家公司中报业绩首亏","url":"http://mnews.gw.com.cn/wap/data/news/xbsjxw/2018/08/274686.json","views":""},{"advTypeShare":"","countid":20035,"id":"274685","img":"","otime":"2018-08-28 08:36:54","resType":"","source":"","summary":"据经济参考报报道,上市公司半年报披露接近尾声,微观端企业的运行状况逐渐浮出水面。数据显示,上半年A股上市公司盈利依然保持较高增速,但内部结构分化加剧。在周期性行","title":"七成净利实现增长 上市公司中报凸显经济新变化","url":"http://mnews.gw.com.cn/wap/data/news/xbsjxw/2018/08/274685.json","views":""},{"advTypeShare":"","countid":20035,"id":"274538","img":"","otime":"2018-08-27 10:12:02","resType":"","source":"","summary":"截至8月26日,上交所融资余额报5175.64亿元,较前一交易日减少18.89亿元;深交所融资余额报3374.06亿元,较前一交易日减少8.12亿元;两市合计8","title":"两市融资余额减少27.01亿元","url":"http://mnews.gw.com.cn/wap/data/news/xbsjxw/2018/08/274538.json","views":""},{"advTypeShare":"","countid":20035,"id":"274503","img":"","otime":"2018-08-27 08:18:28","resType":"","source":"","summary":"上周,北上资金除在最后一个交易日小幅流出外,其他交易日均呈现净流入状态,市场\u201c地量\u201d震荡之际,北上资金逆势加仓。数据显示,上周北上资金净流入115.57亿元,北","title":"北上资金上周加仓超百亿","url":"http://mnews.gw.com.cn/wap/data/news/xbsjxw/2018/08/274503.json","views":""},{"advTypeShare":"","countid":20035,"id":"274502","img":"","otime":"2018-08-27 08:17:40","resType":"","source":"","summary":"根据沪深交易所的安排,本周沪深两市共有35家公司的限售股解禁上市流通,解禁股数共计22.79亿股,占未解禁限售A股的0.27%。其中,沪市1.59亿股,占沪市限","title":"本周解禁市值环比降五成 仅35家公司解禁207亿元","url":"http://mnews.gw.com.cn/wap/data/news/xbsjxw/2018/08/274502.json","views":""},{"advTypeShare":"","countid":20035,"id":"274501","img":"","otime":"2018-08-27 08:16:13","resType":"","source":"","summary":"截至8月23日,沪深两市有1606家已披露半年报的上市公司中,有137家获QFII重仓持有,QFII持股规模约507亿元。其中,QFII在二季度新进持有32家公","title":"QFII调仓路径曝光 加仓周期减持消费","url":"http://mnews.gw.com.cn/wap/data/news/xbsjxw/2018/08/274501.json","views":""},{"advTypeShare":"","countid":20035,"id":"274315","img":"","otime":"2018-08-24 07:59:30","resType":"","source":"","summary":"《证券日报》市场研究中心根据数据统计发现,6月份以来,沪股通资金净流入610.19亿元,深股通资金净流入316.25亿元,北上资金期间合计净流入资金达到926.","title":"北上资金逆市\u201c买买买\u201d","url":"http://mnews.gw.com.cn/wap/data/news/xbsjxw/2018/08/274315.json","views":""}]
     * header : {"first":"page_1.json","last":"page_10.json","next":"page_2.json","pagesize":20,"pre":"page_1.json","totalpage":10,"totalsize":200}
     */

    private HeaderBean header;
    private List<DataBean> data;

    public HeaderBean getHeader() {
        return header;
    }

    public void setHeader(HeaderBean header) {
        this.header = header;
    }

    public List<DataBean> getData() {
        return data;
    }

    public void setData(List<DataBean> data) {
        this.data = data;
    }

    public static class HeaderBean {
        /**
         * first : page_1.json
         * last : page_10.json
         * next : page_2.json
         * pagesize : 20
         * pre : page_1.json
         * totalpage : 10
         * totalsize : 200
         */

        private String first;
        private String last;
        private String next;
        private int pagesize;
        private String pre;
        private int totalpage;
        private int totalsize;

        public String getFirst() {
            return first;
        }

        public void setFirst(String first) {
            this.first = first;
        }

        public String getLast() {
            return last;
        }

        public void setLast(String last) {
            this.last = last;
        }

        public String getNext() {
            return next;
        }

        public void setNext(String next) {
            this.next = next;
        }

        public int getPagesize() {
            return pagesize;
        }

        public void setPagesize(int pagesize) {
            this.pagesize = pagesize;
        }

        public String getPre() {
            return pre;
        }

        public void setPre(String pre) {
            this.pre = pre;
        }

        public int getTotalpage() {
            return totalpage;
        }

        public void setTotalpage(int totalpage) {
            this.totalpage = totalpage;
        }

        public int getTotalsize() {
            return totalsize;
        }

        public void setTotalsize(int totalsize) {
            this.totalsize = totalsize;
        }
    }

    public static class DataBean {
        /**
         * advTypeShare :
         * countid : 20035
         * id : 275200
         * img :
         * otime : 2018-08-31 07:52:32
         * resType :
         * source :
         * summary : 截至8月30日记者发稿时,沪深两市3539家上市公司发布了半年报业绩。这些公司上半年共实现营收20.8万亿元,同比增长12.81%;实现净利润1.96万亿元,同
         * title : 3539家上市公司上半年净利增长14.39%
         * url : http://mnews.gw.com.cn/wap/data/news/xbsjxw/2018/08/275200.json
         * views :
         */

        private String advTypeShare;
        private int countid;
        private String id;
        private String img;
        private String otime;
        private String resType;
        private String source;
        private String summary;
        private String title;
        private String url;
        private String views;

        public String getAdvTypeShare() {
            return advTypeShare;
        }

        public void setAdvTypeShare(String advTypeShare) {
            this.advTypeShare = advTypeShare;
        }

        public int getCountid() {
            return countid;
        }

        public void setCountid(int countid) {
            this.countid = countid;
        }

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getImg() {
            return img;
        }

        public void setImg(String img) {
            this.img = img;
        }

        public String getOtime() {
            return otime;
        }

        public void setOtime(String otime) {
            this.otime = otime;
        }

        public String getResType() {
            return resType;
        }

        public void setResType(String resType) {
            this.resType = resType;
        }

        public String getSource() {
            return source;
        }

        public void setSource(String source) {
            this.source = source;
        }

        public String getSummary() {
            return summary;
        }

        public void setSummary(String summary) {
            this.summary = summary;
        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public String getUrl() {
            return url;
        }

        public void setUrl(String url) {
            this.url = url;
        }

        public String getViews() {
            return views;
        }

        public void setViews(String views) {
            this.views = views;
        }
    }
}

2.写基类

public abstract class BaseActivity extends AppCompatActivity {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(layoutResID());
        initView();
        initData();
    }

    protected abstract void initData();

    protected abstract void initView();

    protected abstract int layoutResID();
}

public abstract class BaseFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view  = View.inflate(getContext(),layoutResID(),null);
        initView(view);
        initData();
        return view;
    }

    protected abstract void initData();

    protected abstract void initView(View view);

    protected abstract int layoutResID();
}

3.封装 HttpConnection请求数据 网络判断

public class ConnteUtils {
    public static String isNetConnection(String path) {
        try {
            URL url = new URL(path);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setConnectTimeout(50000);
            conn.setReadTimeout(5000);
            conn.setRequestMethod("GET");
            if (conn.getResponseCode() == 200) {
                InputStream inputStream = conn.getInputStream();
                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                byte[] bytes = new byte[1024];
                int len = 0;
                while ((len = inputStream.read(bytes)) != -1) {
                    stream.write(bytes, 0, len);
                }
                stream.close();
                inputStream.close();
                String data = stream.toString();
                return data;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

public class Neteorks {
    public static boolean isNetWork(Context context){
        ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo info = manager.getActiveNetworkInfo();
        if(info!=null){
            return info.isAvailable();
        }
        return false;
    }
}

5.Application

public class MyApplication extends ChannelApplication {
    @Override
    public void onCreate() {
        super.onCreate();
        ImageLoaderConfiguration imageLoaderConfiguration = new ImageLoaderConfiguration.Builder(this)
                .build();
        ImageLoader.getInstance().init(imageLoaderConfiguration);
    }
}


6.Fragment

public class Fragments extends BaseFragment {
    private String path = "http://mnews.gw.com.cn/wap/data/news/";
    private PullToRefreshListView pull;
    private int page =1;
    private ArrayList<Datas.DataBean> list = null;
    private Handler handler = new Handler();
    private String arr[] = {"xbsjxw", "txs", "toutiao", "news/mobile/jbgg", "news/mobile/mgxw", "news/mobile/ggxw", "news/mobile/jjxw", "news/mobile/lcxw"};
    private int id;

    @Override
    protected int layoutResID() {
        return R.layout.fragmetns;
    }
    @Override
    protected void initView(View view) {
         id = getArguments().getInt("name");
        pull = view.findViewById(R.id.pull);
        pull.setPullToRefreshEnabled(true);
        pull.setMode(PullToRefreshBase.Mode.BOTH);
        pull.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                boolean netWork = Neteorks.isNetWork(getContext());
                if (netWork) {
                    handler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            page = 1;
                            setAsync(path + arr[id] + "/page_" + page + ".json");
                            pull.onRefreshComplete();
                        }
                    }, 2000);
                } else {
                    handler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            pull.onRefreshComplete();
                        }
                    }, 2000);
                }
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                boolean netWork = Neteorks.isNetWork(getContext());
                if (netWork) {
                    handler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            page++;
                            setAsync(path + arr[id] + "/page_" + page + ".json");
                            pull.onRefreshComplete();
                        }
                    }, 2000);
                } else {
                    handler.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            pull.onRefreshComplete();
                        }
                    }, 2000);
                }
            }
        });
    }

    private void setAsync(String s) {
        MyAsync myAsync = new MyAsync();
        myAsync.execute(s);
    }

    @Override
    protected void initData() {
        setInterent();
    }

    private void setInterent() {
        NetInterent netInterent = new NetInterent();
        IntentFilter filter = new IntentFilter();
        filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        getActivity().registerReceiver(netInterent,filter);
    }


    private class MyAsync extends AsyncTask<String,Integer,String> {
        @Override
        protected String doInBackground(String... strings) {
            return ConnteUtils.isNetConnection(strings[0]);
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            try {
                JSONArray jsonArray = new JSONArray(s);
                JSONObject jsonObject = jsonArray.getJSONObject(0);
                String data = jsonObject.toString();
                Log.i("xxx", data);
                Gson gson = new Gson();
                Datas datas = gson.fromJson(data, Datas.class);
                List<Datas.DataBean> data1 = datas.getData();
                MySqlDao mySqlDao = new MySqlDao(getActivity());
                if (page == 1) {
                    list = new ArrayList<>();
                    mySqlDao.delete();
                    mySqlDao.add(data1);
                }
                list.addAll(data1);
                PullAdapter pullAdapter = new PullAdapter(getActivity(),list);
                pull.setAdapter(pullAdapter);
                pull.setSelection(pullAdapter.getCount() - data1.size());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private class NetInterent extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            boolean netWork = Neteorks.isNetWork(getContext());
            if(netWork){
                setAsync(path + arr[id] + "/page_" + page + ".json");
            }else{
                MySqlDao mySqlDao = new MySqlDao(getActivity());
                ArrayList<Datas.DataBean> select = mySqlDao.select();
                PullAdapter pullAdapter = new PullAdapter(getActivity(),select);
                pull.setAdapter(pullAdapter);
            }
        }
    }
}


Frag01

public class Frag01 extends BaseFragment {

    private TabLayout tab;
    private ViewPager pager;
    private ArrayList<String> list = new ArrayList<>();
    private Button btn;
    private ArrayList<ChannelItem> tabData;
    private ArrayList<ChannelItem> tabData1;

    @Override
    protected int layoutResID() {
        return R.layout.frag01;
    }

    @Override
    protected void initView(View view) {
        tab = view.findViewById(R.id.tab);
        pager = view.findViewById(R.id.pager);
        btn = view.findViewById(R.id.btn);
    }
    @Override
    protected void initData() {
        //集合
        tabData = DBUtil.getTabData(getActivity());
        //适配器
        pager.setAdapter(new FragmentPagerAdapter(getChildFragmentManager()) {
            @Override
            public int getCount() {
                return tabData.size();
            }
            @Override
            public Fragment getItem(int i) {
                Fragments frag = new Fragments();
                 Bundle bundle = new Bundle();
                 bundle.putInt("name",i);
                frag.setArguments(bundle);
                return frag;
            }
            @Nullable
            @Override
            public CharSequence getPageTitle(int position) {
                return tabData.get(position).getName();
            }
        });
        //关联适配器
        tab.setupWithViewPager(pager);
        //跳转
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), ChannelActivity.class);
                startActivityForResult(intent,0);
            }
        });
        
   }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        tabData.clear();
        //集合
        tabData1 = DBUtil.getTabData(getActivity());
        //适配器
        pager.setAdapter(new FragmentPagerAdapter(getChildFragmentManager()) {
            @Override
            public int getCount() {
                return tabData1.size();
            }
            @Override
            public Fragment getItem(int i) {
                Fragments frag = new Fragments();
                Bundle bundle = new Bundle();
                bundle.putInt("name",i);
                frag.setArguments(bundle);
                return frag;
            }
            @Nullable
            @Override
            public CharSequence getPageTitle(int position) {
                return Frag01.this.tabData1.get(position).getName();
            }
        });
        //关联适配器
        tab.setupWithViewPager(pager);
        
        
    }
}

8.MainActivity

public class MainActivity extends BaseActivity {
    private String path = "http://mnews.gw.com.cn/wap/data/news/xbsjxw/page_1.json";
    private RadioGroup radioGroup;
    private DrawerLayout dr;
    private FragmentTransaction transaction;
    private FragmentManager manager;
    private Frag01 frag01;
    private Frag02 frag02;
    private Frag03 frag03;
    private Frag04 frag04;
    private Frag05 frag05;
    private int page =1;
    private PullToRefreshListView pull;

    @Override
    protected int layoutResID() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        radioGroup = findViewById(R.id.radioGroup);
        dr = findViewById(R.id.dr);
        pull = findViewById(R.id.pull);
    }

    @Override
    protected void initData() {
        manager = getSupportFragmentManager();
        transaction = manager.beginTransaction();

        frag01 = new Frag01();
        frag02 = new Frag02();
        frag03 = new Frag03();
        frag04 = new Frag04();
        frag05 = new Frag05();
        transaction.add(R.id.frame,frag01);
        transaction.add(R.id.frame,frag02);
        transaction.add(R.id.frame,frag03);
        transaction.add(R.id.frame,frag04);
        transaction.add(R.id.frame,frag05);
        transaction.show(frag01).hide(frag02).hide(frag03).hide(frag04).hide(frag05);
        transaction.commit();
        radioGroup.check(R.id.radio0);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                FragmentTransaction transaction1 = manager.beginTransaction().hide(frag04).hide(frag05);
                switch (checkedId){
                    case R.id.radio0:
                        transaction1.show(frag01).hide(frag02).hide(frag03).hide(frag04).hide(frag05);
                        break;
                    case R.id.radio1:
                        transaction1.show(frag02).hide(frag01).hide(frag03).hide(frag04).hide(frag05);
                        break;
                    case R.id.radio2:
                        transaction1.show(frag03).hide(frag02).hide(frag01).hide(frag04).hide(frag05);
                        break;
                    case R.id.radio3:
                        transaction1.show(frag04).hide(frag02).hide(frag01).hide(frag03).hide(frag05);
                        break;
                    case R.id.radio4:
                        transaction1.show(frag05).hide(frag02).hide(frag01).hide(frag03).hide(frag04);
                        break;
                }
                transaction1.commit();
                getSerData();
            }
        });

    }

    private void getSerData() {
        MyAsyncTask task = new MyAsyncTask();
        task.execute();
    }


    private class MyAsyncTask extends AsyncTask<String,Integer,String> {
        @Override
        protected String doInBackground(String... strings) {
            try {
                URL url = new URL(path);
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.setConnectTimeout(50000);
                conn.setReadTimeout(5000);
                conn.setRequestMethod("GET");
                if (conn.getResponseCode() == 200) {
                    InputStream inputStream = conn.getInputStream();
                    ByteArrayOutputStream stream = new ByteArrayOutputStream();
                    byte[] bytes = new byte[1024];
                    int len = 0;
                    while ((len = inputStream.read(bytes)) != -1) {
                        stream.write(bytes, 0, len);
                    }
                    stream.close();
                    inputStream.close();
                    String data = stream.toString();
                    return data;
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;

        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            try {
                JSONArray jsonArray = new JSONArray(s);
                JSONObject jsonObject = jsonArray.getJSONObject(0);
                String data = jsonObject.toString();
                Log.i("xxx", data);
                Gson gson = new Gson();
                Datas datas = gson.fromJson(data, Datas.class);
                List<Datas.DataBean> data1 = datas.getData();
                PullAdapter pullAdapter = new PullAdapter(MainActivity.this, data1);
                pull.setAdapter(pullAdapter);

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

9.布局
activity_main

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <android.support.v4.widget.DrawerLayout
        android:id="@+id/dr"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <FrameLayout
                android:id="@+id/frame"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                ></FrameLayout>
            <RadioGroup
                android:id="@+id/radioGroup"
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                >
                <RadioButton
                    android:id="@+id/radio0"
                    android:layout_weight="1"
                    android:button="@null"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="首页1"
                    />
                <RadioButton
                    android:id="@+id/radio1"
                    android:layout_weight="1"
                    android:button="@null"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="首页2"
                    />
                <RadioButton
                    android:id="@+id/radio2"
                    android:layout_weight="1"
                    android:button="@null"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="首页3"

                    />
                <RadioButton
                    android:id="@+id/radio3"
                    android:layout_weight="1"
                    android:button="@null"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="首页4"

                    />
                <RadioButton
                    android:id="@+id/radio4"
                    android:layout_weight="1"
                    android:button="@null"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="首页5"

                    />
            </RadioGroup>
        </LinearLayout>
        <LinearLayout
            android:background="@color/colorAccent"
            android:layout_gravity="start"
            android:layout_width="300dp"
            android:layout_height="match_parent">
            <com.handmark.pulltorefresh.library.PullToRefreshListView
                android:id="@+id/pull"
                android:layout_width="match_parent"
                android:layout_height="match_parent"></com.handmark.pulltorefresh.library.PullToRefreshListView>
        </LinearLayout>
    </android.support.v4.widget.DrawerLayout>
</LinearLayout>

frag01

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:orientation="horizontal"
        >
        <android.support.design.widget.TabLayout
            android:id="@+id/tab"
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="50dp">
        </android.support.design.widget.TabLayout>
        <Button
            android:id="@+id/btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/ic_launcher_background"
            />
    </LinearLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></android.support.v4.view.ViewPager>
</LinearLayout>

fragments

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
<com.handmark.pulltorefresh.library.PullToRefreshListView
    android:id="@+id/pull"
    android:layout_width="match_parent"
    android:layout_height="match_parent"></com.handmark.pulltorefresh.library.PullToRefreshListView>
</LinearLayout>

item_layout_one

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="10dp"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/img1"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@mipmap/ic_launcher" />

    <TextView
        android:id="@+id/title1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="hhhhhh"
        android:textSize="20sp" />
</LinearLayout>

item_layout_two

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="10dp"
    android:orientation="horizontal">


    <TextView
        android:id="@+id/title2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="hhhhhh"
        android:textSize="20sp" />
</LinearLayout>

item0

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/tv0"
        android:layout_width="match_parent"
        android:layout_height="120dp"
        android:layout_weight="1"
        android:textSize="25sp" />

    <ImageView
        android:id="@+id/iw"
        android:layout_width="150dp"
        android:layout_height="120dp"
        android:src="@mipmap/ic_launcher" />

</LinearLayout>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值