曾幾何時,自 Android 發布之後一直以來就被管以 Geek 的玩具,而不太注重介面設計和使用者互動設計,這讓 Android 開發人員也順其自然的接受了這個不好的頭銜,但我一度認為這隻是不思進取的行為,誰說 Android 就不能像 iPhone 的介面那樣設計,我認為只有用心,沒神馬不能實現的。說回正題,第一次看到 Android 有這項功能的莫過於改版後的 Twitter,我認為它絕對是一個標榜性的 App(同時也包括已開源許久的Foursquare)。於是全世界的開發人員就開始尋思這個效果
Twitter 是如何?的,於是就有了...
這是由
johannilsson 以及眾位其好友的研究成果,並已 Apache 2.0 協議開源託管在 Github:https://github.com/johannilsson/android-pulltorefresh
大概看了一下源碼,發現和我初步的想法類似,除了需要整合系統的 ListView 之外,只能在 HeaderView 上面做文章,外加配合 onTouchEvent, onScroll 事件可以捕捉使用者下拉和上滑的事件監聽。
用法:
Layout
<!-- The PullToRefreshListView replaces a standard ListView widget.--><com.markupartist.android.widget.PullToRefreshListView android:id="@+id/android:list" android:layout_height="fill_parent" android:layout_width="fill_parent" />
Activity
// Set a listener to be invoked when the list should be refreshed.((PullToRefreshListView) getListView()).setOnRefreshListener(new OnRefreshListener() { @Override public void onRefresh() { // Do work to refresh the list here. new GetDataTask().execute(); }}); private class GetDataTask extends AsyncTask<Void, Void, String[]> { ... @Override protected void onPostExecute(String[] result) { mListItems.addFirst("Added after refresh..."); // Call onRefreshComplete when the list has been refreshed. ((PullToRefreshListView) getListView()).onRefreshComplete(); super.onPostExecute(result); }}
?倉庫裡除了實作類別外,還有一個執行個體,看看人家夠意思吧。