public class MainActivity extends Activity { private ListView listview_load; private View layout; // private LinearLayout layout; private MyBaseAdapter adapter; private List<Map<String, Object>> data; private boolean isbottom;// 是否爲最後一個數據標記 private String url = "http://m2.qiushibaike.com/article/list/suggest?page="; private int currentPage = 1;// 當前頁碼 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); this.listview_load = (ListView) this.findViewById(R.id.listview_load); layout = this.findViewById(R.id.layout); // layout = (LinearLayout) this.findViewById(R.id.layout); data = new ArrayList<Map<String, Object>>(); adapter = new MyBaseAdapter(data); listview_load.setAdapter(adapter); new MyAsyncTask().execute(url + currentPage); listview_load.setOnScrollListener(new OnScrollListener() { @Override public void onScrollStateChanged(AbsListView view, int scrollState) { switch (scrollState) { case OnScrollListener.SCROLL_STATE_IDLE:// 中止時 if (isbottom) { currentPage++; // 加載下一頁數據, 顯示進度條 layout.setVisibility(View.VISIBLE); new MyAsyncTask().execute(url + currentPage); } break; } } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { // 到達底部最後一條,判斷再加載數據 isbottom = ((firstVisibleItem + visibleItemCount) == totalItemCount); } }); } // 異步方法進行聯網下載 class MyAsyncTask extends AsyncTask<String, Void, byte[]> { @Override protected byte[] doInBackground(String... params) { HttpClient client = new DefaultHttpClient(); HttpGet get = new HttpGet(params[0]); try { Log.i("MainActivity", "-3-"); HttpResponse response = client.execute(get); if (response.getStatusLine().getStatusCode() == 200) { return EntityUtils.toByteArray(response.getEntity()); } Log.i("MainActivity", "-4-"); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } // 聯網下載完後立刻執行 @Override protected void onPostExecute(byte[] result) { if (result != null) { List<Map<String, Object>> list = parserJSON(new String(result)); adapter.data.addAll(list);// 將數據給到data-list adapter.notifyDataSetChanged();// 會調用MyBaseAdapter中的getView方法 Log.i("MainActivity", "-list-" + list.size()); } else { Toast.makeText(MainActivity.this, "網絡鏈接錯誤!!", Toast.LENGTH_SHORT).show(); } layout.setVisibility(View.GONE); } // JSON解析 private List<Map<String, Object>> parserJSON(String json) { List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); try { JSONObject obj = new JSONObject(json); JSONArray array = obj.getJSONArray("items"); for (int i = 0; i < array.length(); i++) { JSONObject object_items = array.getJSONObject(i); Map<String, Object> itemMap = new HashMap<String, Object>(); itemMap.put("content", object_items.getString("content")); itemMap.put("image", object_items.getString("image")); itemMap.put("comments_count", object_items.getString("comments_count")); list.add(itemMap); } Log.i("MainActivity", "itemMap"); } catch (JSONException e) { e.printStackTrace(); } return list; } } // BaseAdapter適配器的代碼填寫 class MyBaseAdapter extends BaseAdapter { private List<Map<String, Object>> data; public MyBaseAdapter(List<Map<String, Object>> data) { this.data = data; } @Override public int getCount() { return this.data.size(); } @Override public Object getItem(int position) { return this.data.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder vHolder; if (convertView == null) { vHolder = new ViewHolder(); convertView = getLayoutInflater().inflate( R.layout.listview_item, null); TextView text_content = (TextView) convertView .findViewById(R.id.text_content); TextView text_image = (TextView) convertView .findViewById(R.id.text_image); TextView comments_count = (TextView) convertView .findViewById(R.id.comments_count); vHolder.text_content = text_content; vHolder.text_image = text_image; vHolder.comments_count = comments_count; convertView.setTag(vHolder); } else { vHolder = (ViewHolder) convertView.getTag(); } vHolder.text_content.setText(this.data.get(position).get("content") .toString()); vHolder.comments_count.setText(this.data.get(position) .get("comments_count").toString()); // 圖片時另外判斷,固然這裏只是獲取圖片的名字,並無下載圖片,實際中文字和圖片是分開下載的 if (!this.data.get(position).get("image").equals("null")) { vHolder.text_image.setText(this.data.get(position).get("image") .toString()); } return convertView; } class ViewHolder { private TextView text_content; private TextView text_image; private TextView comments_count; } } } //主佈局 <ListView android:id="@+id/listview_load" android:layout_width="match_parent" android:layout_height="match_parent" /> <!-- 如下這個是進度條 --> <LinearLayout android:id="@+id/layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:background="#000" android:gravity="center" android:orientation="horizontal" android:visibility="gone" > <ProgressBar android:id="@+id/pb" style="?android:attr/progressBarStyleSmall" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/text_load" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="正在下載。。。。" android:textColor="#fff" /> </LinearLayout> //適配器自定義佈局 <TextView android:id="@+id/text_content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20sp" android:textColor="#00f" /> <TextView android:id="@+id/text_image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#f00" /> <TextView android:id="@+id/comments_count" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#999" />