import android.os.AsyncTask;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import com.example.pulltolibrary.PullToRefreshBase;
import com.example.pulltolibrary.PullToRefreshListView;
import com.google.gson.Gson;
import java.util.ArrayList;
import java.util.List;
import jiaoyibo.bawei.com.jiaoyibo20190109.R;
import jiaoyibo.bawei.com.jiaoyibo20190109.adapter.MyAdapter;
import jiaoyibo.bawei.com.jiaoyibo20190109.base.BaseFragment;
import jiaoyibo.bawei.com.jiaoyibo20190109.bean.Bean;
import jiaoyibo.bawei.com.jiaoyibo20190109.utils.NetWorkUtils;
/**
* <p>文件描述:<p>
* <p>作者:JYB<p>
* <p>创建时间:2019/1/9<p>
* <p>更改时间:2019/1/9<p>
* <p>版本号:1<p>
*/
public class TablayoutOne extends BaseFragment {
private List<Bean.DataBean> list = new ArrayList<>();
private String urlString = "http://api.expoon.com/AppNews/getNewsList/type/1/p/1";
private int page = 1;
private PullToRefreshListView pullToRefreshListView;
private MyAdapter adapter;
@Override
protected void initData() {
new MyAsyncTask().execute(urlString+page);
}
@Override
protected void initView(View view) {
pullToRefreshListView = view.findViewById(R.id.pulltoLibrary);
pullToRefreshListView.setMode(PullToRefreshListView.Mode.BOTH);
adapter = new MyAdapter(list, getActivity());
pullToRefreshListView.setAdapter(adapter);
pullToRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
@Override
public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
page=1;
list.clear();
initData();
}
@Override
public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
page++;
initData();
}
});
initData();
}
@Override
protected int getLayoutId() {
return R.layout.tablayout_01;
}
private class MyAsyncTask extends AsyncTask<String,Void,String> {
@Override
protected String doInBackground(String... strings) {
return NetWorkUtils.getNetGson(strings[0]);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Gson gson = new Gson();
Bean bean = gson.fromJson(s, Bean.class);
Log.e("sbean",bean+"");
List<Bean.DataBean> data = bean.getData();
list.addAll(data);
Log.e("datas",data+"");
adapter.notifyDataSetChanged();
//刷新头和刷新底部返回
pullToRefreshListView.onRefreshComplete();
}
}
}
PullToRefreshListView简单的展示
最新推荐文章于 2019-01-11 10:10:43 发布