XlistView会使我们工作中经常用到的自定义控件,所以今天简单的来学习一下它的用法
首先去github下载下来,倒入自己的工程,这个Demo里面用到了Xutils请求数据,pull解析XML数据等。
然后代码如下:记得找到XlistView控件之后要打开他刷新的开关才可以正常使用!同时记得在androidmanifest.xml中开启联网权限
public class MainActivity extends Activity implements IXListViewListener {
private XListView xListView;
private String uul = "http://www.oschina.net/action/api/tweet_list";
private News news;
private int down = 0;
private int pageIndex = 0;
private int pageSize = 20;
private int count = 0;
private Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
int count = msg.what;
switch (count) {
// 第一次进入程序时加载的数据
case 0:
ArrayList
list0 = (ArrayList
) msg.obj;
xListView.setAdapter(new MyAdapter(MainActivity.this, list0));
break;
// 下拉加载的数据
case 1:
ArrayList
list1 = (ArrayList
) msg.obj;
arr.addAll(list1);
xListView.setAdapter(new MyAdapter(MainActivity.this, arr));
// 停止刷新
xListView.stopRefresh();
break;
// 上拉加载的数据
case 2:
count++;
// 判断是否达到信息的总条数
int index = pageSize * count;
if (count <= 500) {
ArrayList
list2 = (ArrayList
) msg.obj; arr.addAll(list2); xListView.setAdapter(new MyAdapter(MainActivity.this, arr)); // 停止刷新 xListView.stopLoadMore(); } break; default: break; } }; }; private ArrayList
list; private ArrayList
arr; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 实例化一个空集合用来存加载的数据 arr = new ArrayList
(); // 找到控件 xListView = (XListView) findViewById(R.id.xlistView); // 设置xlistView的下拉上拉开关为true,才能刷新数据 xListView.setPullLoadEnable(true); xListView.setPullRefreshEnable(true); // 设置刷新监听 xListView.setXListViewListener(this); // 请求数据的方法 getDataFromNet(uul, 0); } private void getDataFromNet(String uul, final int i) { HttpUtils utils = new HttpUtils(); utils.send(HttpMethod.GET, uul, new RequestCallBack
() { @Override public void onFailure(HttpException arg0, String arg1) { // TODO Auto-generated method stub } @Override public void onSuccess(ResponseInfo
arg0) { // 将请求结果取出 String result = arg0.result; // 因为是XML,要用pull解析,所以要把字符串转换成输入流 InputStream is = new ByteArrayInputStream(result.getBytes()); // XML解析 ArrayList
list = parseXml(is); Message msg = new Message(); msg.what = i; msg.obj = list; handler.sendMessage(msg); } }); } private ArrayList
parseXml(InputStream result) { // 获取解析者 XmlPullParser pullParser = Xml.newPullParser(); try { pullParser.setInput(result, "utf-8"); list = new ArrayList
(); // 获取事件类型 int eventType = pullParser.getEventType(); // 判断事件类型 while (eventType != pullParser.END_DOCUMENT) { switch (eventType) { // 判断节点名字 case XmlPullParser.START_TAG: // 获取节点名字 String startName = pullParser.getName(); if (startName.equals("tweet")) { news = new News(); } else if (startName.equals("id")) { news.setId(pullParser.nextText()); } else if (startName.equals("portrait")) { news.setPortrait(pullParser.nextText()); } else if (startName.equals("author")) { news.setAuthor(pullParser.nextText()); } else if (startName.equals("body")) { news.setBody(pullParser.nextText()); } else if (startName.equals("imgSmall")) { news.setImgSmall(pullParser.nextText()); } break; case XmlPullParser.END_TAG: String endName = pullParser.getName(); if (endName.equals("tweet")) { list.add(news); } break; default: break; } eventType = pullParser.next(); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return list; } // 下拉刷新 @Override public void onRefresh() { // 请求数据的方法 getDataFromNet(uul, 1); } // 上拉加载 @Override public void onLoadMore() { pageIndex++; // 重新传递一个网址 String uul2 = "http://www.oschina.net/action/api/tweet_list?pageIndex=" + pageIndex; // 请求数据的方法 getDataFromNet(uul2, 2); } }