//---------------主布局文件----------------------------
<ListView android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/lv"></ListView>
//---------------主Activity------------------------------
package com.example.listviewscroll;
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 com.example.listviewscroll.JingDian.ResultBean;
import com.example.listviewscroll.JingDian.ResultBean.RowsBean;
import com.google.gson.Gson;
import android.os.AsyncTask;
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.view.View.OnClickListener;
import android.widget.AbsListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ListView;
public class MainActivity extends Activity {
private ListView lv;
private List<RowsBean> rowsBeans = new ArrayList<JingDian.ResultBean.RowsBean>();
private MyAdapter adapter;
private List<RowsBean> list;
// 定义一个页数
private int ye = 0;
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;
}
}
};
//----------注意:使用了Gson.jar包-解析json串---------
// 解析数据
private void tojson(String text) {
Gson gson = new Gson();
// 类名
JingDian jingDian = gson.fromJson(text, JingDian.class);
ResultBean resultBean = jingDian.getResult();
// 添加
rowsBeans = resultBean.getRows();
Log.i("11111111111", rowsBeans.toString());
list.addAll(rowsBeans);
//更新适配器
if (adapter == null) {
adapter = new MyAdapter(list,this);
lv.setAdapter(adapter);
} else {
adapter.notifyDataSetChanged();
}
};
//------------------onCreate中------------------
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//找到控件Listview
lv = (ListView) findViewById(R.id.lv);
list=new ArrayList<JingDian.ResultBean.RowsBean>();
//获得网络数据
huodeshuju(ye);
//设置Listview的滑动监听
lv.setOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
switch (scrollState) {
//滚动结束
case OnScrollListener.SCROLL_STATE_IDLE:
//滚动停止时,判断如果滚动到底部
if (view.getLastVisiblePosition()==view.getCount()-1) {
// 使页数加1
ye++;
Toast.makeText(MainActivity.this, "" + ye, Toast.LENGTH_SHORT).show();
huodeshuju(ye);
adapter.notifyDataSetChanged();
}else if (view.getFirstVisiblePosition()==0) {
//滚动到顶部
// 重新定义为第一页
list.clear();
ye = 1;
Toast.makeText(MainActivity.this, "" + ye, Toast.LENGTH_SHORT).show();
// 获得数据
huodeshuju(ye);
// 刷新适配器
adapter.notifyDataSetChanged();
}
break;
//开始滚动
case OnScrollListener.SCROLL_STATE_FLING:
break;
//正在滚动
case OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
break;
default:
break;
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem,int visibleItemCount, int totalItemCount) {
}
});
}
// 获得数据
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");
// 获取本次网络请求的状态码
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);
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
};
}.start();
}
}