1. 程式人生 > >XListView解析資料及PullToRefresh解析資料

XListView解析資料及PullToRefresh解析資料

package com.example.week3.Fragment;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.NonNull;
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.example.mylibrary.me.maxwin.view.XListView;
import com.example.week3.Bean.MyData;
import com.example.week3.R;
import com.example.week3.Utils.Utils;
import com.example.week3.adapter.MyBaseAdapter;
import com.google.gson.Gson;

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

public class Three extends Fragment implements XListView.IXListViewListener{
private String mUrl = “

http://api.expoon.com/AppNews/getNewsList/type/1/p/1”;
private List<MyData.DataBean> mList = new ArrayList<>();
private MyBaseAdapter baseAdapter;
private XListView xListView;
private int start=0;

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View inflate = View.inflate(getActivity(), R.layout.three, null);
    initView(inflate);
    return inflate;
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    baseAdapter = new MyBaseAdapter(getActivity(), mList);
    xListView.setAdapter(baseAdapter);
    new MyAsync().execute(mUrl+start);
}

private void initView(View inflate) {
    xListView = (XListView) inflate.findViewById(R.id.lv);


    xListView.setPullLoadEnable(true);
    xListView.setXListViewListener(this);


}



class MyAsync extends AsyncTask<String, Void, List<MyData.DataBean>> {

    @Override
    protected List<MyData.DataBean> doInBackground(String... strings) {
        try {
            String str = Utils.getStr(strings[0]);
            Gson gson = new Gson();
            MyData myData = gson.fromJson(str, MyData.class);
            return myData.getData();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(List<MyData.DataBean> dataBeans) {
        super.onPostExecute(dataBeans);
        mList.addAll(dataBeans);
        baseAdapter.setmList(mList);
        stop();
    }
}

private void stop() {
    xListView.stopRefresh();
    xListView.stopLoadMore();
    xListView.setRefreshTime("剛剛");
}
//上拉重新整理
@Override
public void onRefresh() {
   mList.clear();
   start=1;
   new MyAsync().execute(mUrl+start);
}

//下拉載入
@Override
public void onLoadMore() {

 new MyAsync().execute(mUrl+start);
}

}
PullToRefresh解析資料
package com.example.week3.Fragment;

import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
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.example.pulltolibrary.PullToRefreshBase;
import com.example.pulltolibrary.PullToRefreshListView;
import com.example.week3.Bean.MyData;
import com.example.week3.R;
import com.example.week3.Utils.Utils;
import com.example.week3.adapter.MyBaseAdapter;
import com.google.gson.Gson;

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

public class Four extends Fragment {
private PullToRefreshListView Pull_To_Re;
private String mUrl=“http://api.expoon.com/AppNews/getNewsList/type/1/p/1”;
private List<MyData.DataBean> data=new ArrayList<>();
private int start=0;
private MyBaseAdapter baseAdapter;
private View inflate;

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    inflate = View.inflate(getActivity(), R.layout.four, null);
    initView(inflate);
     baseAdapter = new MyBaseAdapter(getActivity(), data);
     Pull_To_Re.setAdapter(baseAdapter);
    new Async().execute(mUrl+start);
    setListener();
    return inflate;
}

private void initView(View inflate) {
    Pull_To_Re = (PullToRefreshListView) inflate.findViewById(R.id.Pull_To_Re);
    //設定上下滑動
    Pull_To_Re.setMode(PullToRefreshBase.Mode.BOTH);
    baseAdapter = new MyBaseAdapter(getActivity(), data);
    Pull_To_Re.setAdapter(baseAdapter);

}
//開始寫邏輯程式碼

class Async extends AsyncTask<String,Void,List<MyData.DataBean>>{

  @Override
  protected List<MyData.DataBean> doInBackground(String... strings) {
      try {
          String str = Utils.getStr(strings[0]);
          Gson gson = new Gson();
          MyData myData = gson.fromJson(str, MyData.class);
          return  myData.getData();
      } catch (Exception e) {
          e.printStackTrace();
      }
      return null;
  }

    @Override
    protected void onPostExecute(List<MyData.DataBean> dataBeans) {
        super.onPostExecute(dataBeans);
        data.addAll(dataBeans);
       baseAdapter.setmList(data);
    }
}
//設定監聽事件
public void setListener(){
    Pull_To_Re.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
        @Override
        public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
           start=1;
           data.clear();
          new Async().execute(mUrl);
          Pull_To_Re.postDelayed(new Runnable() {
              @Override
              public void run() {
                  Pull_To_Re.onRefreshComplete();
              }
          },1000);
        }
        @Override
        public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
             start++;
            new Async().execute(mUrl);
            Pull_To_Re.postDelayed(new Runnable() {
                @Override
                public void run() {
                    Pull_To_Re.onRefreshComplete();
                }
            },1000);
        }
    });
}

}