public class Fragment_zuixin extends Fragment { private SpringView sv; private RecyclerView rv; private View view; private ArrayList<String> list_image; private Banner banner; int index = 1; private ZuixinAdapter adapter; ArrayList<BeanZuiXin.Stories> list; String url = "http://news-at.zhihu.com/api/4/news/latest"; Handler handler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); if (msg.what == 1) { String s = (String) msg.obj; Gson gson = new Gson(); BeanZuiXin beanZuiXin = gson.fromJson(s, BeanZuiXin.class); //得到viewpager的数据 ArrayList<BeanZuiXin.Top_stories> top_stories = beanZuiXin.top_stories; list_image = new ArrayList<>(); for (int i = 0; i < top_stories.size(); i++) { list_image.add(top_stories.get(i).image); } //设置图片加载器 banner.setImageLoader(new GlideImageLoader()); //设置图片集合 banner.setImages(list_image); //banner设置方法全部调用完毕时最后调用 banner.start(); //设置布局管理器 LinearLayoutManager manager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false); rv.setLayoutManager(manager); //把获取的数据都放入一个新的集合 ArrayList<BeanZuiXin.Stories> stories = beanZuiXin.stories; list = new ArrayList<>(); for (int i = 0; i <stories.size() ; i++) { list.add(stories.get(i)); } //适配数据 if (adapter == null){ adapter = new ZuixinAdapter(getActivity(), list); rv.setAdapter(adapter); }else{ adapter.setList(list); } }else if (msg.what == 2) { //加载的条目 String s = (String) msg.obj; Gson gson = new Gson(); //获取数据 BeanZuiXin beanZuiXin = gson.fromJson(s, BeanZuiXin.class); //把获取的数据都放入一个新的集合 ArrayList<BeanZuiXin.Stories> stories = beanZuiXin.stories; list = new ArrayList<>(); for (int i = 0; i <stories.size() ; i++) { list.add(stories.get(i)); } //适配数据 if (adapter == null){ adapter = new ZuixinAdapter(getActivity(), list); rv.setAdapter(adapter); }else{ adapter.setList(list); } } } }; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { view = inflater.inflate(R.layout.zuixin_layout, null); //初始化 initView(); //获取数据 getData(url); //上啦加载,下拉刷新 setRefresh(); return view; } private void setRefresh() { sv.setType(SpringView.Type.FOLLOW); sv.setHeader(new DefaultHeader(getActivity())); sv.setFooter(new DefaultFooter(getActivity())); sv.setListener(new SpringView.OnFreshListener() { @Override public void onRefresh() { getData(url); sv.postDelayed(new Runnable() { @Override public void run() { sv.onFinishFreshAndLoad(); } }, 1200); } @Override public void onLoadmore() { index++; String url2 = "http://news-at.zhihu.com/api/4/news/before/2013111"+index; getData2(url2); sv.postDelayed(new Runnable() { @Override public void run() { sv.onFinishFreshAndLoad(); } }, 1200); } }); } //获取数据 private void getData2(String url2) { OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder().url(url2).build(); Call call = client.newCall(request); call.enqueue(new Callback() { @Override public void onFailure(Request request, IOException e) {} @Override public void onResponse(Response response) throws IOException { String string = response.body().string(); Message message = Message.obtain(); message.obj = string; message.what = 2; handler.sendMessage(message); } }); } //获取数据 private void getData(String url) { OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder().url(url).build(); Call call = client.newCall(request); call.enqueue(new Callback() { @Override public void onFailure(Request request, IOException e) { } @Override public void onResponse(Response response) throws IOException { String string = response.body().string(); Message message = Message.obtain(); message.obj = string; message.what = 1; handler.sendMessage(message); } }); } private void initView() { sv = (SpringView) view.findViewById(R.id.zuixin_sv); rv = (RecyclerView) view.findViewById(R.id.zuixin_rv); banner = (Banner) view.findViewById(R.id.banner); } }
shuaxing
最新推荐文章于 2021-03-23 06:16:32 发布