上拉加载、下拉刷新功能实现
package com.example.my_xlistview_text;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.channels.GatheringByteChannel;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import com.example.adapter.MyAdapter;
import com.example.bean.BeanVO;
import com.example.fresh.XListView;
import com.example.fresh.XListView.IXListViewListener;
import com.google.gson.Gson;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Toast;
public class MainActivity extends Activity implements IXListViewListener{
private XListView xlv;
private int flag=10;
private List<String> list,initlist;
private MyAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//找控件
xlv = (XListView) findViewById(R.id.xlv);
//设置上拉下拉
xlv.setPullRefreshEnable(true);
xlv.setPullLoadEnable(true);
xlv.setXListViewListener(this);
list=new ArrayList<String>();
initlist=new ArrayList<String>();
adapter=new MyAdapter(list, this);
xlv.setAdapter(adapter);
//请求并解析数据
new Thread(){
public void run() {
getdata();
};
}.start();
}
/**
* 请求并解析数据
*/
public void getdata(){
//HttpClient请求数据
HttpClient client=new DefaultHttpClient();
HttpGet get=new HttpGet("http://op.juhe.cn/onebox/news/words?key=aadc8e1a325cd0b899e91639018d1769");
try {
HttpResponse response = client.execute(get);
if (response.getStatusLine().getStatusCode()==200) {
//得到数据
Log.i("请求数据", "刷新");
InputStream is=response.getEntity().getContent();
BufferedReader reader=new BufferedReader(new InputStreamReader(is));
StringBuffer sb=new StringBuffer();
String str;
while ((str=reader.readLine())!=null) {
sb.append(str);
}
String jsonstr=sb.toString();
//Gson解析
Gson gson=new Gson();
BeanVO bv=gson.fromJson(jsonstr, BeanVO.class);
list = bv.getResult();
initdata();
//发送消息
Message msg=handler.obtainMessage(1, initlist);
handler.sendMessage(msg);
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 展示的条目数
*/
public void initdata(){
initlist.clear();
if (list.size()>flag) {
for (int i = 0; i < flag; i++) {
initlist.add(list.get(i));
}
} else {
initlist.addAll(list);
}
}
/**
* 处理消息
*/
Handler handler=new Handler(){
public void handleMessage(android.os.Message msg) {
if (msg.what==1) {
//得到传来的数据
List<String> lists=(List<String>) msg.obj;
//刷新适配器展示数据
adapter.setNewData(lists);
}
};
};
/**
* 下拉刷新
*/
@Override
public void onRefresh() {
// TODO Auto-generated method stub
//请求数据
flag=10;
new Thread(){
public void run() {
getdata();
};
}.start();
onLoad();
}
/**
* 上拉加载
*/
@Override
public void onLoadMore() {
// TODO Auto-generated method stub
flag+=10;
initdata();
adapter.setNewData(initlist);
onLoad();
if (list.size()<flag) {
Toast.makeText(MainActivity.this, "没有更多数据", 1000).show();
}
}
/**
* 停止加载
*/
public void onLoad(){
xlv.stopLoadMore();
xlv.stopRefresh();
}
}
layout布局效果
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<com.example.fresh.XListView
android:id="@+id/xlv"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>