注意:此Demo用的是第三方的Xlistview.jar,需要复制me文件夹到项目中,两个XML布局文件和一张图片
把下面的复制到String中
<string name="xlistview_header_hint_normal">下拉刷新</string>
<string name="xlistview_header_hint_ready">松开刷新数据</string>
<string name="xlistview_header_hint_loading">正在加载...</string>
<string name="xlistview_header_last_time">上次更新时间:</string>
<string name="xlistview_footer_hint_normal">查看更多</string>
<string name="xlistview_footer_hint_ready">松开载入更多</string>
还要注意:用到了网络,所以需要加网络权限
//----------------主布局文件----------------------------------------
<me.XListView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/lv"></me.XListView>
//----======================--------------主Activity-----====================-------------------
package com.bwie.test;
import java.io.BufferedReader;
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.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.bwie.test.JingDian.ResultBean;
import com.bwie.test.JingDian.ResultBean.RowsBean;
import com.bwie.test.JingDian.ResultBean.RowsBean.InfoBean;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
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.ListView;
import android.widget.TextView;
public class MainActivity extends Activity implements IXListViewListener{
private String urlPath="http://api.fang.anjuke.com/m/android/1.3/shouye/recInfosV3/?city_id=14&lat=40.04652&lng=116.306033&api_key=androidkey&sig=9317e9634b5fbc16078ab07abb6661c5&macid=45cd2478331b184ff0e15f29aaa89e3e&app=a-ajk&_pid=11738&o=PE-TL10-user+4.4.2+HuaweiPE-TL10+CHNC00B260+ota-rel-keys%2Crelease-keys&from=mobile&m=Android-PE-TL10&cv=9.5.1&cid=14&i=864601026706713&v=4.4.2&qtime=20160411091603&pm=b61&uuid=1848c59c-185d-48d9-b0e9-782016041109&_chat_id=";
private List<RowsBean> rowsBeans=new ArrayList<JingDian.ResultBean.RowsBean>();
private XListView lv;
private TextView tv_jia;
private MyAdapter adapter;
//定义一个页数
private int ye=0;
private int STATE_1 = 1;
private int STATE_2 = 2;
private int STATE_3;
//定义一个集合用于刷新数据
private List<RowsBean> list;
private Handler handler=new Handler(){
public void handleMessage(Message msg) {
switch (msg.what) {
case 0:
//获得数据
String text=(String) msg.obj;
tojson(text);
break;
default:
break;
}
}
};
//解析数据
private void tojson(String text) {
//判断,如果有数据先清空----如果让他显示上一页的数据就不用清空集合
if (STATE_3 == STATE_1) {
list .clear();
}
Gson gson=new Gson();
//类名
JingDian jingDian=gson.fromJson(text, JingDian.class);
ResultBean resultBean=jingDian.getResult();
//添加
rowsBeans=resultBean.getRows();
//全部添加到集合
list.addAll(rowsBeans);
//更新适配器
if (adapter == null) {
adapter = new MyAdapter(list,this);
lv.setAdapter(adapter);
} else {
adapter .notifyDataSetChanged();
}
//加载数据
load();
};
private void load() {
//停止更新
lv.stopRefresh();
//加载更多
lv.stopLoadMore();
//更新时间
lv.setRefreshTime("刚刚");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//找到控件
lv = (XListView) findViewById(R.id.lv);
tv_jia = (TextView) findViewById(R.id.tv_jia);
//创建集合
list = new ArrayList<JingDian.ResultBean.RowsBean>();
lv.setPullLoadEnable(true);
//获得数据
huodeshuju(ye);
//设置xListview的监听
lv.setXListViewListener(this);
}
//---------------------------------
//获得数据
private void huodeshuju(final int ye) {
//使用线程
new Thread(){
public void run() {
try {
//先创建出了一个URL对象,urlPath:是我们访问接口地址
URL url=new URL("http://api.fang.anjuke.com/m/android/1.3/shouye/recInfosV3/?city_id=14&lat=40.04652&lng=116.306033&api_key=androidkey&sig=9317e9634b5fbc16078ab07abb6661c5&macid=45cd2478331b184ff0e15f29aaa89e3e&app=a-ajk&_pid=11738&o=PE-TL10-user+4.4.2+HuaweiPE-TL10+CHNC00B260+ota-rel-keys%2Crelease-keys&from=mobile&m=Android-PE-TL10&cv=9.5.1&cid=14&i=864601026706713&v=4.4.2&qtime=20160411091603&pm=b61&uuid=1848c59c-185d-48d9-b0e9-782016041109&_chat_id="+ye);
//URL链接对象,通过URL对象打开一个connection链接对像
HttpURLConnection urlConnection=(HttpURLConnection) url.openConnection();
//设置urlConnection对象链接超时
urlConnection.setConnectTimeout(5000);
//设置urlConnection对象获取数据超时
urlConnection.setReadTimeout(5000);
//设置本次urlConnection请求方式
urlConnection.setRequestMethod("GET");
//调用urlConnection的链接方法,线程等待,等待的是服务器所给我们返回的结果集
// urlConnection.connect();
//获取本次网络请求的状态码
int code=urlConnection.getResponseCode();
//如果本次返回的状态吗是200(成功)
if (code==200) {
//调用urlConnection.getInputStream得到本次请求所返回的结果流
InputStream inputStream=urlConnection.getInputStream();
//创建一个BufferedReader,去读取结果流
BufferedReader reader=new BufferedReader(new InputStreamReader(inputStream));
String readLine;
StringBuffer buffer=new StringBuffer();
while ((readLine=reader.readLine())!=null) {
buffer.append(readLine);
}
//读取完结果流之后所得到的结果
String result=buffer.toString();
Log.i("数据:----", ye+":"+result);
Message message=new Message();
message.what=0;
message.obj=result;
handler.sendMessage(message);
inputStream.close();
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
};
}.start();
}
@Override
public void onRefresh() {
STATE_3 = STATE_1;//设置判断
//获得网络数据
//下拉刷新时,将页数在设为1,从第一页开始
list.clear();
ye=0;
huodeshuju(ye);
}
@Override
public void onLoadMore() {
STATE_3 = STATE_2;
//每一次上拉,让页数加1
ye++;
Log.i("页", ye+"");
//获得网络数据
huodeshuju(ye);
Log.i("页222", ye+"");
}
}