Fragment

作者: summer_yatou | 来源:发表于2017-10-26 08:33 被阅读0次

    public class FragmentA extends Fragment{ 

      //日报    private String path="http://news-at.zhihu.com/api/4/news/latest";  

    private int riqi=20131101;  

    private View view; 

      private Banner banner; 

      private Handler handler=new Handler(){   

        @Override        public void handleMessage(Message msg) {            super.handleMessage(msg);       

        switch (msg.what){     

              case 0:                    String obj = (String) msg.obj;       

                Gson gson=new Gson();           

            NewsDayBao newsDayBao = gson.fromJson(obj, NewsDayBao.class);                    Listtop_stories = newsDayBao.getTop_stories();          

            initBanner(top_stories);          

            break;              

    case 1:        

              String objobj = (String) msg.obj;        

              Gson gson1=new Gson();        

              PullLoadData pullLoadData = gson1.fromJson(objobj, PullLoadData.class);                    Liststories = pullLoadData.getStories();        

              Log.i("sss",stories.get(0).getTitle());    

                  if (riqi==20131101){          

                pullData = new ArrayList<>();      

                }              

        for (int i = 0; ipullData) {     

      if (mypulladapter==null){      

        mypulladapter = new MyPullLoadRecycler(getActivity(),pullData);            pullload.setAdapter(mypulladapter);    

      }        else {       

      mypulladapter.notifyDataSetChanged();     

      }    }  

    private PullLoadMoreRecyclerView pullload;  

    private ListpullData;  

    @Nullable  

    @Override 

      public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {    

      view = inflater.inflate(R.layout.fragmenta, container, false);     

      return view;  

    }  

    @Override  

    public void onActivityCreated(

    @Nullable Bundle savedInstanceState) {  

        super.onActivityCreated(savedInstanceState);  

      //找控件     

      initId();    

      initData();     

      pullload.setLinearLayout();  

        pullload.setPushRefreshEnable(true);    

      pullload.setPullRefreshEnable(true);   

        pullload.setOnPullLoadMoreListener(new PullLoadMoreRecyclerView.PullLoadMoreListener() {         

      @Override      

        public void onRefresh() {

                    riqi=20131101;        

          new Handler().postDelayed(new Runnable() {          

            @Override         

              public void run() {     

                      pullload.setPullLoadMoreCompleted();    

                  }           

        },1000);     

          }        

      @Override        

      public void onLoadMore() {     

              new Handler().postDelayed(new Runnable() {      

                @Override                    public void run() {       

                    riqi++;                

          initWData();            

              pullload.setPullLoadMoreCompleted();           

            }                },1000);    

          }        });     

      initWData();  

      private void initWData() { 

        String url="http://news-at.zhihu.com/api/4/news/before/"+riqi; 

          OkHttp3Utils.doGet(url, new Callback() {     

          @Override      

        public void onFailure(Call call, IOException e) {     

          }       

        @Override        

      public void onResponse(Call call, Response response) throws IOException {   

                String pull = response.body().string();       

            Message message=new Message();                message.what=1;                message.obj=pull;                handler.sendMessage(message);            }        });    }   

    private void initData() {     

      OkHttp3Utils.doGet(path, new Callback() {        

      @Override    

          public void onFailure(Call call, IOException e) {            }      

        @Override          

    public void onResponse(Call call, Response response) throws IOException {      

            String json = response.body().string();        

          Log.i("ribao",json);         

          Message message=new Message();                message.what=0;          

        message.obj=json;          

        handler.sendMessage(message);            }        });    }  

    private void initBanner(Listtop_stories) {        Listbanndeimage=new ArrayList<>();

    //设置自动轮播,通常情况下为true

    banner.isAutoPlay(true);

    //将图片集合放入,加载图片

    for (int i = 0; i < top_stories.size(); i++) {

    banndeimage.add(top_stories.get(i).getImage());

    }

    banner.setImages(banndeimage);

    //每隔3秒切换一次

    banner.setDelayTime(3000);

    //样式

    banner.setBannerStyle(Banner.CIRCLE_INDICATOR_TITLE);

    banner.setIndicatorGravity(Banner.CIRCLE_INDICATOR);

    }

    private void initId() {

    banner = view.findViewById(R.id.home_banner);

    pullload = view.findViewById(R.id.pullload);

    }

    }

    //------------------------------------------------

    public class FragmentD extends Fragment{ 

      private View view;  

    private GridView gridview; 

      private Handler handler=new Handler(){   

        @Override        public void handleMessage(Message msg) {            super.handleMessage(msg);          

    switch (msg.what){             

      case 0:             

          String  obj = (String) msg.obj;             

          Gson gson=new Gson();        

              ZhuTiData zhuTiData = gson.fromJson(obj, ZhuTiData.class);      

                Listothers = zhuTiData.getOthers();      

                adapterData(others);         

              break;      

        }    

      }    };  

    private MyGridAdapter adapter; 

      private void adapterData(Listothers) {

    if (adapter==null) {

    adapter = new MyGridAdapter(getActivity(), others);

    gridview.setAdapter(adapter);

    }

    else {

    adapter.notifyDataSetChanged();

    }

    }

    @Nullable

    @Override

    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    view = inflater.inflate(R.layout.fragmentd, container, false);

    return view;

    }

    @Override

    public void onActivityCreated(@Nullable Bundle savedInstanceState) {

    super.onActivityCreated(savedInstanceState);

    initId();

    initData();

    gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

    @Override

    public void onItemClick(AdapterView adapterView, View view, int i, long l) {

    Intent intent=new Intent(getActivity(), GrideActivity.class);

    intent.putExtra("id",i);

    startActivity(intent);

    }

    });

    }

    private void initData() {

    String path="http://news-at.zhihu.com/api/4/themes";

    OkHttp3Utils.doGet(path, new Callback() {

    @Override

    public void onFailure(Call call, IOException e) {

    }

    @Override

    public void onResponse(Call call, Response response) throws IOException {

    String json = response.body().string();

    Log.i("jso",json);

    Message message=new Message();

    message.obj=json;

    message.what=0;

    handler.sendMessage(message);

    }

    });

    }

    private void initId() {

    gridview = view.findViewById(R.id.gridview);

    }

    }

    相关文章

      网友评论

          本文标题:Fragment

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