这是由 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);
}
}