Android: ListView數據的分批加載 以及 Handle 總結
這是效果圖:
activity_main.xml
<LinearLayout 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" > <ListView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/listview" /> </LinearLayout>
listview_item.xml listview每一條
<LinearLayout 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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/text" /> </LinearLayout>
footer.xml 作為每一頁翻到最后時顯示:正在加載中.....
<LinearLayout 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" android:orientation="horizontal" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" > <ProgressBar style="?android:attr/process" android:layout_width="50dp" android:layout_height="wrap_content" /> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:text="正在加載....." android:textSize="18dp" /> </LinearLayout>
下面是主界面:
public class MainActivity extends Activity { private ListView listview; private List<String> data = new ArrayList<String>(); private ArrayAdapter<String> adapter; private LayoutInflater inflater; private View footer;// 頁腳-正在加載中..... @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); inflater = getLayoutInflater(); footer = inflater.inflate(R.layout.footer, null); listview = (ListView) findViewById(R.id.listview); listview.setOnScrollListener(new scrollListener()); data.addAll(DataService.getData(0, 20)); adapter = new ArrayAdapter<String>(this, R.layout.listview_item, R.id.text, data); /* 在適配器之前加頁腳,這樣適配器會重新被封裝成 '有頁腳的適配器' */ listview.addFooterView(footer); listview.setAdapter(adapter); listview.removeFooterView(footer); } /** * listview滾動監聽類 * */ public class scrollListener implements OnScrollListener { int pagesize = 20;// 每頁顯示條目 int maxpage = 5;// 最多頁數 int currentpage;// 當前頁 int nextpage; boolean finish_load = true;// 加載是否完成,默認完成 /** * 監聽滾動狀態改變:1-手指正在滑動 2-手指停止滑動 3-組件停止滾動 */ public void onScrollStateChanged(AbsListView view, int scrollState) { } /** * firstVisibleItem:第一個可見item visibleItemCount:可見item數量 * totalItemCount:總條目數量 */ public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { final int total = totalItemCount; /* 如果滾動到最后一條 */ if (listview.getLastVisiblePosition() + 1 == totalItemCount) { if (totalItemCount > 0) { /* 獲取當前頁 */ currentpage = totalItemCount % pagesize == 0 ? totalItemCount / pagesize : totalItemCount / pagesize + 1; nextpage = currentpage + 1; /* * 如果當前頁小于規定的最大頁數,并且加載完成(不斷滾動就會不斷執行onScroll方法, * 所以用finish_load鎖定翻頁) */ if (nextpage <= maxpage && finish_load) { finish_load = false; /* 每次翻頁前添加頁腳 */ listview.addFooterView(footer); /* 創建子線程,執行翻頁 */ new Thread(new Runnable() { public void run() { try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } List<String> l = DataService.getData(total, pagesize); handle.sendMessage(handle.obtainMessage(123, l)); } }).start(); } } } } /* 通過handle和主線程通訊,主線程接收消息更新UI */ Handler handle = new Handler() { public void handleMessage(Message msg) { data.addAll((List<String>) msg.obj); adapter.notifyDataSetChanged(); /* 頁腳顯示完就刪掉 */ if (listview.getFooterViewsCount() > 0) listview.removeFooterView(footer); finish_load = true; }; }; } }
獲取數據service (模擬從網上獲取)
public class DataService { /** * * @param startposition * :從第startposition條數據開始加載 * @param pagesize * :每頁顯示數量 * @return:服務器返回數組 */ public static List<String> getData(int startposition, int pagesize) { List<String> list = new ArrayList<String>(); for (int i = startposition; i <startposition+ pagesize; i++) { list.add("第" + i + "條數據"); } return list; } }
下面是Handle詳解:
一、Handler的定義:
主要接受子線程發送的數據, 并用此數據配合主線程更新UI.
解釋: 當應用程序啟動時,Android首先會開啟一個主線程 (也就是UI線程) , 主線程為管理界面中的UI控件,進行事件分發, 比如說, 你要是點擊一個 Button ,Android會分發事件到Button上,來響應你的操作。 如果此時需要一個耗時的操作,例如: 聯網讀取數據, 或者讀取本地較大的一個文件的時候,你不能把這些操作放在主線程中,,如果你放在主線程中的話,界面會出現假死現象, 如果5秒鐘還沒有完成的話,,會收到Android系統的一個錯誤提示 "強制關閉". 這個時候我們需要把這些耗時的操作,放在一個子線程中,因為子線程涉及到UI更新,,Android主線程是線程不安全的,也就是說,更新UI只能在主線程中更新,子線程中操作是危險的. 這個時候,Handler就出現了.,來解決這個復雜的問題 , 由于Handler運行在主線程中(UI線程中), 它與子線程可以通過Message對象來傳遞數據, 這個時候,Handler就承擔著接受子線程傳過來的(子線程用sedMessage()方法傳弟)Message對象,(里面包含數據) , 把這些消息放入主線程隊列中,配合主線程進行更新UI。
二、Handler一些特點
handler可以分發Message對象和Runnable對象到主線程中, 每個Handler實例,都會綁定到創建他的線程中(一般是位于主線程),
它有兩個作用: (1): 安排消息或Runnable 在某個主線程中某個地方執行, (2)安排一個動作在不同的線程中執行
Handler中分發消息的一些方法
post(Runnable)
postAtTime(Runnable,long)
postDelayed(Runnable long)
sendEmptyMessage(int)
sendMessage(Message)
sendMessageAtTime(Message,long)
sendMessageDelayed(Message,long)
以上post類方法允許你排列一個Runnable對象到主線程隊列中,
sendMessage類方法, 允許你安排一個帶數據的Message對象到隊列中,等待更新.