此篇文章注释较少(下载数据使用的是Volley),如若有不解之处,
可以参考另一篇博客(下载数据使用的是Xutils):
《PullToRefresh下拉刷新,上拉分页,返回顶部》
package com.myapplication.newpagerrefreshlistview;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.widget.ListView;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.handmark.pulltorefresh.library.ILoadingLayout;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.myapplication.newpagerrefreshlistview.VolleyUtils.MyDataUtils;
import com.myapplication.newpagerrefreshlistview.VolleyUtils.StringCallBack;
import com.myapplication.newpagerrefreshlistview.VolleyUtils.VolleyUtils;
import org.json.JSONException;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private PullToRefreshListView pullToRefreshListView;
private String path = "http://api.fengniao.com/app_ipad/pic_bbs_list_v2." +
"php?appImei=99000505856649&osType=Android&osVersion=4.1.1&fid=125&page=";
//封装好的下载工具
private VolleyUtils volleyUtils;
private List<Bean> totallist = new ArrayList<>();
private ListViewAdapter listViewAdapter;
private int page = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
getData(1);
setPullListener();
}
private void init() {
volleyUtils = new VolleyUtils();
pullToRefreshListView = (PullToRefreshListView) findViewById(R.id.refresh_to_update_next_pager);
//先设置拉动模式(常见3种),
pullToRefreshListView.setMode(PullToRefreshBase.Mode.BOTH);
//设置刷新界面,拿到刷新界面
ILoadingLayout loadingLayout = pullToRefreshListView.getLoadingLayoutProxy(true, true);
//设置加载时候的图片
loadingLayout.setLoadingDrawable(getResources().getDrawable(R.mipmap.ic_launcher));
//最近更新,一般还要设置更新时间
loadingLayout.setLastUpdatedLabel(MyDataUtils.getCurrentDate());
//设置刷新时候的文字
loadingLayout.setRefreshingLabel("拼命加载中,莫捉急……");
//设置拉动和释放时候的文字
loadingLayout.setPullLabel("轻一点拉动,爱护屏幕……");
loadingLayout.setRefreshingLabel("释放中……");
listViewAdapter = new ListViewAdapter(this, totallist);
pullToRefreshListView.setAdapter(listViewAdapter);
}
private void setPullListener() {
pullToRefreshListView.setOnRefreshListener(
new PullToRefreshBase.OnRefreshListener2<ListView>() {
@Override
public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
//TODo 下拉时触发的事件
new GetDataTask().execute();//不加此行,刷新不会停止,有bug待解决
}
@Override
public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
//TODO 上拉时触发的事件
page++;
getData(page);
new GetDataTask().execute();//不加此行,刷新不会停止,有bug待解决
}
});
}
public void getData(int page) {
volleyUtils.getStringData(path + page, new StringCallBack() {
@Override
public void getStringData(String data) {
//TODO json解析
//jsonToList(data);
fastJsonToList(data);
}
});
}
List<Bean> fastJsonToList(String s) {
if (!TextUtils.isEmpty(s)) {
JSONObject json = JSON.parseObject(s);
JSONArray array = json.getJSONArray("list");
List<Bean> list = JSON.parseArray(array.toJSONString(), Bean.class);
totallist.addAll(list);
listViewAdapter.notifyDataSetChanged();
if (list.size() != 0) {
return list;
}
}
return null;
}
List<Bean> jsonToList(String s) {
List<Bean> list = new ArrayList<>();
try {
org.json.JSONObject object = new org.json.JSONObject(s);
org.json.JSONArray array = object.getJSONArray("list");
for (int i = 0; i < array.length(); i++) {
org.json.JSONObject j = array.getJSONObject(i);
String pic_url = j.getString("pic_url");
String title = j.getString("title");
Bean bean = new Bean();
bean.setPic_url(pic_url);
bean.setTitle(title);
list.add(bean);
}
totallist.addAll(list);
listViewAdapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
return list;
}
private class GetDataTask extends AsyncTask<Void, Void, List<Bean>> {
@Override
protected List<Bean> doInBackground(Void... params) {
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
}
return totallist;
}
@Override
protected void onPostExecute(List<Bean> result) {
listViewAdapter.notifyDataSetChanged();
pullToRefreshListView.onRefreshComplete();
super.onPostExecute(result);
}
}
}
ListViewAdapter中代码:
package com.myapplication.newpagerrefreshlistview;
import android.content.Context;
import android.graphics.Bitmap;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.myapplication.newpagerrefreshlistview.VolleyUtils.BitmapCallBack;
import java.util.List;
/** * Created by Administrator on 2016/3/8. */
public class ListViewAdapter extends MyBaseAdapter<Bean> {
public ListViewAdapter(Context context, List<Bean> list) {
super(context, list);
}
@Override
public View getItemView(int position, View convertView, ViewGroup parent) {
final ViewHolder viewHolder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.item, parent, false);
viewHolder = new ViewHolder();
viewHolder.img = (ImageView) convertView.findViewById(R.id.iv_item_pic);
viewHolder.txt = (TextView) convertView.findViewById(R.id.tv_item_txt);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
//数据显示
Bean bean = list.get(position);
viewHolder.txt.setText(bean.getTitle());
utils.getImageBitmap(bean.getPic_url(), new BitmapCallBack() {
@Override
public void getBitmap(Bitmap bitmap) {
viewHolder.img.setImageBitmap(bitmap);
}
});
return convertView;
}
class ViewHolder {
ImageView img;
TextView txt;
}
}