Volley 之ListView ArrayAdapter 下滑加载数据
1:MyListViewActivity.java
public class MyListViewActivity extends Activity { private ListView listView = null; private boolean mHasData = false; private boolean mInError = false; private boolean mEnd = false; private ArrayList<String> mTitles = new ArrayList<String>(); private ArrayAdapter<String> mAdapter = null; private int offset = 15; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_my_list_view); listView = (ListView)findViewById(R.id.my_listView_test); mAdapter = new ArrayAdapter<String>( this, R.layout.list_test, R.id.tv_title, mTitles ); listView.setAdapter(mAdapter); listView.setOnScrollListener(new MyScrollListener()); } @Override protected void onResume() { super.onResume(); if (!mHasData && !mInError) { loadPage(); } } //滚动时一直回调,直到停止滚动时才停止回调。单击时回调一次。 //firstVisibleItem:当前能看见的第一个列表项ID(从0开始) //visibleItemCount:当前能看见的列表项个数(小半个也算) //totalItemCount:列表项共数 public class MyScrollListener implements OnScrollListener { private boolean isLastRow = false; public MyScrollListener(){} @Override public void onScroll(AbsListView arg0, int firstVisibleItem, int visibleItemCount, int totalItemCount) { //判断是否滚到最后一行 if (totalItemCount > 0 && (firstVisibleItem + visibleItemCount == totalItemCount)){ isLastRow = true; Log.i("++onScroll++", "onScroll"); } } @Override public void onScrollStateChanged(AbsListView arg0, int scrollState) { //当滚到最后一行且停止滚动时,执行加载 if (!mHasData && isLastRow && scrollState == AbsListView.OnScrollListener.SCROLL_STATE_IDLE) { loadPage(); isLastRow = false; Log.i("++loadPage++", "loadPage"); } if(!mEnd && mHasData && isLastRow){ Toast.makeText(MyListViewActivity.this, "数据全部加载完毕!", Toast.LENGTH_SHORT).show(); mEnd = true; } } } private void loadPage() { int startIndex = mTitles.size(); RequestQueue rQueue = MyVolley.getRequestQueue(); String url = "http://192.168.0.116/wsht/server/selectTitle2json.php?offset=" +offset+"&startIndex="+startIndex; JsonArrayRequest req = new JsonArrayRequest( url, createGetTitlesSuccess(), createGetTitlesError() ); rQueue.add(req); } private Response.Listener<JSONArray> createGetTitlesSuccess(){ return new Response.Listener<JSONArray>() { @Override public void onResponse(JSONArray response) { int len = response.length(); if(len < offset){ //此时,说明数据已经全部加载完毕 mHasData = true; //true表示没有数据了 } JSONObject temp = null; try{ for(int i = 0; i < len; i++){ //从JSONArray中获取每个JSONObject对象 temp = response.getJSONObject(i); String id = temp.getString("id"); String title = temp.getString("title"); mTitles.add(id+"---"+title); } mAdapter.notifyDataSetChanged(); }catch(JSONException e){ showErrorDialog(); } } }; } private Response.ErrorListener createGetTitlesError(){ return new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError arg0) { showErrorDialog(); } }; } private void showErrorDialog() { mInError = true; AlertDialog.Builder b = new AlertDialog.Builder(MyListViewActivity.this); b.setMessage("网络异常,加载数据失败!"); b.show(); } }
2:使用PHP+mysql 作为服务端
<?php require_once '../mysql/MysqlTool.class.php'; $mt=new MysqlTool(); $offset = $_GET['offset']; $startIndex = $_GET['startIndex']; $begin = $startIndex; $sql="select id,title from test limit $begin, $offset"; echo json_encode($mt->select($sql)); ?>