XListview的下拉刷新、上拉加载、用Pull解析XML
做之前需要导入XListview的文件,此是用第三方的xListview实现的,东西没写全。此是在Fragment中实现的
//--------------XListView的布局----------------
<me.XListView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/lv"></me.XListView>
//=============
package com.bw.test.fragment;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import me.XListView;
import me.XListView.IXListViewListener;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import com.bw.test.R;
import com.bw.test.adapter.MyAdapter;
import com.bw.test.bean.News;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.util.Xml;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
//-----设置了xlistview的监听
public class Fragment1 extends Fragment implements IXListViewListener{
private XListView lv;
private MyAdapter adapter;
//定义一个页数
private int ye=1;
//封装一个集合
private List<News> news=new ArrayList<News>();
private int STATE_1 = 1;
private int STATE_2 = 2;
private int STATE_3;
//----------------------------------------
//使用Handler 更新UI
private Handler handler=new Handler(){
public void handleMessage(Message msg) {
switch (msg.what) {
case 0:
//获得线程发来的数据
String text=(String) msg.obj;
//XML解析
toXml(text);
break;
default:
break;
}
}
};
//--------------------------------------
//使用PUll解析XML
private void toXml(String text) {
News newss=null;
try {
//判断,如果有数据先清空----如果让他显示上一页的数据就不用清空集合
if (STATE_3 == STATE_1) {
news .clear();
}
XmlPullParser parser=Xml.newPullParser();
ByteArrayInputStream byteArrayInputStream=new ByteArrayInputStream(text.getBytes());
parser.setInput(byteArrayInputStream, "utf-8");
int eventType=parser.getEventType();
while (eventType!=XmlPullParser.END_DOCUMENT) {
String name=parser.getName();
switch (eventType) {
case XmlPullParser.START_TAG:
if (name.equals("news")) {
newss=new News();
}else if (name.equals("id")) {
String id=parser.nextText();
newss.setId(id);
}
else if (name.equals("title")) {
String title=parser.nextText();
newss.setTitle(title);
}
else if (name.equals("body")) {
String body=parser.nextText();
newss.setBody(body);
}
else if (name.equals("commentCount")) {
String commentCount=parser.nextText();
newss.setCommentCount(commentCount);
}
else if (name.equals("author")) {
String author=parser.nextText();
newss.setAuthor(author);
}
else if (name.equals("pubDate")) {
String pubDate=parser.nextText();
newss.setPubDate(pubDate);
}
else if (name.equals("url")) {
String url=parser.nextText();
newss.setUrl(url);
}
break;
case XmlPullParser.END_TAG:
if (name.equals("news")) {
news.add(newss);
//更新适配器
if (adapter == null) {
adapter = new MyAdapter(news,getActivity());
lv.setAdapter(adapter);
} else {
adapter .notifyDataSetChanged();
}
//加载数据
load();
}
break;
default:
break;
}
eventType=parser.next();
}
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
};
//-----------------------------------------
private void load() {
//停止更新
lv.stopRefresh();
//加载更多
lv.stopLoadMore();
//更新时间
lv.setRefreshTime("刚刚");
}
//------------------------------------------
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment1_item, null);
lv=(XListView) view.findViewById(R.id.lv);
lv.setPullLoadEnable(true);
huodeshuju(ye);
adapter = new MyAdapter(news, getActivity());
lv.setAdapter(adapter);
lv.setXListViewListener(this);
return view;
}
//---------------------------------------
//获得网络数据的方法
private void huodeshuju(final int ye) {
//创建子线程,做耗时的操作,请求网络数据
new Thread(){
public void run() {
try {
//URL传入页数
URL url=new URL("http://www.oschina.net/action/api/news_list?catalog=1&pageIndex="+ye+"&pageSize=20");
HttpURLConnection urlConnection=(HttpURLConnection) url.openConnection();
urlConnection.setConnectTimeout(5000);
urlConnection.setReadTimeout(5000);
urlConnection.setRequestMethod("GET");
urlConnection.connect();
int code=urlConnection.getResponseCode();
if (code==200) {
InputStream inputStream=urlConnection.getInputStream();
BufferedReader reader=new BufferedReader(new InputStreamReader(inputStream));
String liner;
StringBuffer buffer=new StringBuffer();
while ((liner=reader.readLine())!=null) {
buffer.append(liner);
}
String str=buffer.toString();
Log.i("-------------", str);
Message message=new Message();
message.what=0;
message.obj=str;
//发送消息
handler.sendMessage(message);
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
};
}.start();
}
//--------------------------------
//实现xlistView监听方法的下拉刷新
@Override
public void onRefresh() {
STATE_3 = STATE_1;//设置判断
//获得网络数据
//下拉刷新时,将页数在设为1,从第一页开始
ye=1;
huodeshuju(ye);
}
//--------------------------------------
//实现xlistView监听方法的上拉加载更多
@Override
public void onLoadMore() {
STATE_3 = STATE_2;
//每一次上拉,让页数加1
ye++;
Log.i("页", ye+"");
//获得网络数据
huodeshuju(ye);
Log.i("页222", ye+"");
}
}
//==========无用参考================
//继承IXListViewListener
public class MainActivity extends Activity implements OnItemClickListener,IXListViewListener
private int STATE_1 = 1;
private int STATE_2 = 2;
private int STATE_3;
lv2 = (XListView) findViewById(R.id.lv);
lv2.setPullLoadEnable(true);//上拉刷新
lv2.setXListViewListener(this);//给xListView设置监听
//判断,如果有数据先清空
if (STATE_3 == STATE_1) {
news .clear();
}
//更新适配器
if (adapter == null) {
adapter = new MyAdapter(news,this);
lv2.setAdapter(adapter);
} else {
adapter .notifyDataSetChanged();
}
load();
public void load(){
//停止更新
lv2.stopRefresh();
//加载更多
lv2.stopLoadMore();
//更新时间
lv2.setRefreshTime("刚刚");
}
@Override
public void onRefresh() {
STATE_3 = STATE_1;//设置判断
huodeshuju();
}
@Override
public void onLoadMore() {
STATE_3 = STATE_2;
huodeshuju();
}