1 首先要先粘贴3个Java文档 一个箭头图片 2个String文件 3个xml布局
2 在mainActivity中布局
<me.maxwin.view.XListView
android:id="@+id/xListView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:cacheColorHint="#00000000" >
</me.maxwin.view.XListView>
3 Java代码
3.1 找控件 mListView = (XListView) view.findViewById(R.id.xListView);
mListView.setPullLoadEnable(true);
// mAdapter = new ArrayAdapter<String>(this, R.layout.list_item, items);
// mListView.setAdapter(mAdapter);
// mListView.setPullLoadEnable(false);
// mListView.setPullRefreshEnable(false);
mListView.setXListViewListener(this);
// mHandler = new Handler();
isitData(1);
3.2 实现implements IXListViewListener接口
会出现2个方法
private void onLoad() {
mListView.stopRefresh();
mListView.stopLoadMore();
mListView.setRefreshTime("刚刚");
}
@Override
public void onRefresh() {
// TODO Auto-generated method stub
mListView.stopRefresh();
items.clear();
isitData(0);
mListView.setRefreshTime("刚刚");
}
@Override
public void onLoadMore() {
// TODO Auto-generated method stub
isitData(++i);
// 刷新适配器
handler.postDelayed(new Runnable() {
@Override
public void run() {
// adapter.notifyDataSetChanged();
onLoad();
}
}, 2000);
}
3.3找数据
private void isitData(final int pno) {
new Thread() {
public void run() {
String spec = "http://v.juhe.cn/weixin/query?key=c936dc59b0d7716aa27328de11975dc2&pno="+ pno;
try {
URL url = new URL(spec);
HttpURLConnection openConnection = (HttpURLConnection) url
.openConnection();
openConnection.setConnectTimeout(5000);
openConnection.setReadTimeout(5000);
int responseCode = openConnection.getResponseCode();
if (responseCode == 200) {
InputStream is = openConnection.getInputStream();
int len;
ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
byte[] by = new byte[1024];
while ((len = is.read(by)) != -1) {
arrayOutputStream.write(by, 0, len);
}
String string = arrayOutputStream.toString();
System.out.println(string);
handler.obtainMessage(STCCUE, string).sendToTarget();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
};
}.start();
}
3.4handler接收
if (msg.what == STCCUE) {
String string = (String) msg.obj;
Gson gson = new Gson();
json = gson.fromJson(string, Sanxiaoxi.class);
for (int i = 0; i < json.result.list.size(); i++) {
items.add(new Sanxiala(json.result.list.get(i).firstImg,
json.result.list.get(i).title, json.result.list
.get(i).url));
}
if (adapter==null) {
adapter = new SanXlistviewAdapter(
ThirdFragment.this.getActivity(), items);
mListView.setAdapter(adapter);
}else{
adapter.notifyDataSetChanged();
}
}
这样 一个下拉刷新就解决了
2 在mainActivity中布局
<me.maxwin.view.XListView
android:id="@+id/xListView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:cacheColorHint="#00000000" >
</me.maxwin.view.XListView>
3 Java代码
3.1 找控件 mListView = (XListView) view.findViewById(R.id.xListView);
mListView.setPullLoadEnable(true);
// mAdapter = new ArrayAdapter<String>(this, R.layout.list_item, items);
// mListView.setAdapter(mAdapter);
// mListView.setPullLoadEnable(false);
// mListView.setPullRefreshEnable(false);
mListView.setXListViewListener(this);
// mHandler = new Handler();
isitData(1);
3.2 实现implements IXListViewListener接口
会出现2个方法
private void onLoad() {
mListView.stopRefresh();
mListView.stopLoadMore();
mListView.setRefreshTime("刚刚");
}
@Override
public void onRefresh() {
// TODO Auto-generated method stub
mListView.stopRefresh();
items.clear();
isitData(0);
mListView.setRefreshTime("刚刚");
}
@Override
public void onLoadMore() {
// TODO Auto-generated method stub
isitData(++i);
// 刷新适配器
handler.postDelayed(new Runnable() {
@Override
public void run() {
// adapter.notifyDataSetChanged();
onLoad();
}
}, 2000);
}
3.3找数据
private void isitData(final int pno) {
new Thread() {
public void run() {
String spec = "http://v.juhe.cn/weixin/query?key=c936dc59b0d7716aa27328de11975dc2&pno="+ pno;
try {
URL url = new URL(spec);
HttpURLConnection openConnection = (HttpURLConnection) url
.openConnection();
openConnection.setConnectTimeout(5000);
openConnection.setReadTimeout(5000);
int responseCode = openConnection.getResponseCode();
if (responseCode == 200) {
InputStream is = openConnection.getInputStream();
int len;
ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
byte[] by = new byte[1024];
while ((len = is.read(by)) != -1) {
arrayOutputStream.write(by, 0, len);
}
String string = arrayOutputStream.toString();
System.out.println(string);
handler.obtainMessage(STCCUE, string).sendToTarget();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
};
}.start();
}
3.4handler接收
if (msg.what == STCCUE) {
String string = (String) msg.obj;
Gson gson = new Gson();
json = gson.fromJson(string, Sanxiaoxi.class);
for (int i = 0; i < json.result.list.size(); i++) {
items.add(new Sanxiala(json.result.list.get(i).firstImg,
json.result.list.get(i).title, json.result.list
.get(i).url));
}
if (adapter==null) {
adapter = new SanXlistviewAdapter(
ThirdFragment.this.getActivity(), items);
mListView.setAdapter(adapter);
}else{
adapter.notifyDataSetChanged();
}
}
这样 一个下拉刷新就解决了