首先我们需要先导入pulltorefresh的jar包可以从王网上下载,其次导入imageloader的jar包
之后进行代码的开始编写
示例是一个对于图书分类点击进入后,分类书籍的加载
首先开始写一个utils类,
package com.wj.parser.utils;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
import android.content.Context;
import android.util.Log;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.wj.parser.bean.Book;
/**
* @author Administrator
* @date 2016-7-28
*/
public class Utils {
/**
* @param url
* @return 字节流
* @throws IOException
*/
public static InputStream getInput(String url) throws IOException {
URL urlPath = new URL(url);
HttpURLConnection http = (HttpURLConnection) urlPath.openConnection();
http.setReadTimeout(5000);
http.setConnectTimeout(5000);
http.connect();
if (http.getResponseCode() == 200) {
// 开始返回数据
return http.getInputStream();
}
return null;
}
public static ArrayList getData(InputStream input)
throws IOException, XmlPullParserException {
ArrayList list = null;
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
XmlPullParser pullParser = factory.newPullParser();
pullParser.setInput(input, "utf-8");
int type = pullParser.getEventType();
Book book = null;
while (type != XmlPullParser.END_DOCUMENT) {
String name = pullParser.getName();
switch (type) {
case XmlPullParser.START_DOCUMENT:
list = new ArrayList();
break;
case XmlPullParser.START_TAG:
if (name.equals("item")) {
book = new Book();
Log.i("kankan", name);
} else if (name.equals("catalog")) {
book.setCatalog(pullParser.nextText());
Log.i("kankan", name);
} else if (name.equals("id")) {
book.setId(pullParser.nextText());
Log.i("kankan", name);
}
break;
case XmlPullParser.END_TAG:
if (name.equals("item")) {
list.add(book);
Log.i("kankan", name);
}
break;
}
type = pullParser.next();
Log.i("kankan", type + "");
}
return list;
}
/**
* @param context
* @param url
* @return 从网络上获取数据
* @throws IOException
*/
public static String getDataFromHttp(Context context, String url)
throws IOException {
InputStream input = getInput(url);
byte[] by = new byte[1024];
int len = -1;
ByteArrayOutputStream bop = new ByteArrayOutputStream();
while ((len = input.read(by)) != -1) {
bop.write(by, 0, len);
}
return bop.toString("utf-8");
}
public static ImageLoader getImageLo(Context context) {
ImageLoader image = ImageLoader.getInstance();
image.init(ImageLoaderConfiguration.createDefault(context));
return image;
}
}
package com.wj.parser;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import org.xmlpull.v1.XmlPullParserException;
import com.wj.parser.base.BaseActivity;
import com.wj.parser.bean.Book;
import com.wj.parser.utils.Utils;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private final String URL = "http://apis.juhe.cn/goodbook/catalog?key=1d93c33c310af960cd9060727d38e8aa&dtype=xml";
private ListView lv;
private ArrayList list;
private long mExitTime;
private BaseActivity base;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
base=new BaseActivity();
base.onCreate(this);
initView();
new AsyncTask>() {
@Override
protected ArrayList doInBackground(String... params) {
try {
InputStream input = Utils.getInput(params[0]);
ArrayList data = Utils.getData(input);
return data;
} catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(java.util.ArrayList result) {
list = result;
lv.setAdapter(new BaseAdapter() {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = View.inflate(MainActivity.this, android.R.layout.simple_list_item_1, null);
TextView tv = (TextView) convertView.findViewById(android.R.id.text1);
tv.setText(list.get(position).getCatalog());
return convertView;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public int getCount() {
return list.size();
}
});
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView> parent, View view, int position, long id) {
// 设置跳转到详情页面
Intent intent = new Intent(MainActivity.this, IndexActivity.class);
// 带值传参
intent.putExtra("bean", list.get(position));
startActivity(intent);
}
});
};
}.execute(URL);
}
private void initView() {
lv = (ListView) findViewById(R.id.tt);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
if ((System.currentTimeMillis() - mExitTime) > 2000) {
Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
mExitTime = System.currentTimeMillis();
} else {
base.onDestroy();
}
return true;
}
return super.onKeyDown(keyCode, event);
}
}
package com.wj.parser;
import java.io.IOException;
import java.util.ArrayList;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshGridView;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.wj.parser.bean.Book;
import com.wj.parser.bean.MessageList;
import com.wj.parser.bean.MessageResult;
import com.wj.parser.utils.Utils;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
@SuppressLint("HandlerLeak")
public class IndexActivity extends Activity {
private PullToRefreshGridView gv;
private ImageLoader imageLoder;
private int x = 10;
private int y = 10;
private Book book;
private BaseAdapter base;
Handler mHandler = new Handler() {
public void handleMessage(android.os.Message msg) {
imageLoder = Utils.getImageLo(IndexActivity.this);
String str = null;
str = (String) msg.obj;
Gson gson = new Gson();
MessageResult result = gson.fromJson(str, MessageResult.class);
MessageList list = result.getResult();
final ArrayList data = list.getData();
base=new BaseAdapter() {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = View.inflate(IndexActivity.this, R.layout.gv_item, null);
ImageView ima = (ImageView) convertView.findViewById(R.id.ima_item);
TextView tv = (TextView) convertView.findViewById(R.id.tv_item);
imageLoder.displayImage(data.get(position).getImg(), ima);
tv.setText(data.get(position).getTitle());
return convertView;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public Object getItem(int position) {
return data.get(position);
}
@Override
public int getCount() {
return data.size();
}
};
gv.setAdapter(base);
gv.setOnRefreshListener(new OnRefreshListener() {
@Override
public void onRefresh(PullToRefreshBase refreshView) {
y = y + 10;
new Thread() {
public void run() {
try {
requestData(book, x, y);
} catch (IOException e) {
e.printStackTrace();
}
};
}.start();
Toast.makeText(IndexActivity.this, y+"", Toast.LENGTH_SHORT).show();
// 开始刷新
base.notifyDataSetChanged();
gv.onRefreshComplete();
}
});
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_index);
gv = (PullToRefreshGridView) findViewById(R.id.gv);
Intent intent = getIntent();
book = (Book) intent.getSerializableExtra("bean");
Log.i("kankan", book.getId() + "");
// 开始请求数据
new Thread() {
public void run() {
try {
requestData(book, x, y);
} catch (IOException e) {
e.printStackTrace();
}
};
}.start();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.index, menu);
return true;
}
private void requestData(final Book book, int x, int y) throws IOException {
String http = Utils.getDataFromHttp(IndexActivity.this, "http://apis.juhe.cn/goodbook/query?key=1d93c33c310af960cd9060727d38e8aa&catalog_id="
+ book.getId() + "&rn=" + x + "&rn=" + y);
Message msg = mHandler.obtainMessage();
msg.obj = http;
mHandler.sendMessage(msg);
}
}
最后 展示一下被pulltorefresh覆盖的控件