轉載請註明出處:http://blog.csdn.net/zhaokaiqiang1992
關於上拉刷新和下拉加載,已經有不少的解決方案了,XListView和PullToRefresh應該是被廣爲熟知的開源項目,項目很棒,可以解決我們的需求。但是,今天咱們用一種更簡單的方式,來實現上拉刷新和下拉加載功能,我叫它AutoLoadListView~
先來一張效果圖。
刷新效果使用19版本之後的v4兼容包的SwipeRefreshLayout實現,效果很漂亮,而自動加載下一頁的跳動效果,則是使用了另外一個開源項目JumpingBeans,這跳動的省略號,是不是很贊呢?
下面簡單介紹下使用方法
- 佈局文件
<RelativeLayout 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.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipe_refresh"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.socks.autoload.AutoLoadListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</com.socks.autoload.AutoLoadListView>
</android.support.v4.widget.SwipeRefreshLayout>
</RelativeLayout>
- 刷新。我們實現刷新需要實現SwipeRefreshLayout.OnRefreshListener接口,然後在onRefresh()實現刷新邏輯即可
public void onRefresh()
- 加載下一頁。我們需要添加一個接口,然後在回調中實現我們的加載任務即可
listView.setOnLoadNextListener(new OnLoadNextListener() {
@Override
public void onLoadNext() {
handler.sendEmptyMessageDelayed(MSG_LOAD_MORE, 3000);
}
});
是不是用起來很簡單捏?
有的同學可能問了,我要是想要側滑刪除咋辦?
囧…
又不是隻有這一種交互效果!爲啥抓着這種效果不放啊!用前面介紹的【凱子哥帶你夯實應用層】使用ActionMode實現有刪除動畫的多選刪除功能不行嘛!!!
好吧,既然你們產品經理沉迷於這種效果,咱們就做做吧…
- 第一步,找到我們的ZListView,不明真相的同學,請戳【Android界面實現】ZListView,一個最強大的刷新、加載、滑動刪除的ListView控件(一)
- 把除了ZlistView之外的所有類都拷貝到我們的項目
- 用AutoLoadListView替代ZListView即可
看下效果圖,也還ok啊~但是因爲是demo,所以很多業務沒處理,這個就需要大家根據自己的需求去改源碼了,開源項目的好處不就在此麼~