Android--PullToRefreshListView 的簡單使用


  原文:  http://blog.csdn.net/lmj623565791/article/details/38238749;

pull-to-refresh對ListView進行了封裝,叫做:PullToRefreshListView,用法和listview沒什么區別,下面看demo.

<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" >

    <com.handmark.pulltorefresh.library.PullToRefreshListView
        xmlns:ptr="http://schemas.android.com/apk/res-auto"
        android:id="@+id/pull_refresh_list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:cacheColorHint="#00000000"
        android:divider="#19000000"
        android:dividerHeight="4dp"
        android:fadingEdge="none"
        android:fastScrollEnabled="false"
        android:footerDividersEnabled="false"
        android:headerDividersEnabled="false"
        android:smoothScrollbar="true" >
    </com.handmark.pulltorefresh.library.PullToRefreshListView>

</RelativeLayout>
代碼極其簡單,得到PullToRefreshListView控件,然后像ListView一樣設置數據集。當然了,我們有下拉刷新,所以必須設置下拉刷新的回調:

 

setOnRefreshListener(new OnRefreshListener<ListView>(){}

 // 設置監聽事件  
        mPullRefreshListView  
                .setOnRefreshListener(new OnRefreshListener<ListView>()  
                {  
                    @Override  
                    public void onRefresh(  
                            PullToRefreshBase<ListView> refreshView)  
                    {  
                        String label = DateUtils.formatDateTime(  
                                getApplicationContext(),  
                                System.currentTimeMillis(),  
                                DateUtils.FORMAT_SHOW_TIME  
                                        | DateUtils.FORMAT_SHOW_DATE  
                                        | DateUtils.FORMAT_ABBREV_ALL);  
                        // 顯示最后更新的時間  
                        refreshView.getLoadingLayoutProxy()  
                                .setLastUpdatedLabel(label);  
  
                        // 模擬加載任務  
                        new GetDataTask().execute();  
                    }  
                });  

 

2、添加上拉加載更多

如過希望實現上拉加載更多,那么首先需要在布局文件的聲明屬性中添加一個屬性,用於指定目前的下拉模式:

<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" >

    <com.handmark.pulltorefresh.library.PullToRefreshListView
        xmlns:ptr="http://schemas.android.com/apk/res-auto"
        android:id="@+id/pull_refresh_list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:cacheColorHint="#00000000"
        android:divider="#19000000"
        android:dividerHeight="4dp"
        android:fadingEdge="none"
        android:fastScrollEnabled="false"
        android:footerDividersEnabled="false"
        android:headerDividersEnabled="false"
        android:smoothScrollbar="true"
        ptr:ptrMode="both" >
    </com.handmark.pulltorefresh.library.PullToRefreshListView>

</RelativeLayout>

我們添加了一個屬性:ptr:ptrMode="both" ,意思:上拉和下拉都支持。

可選值為:disabled(禁用下拉刷新),pullFromStart(僅支持下拉刷新),pullFromEnd(僅支持上拉刷新),both(二者都支持),manualOnly(只允許手動觸發)

當然了,如果你不喜歡在布局文件中指定,完全可以使用代碼設置,在onCreate里面寫:mPullRefreshListView.setMode(Mode.BOTH);//設置你需要的模式

 

   mPullRefreshListView.setOnRefreshListener(new OnRefreshListener2<ListView>(){});注意這里的接口類型是OnRefreshListener2,多了個2,和上面的不一樣,這個接口包含兩個方法,一個上拉回調,一個下拉回調。好了,這樣我們就成功添加了上拉與下拉,並且分別可以控制其回調代碼。

mPullRefreshListView  
                .setOnRefreshListener(new OnRefreshListener2<ListView>()  
                {  
                    @Override  
                    public void onPullDownToRefresh(  
                            PullToRefreshBase<ListView> refreshView)  
                    {  
                        Log.e("TAG", "onPullDownToRefresh");  
                        //這里寫下拉刷新的任務  
                        new GetDataTask().execute();  
                    }  
  
                    @Override  
                    public void onPullUpToRefresh(  
                            PullToRefreshBase<ListView> refreshView)  
                    {  
                        Log.e("TAG", "onPullUpToRefresh");  
                        //這里寫上拉加載更多的任務  
                        new GetDataTask().execute();  
                    }  
                });  

 

3、設置下拉刷新的圖標

 <com.handmark.pulltorefresh.library.PullToRefreshGridView
        ptr:ptrDrawable="@drawable/ic_launcher"
        ...
        />

當然了這是旋轉的效果,一般常用的還有,一個箭頭倒置的效果,其實也很簡單,一個屬性:

ptr:ptrAnimationStyle="flip"

去掉 ptr:ptrDrawable="@drawable/ic_launcher"這個屬性,如果你希望用下圖默認的箭頭,你也可以自定義。

ptr:ptrAnimationStyle的取值:flip(翻轉動畫), rotate(旋轉動畫) 。 

ptr:ptrDrawable則就是設置圖標了。

  

4、自定義下拉指示器文本內容等效果

可以在初始化完成mPullRefreshListView后,通過mPullRefreshListView.getLoadingLayoutProxy()可以得到一個ILoadingLayout對象,這個對象可以設置各種指示器中的樣式、文本等。

ILoadingLayout startLabels = mPullRefreshListView
                .getLoadingLayoutProxy();
        startLabels.setPullLabel("你可勁拉,拉...");// 剛下拉時,顯示的提示
        startLabels.setRefreshingLabel("好嘞,正在刷新...");// 刷新時
        startLabels.setReleaseLabel("你敢放,我就敢刷新...");// 下來達到一定距離時,顯示的提示

 設置上次刷新時間:

String label = DateUtils.formatDateTime(  
                                getApplicationContext(),  
                                System.currentTimeMillis(),  
                                DateUtils.FORMAT_SHOW_TIME  
                                        | DateUtils.FORMAT_SHOW_DATE  
                                        | DateUtils.FORMAT_ABBREV_ALL);  
  
                        // Update the LastUpdatedLabel  
                        refreshView.getLoadingLayoutProxy()  
                                .setLastUpdatedLabel(label);  
5、常用的一些屬性

當然了,pull-to-refresh在xml中還能定義一些屬性:

ptrMode,ptrDrawable,ptrAnimationStyle這三個上面已經介紹過。

ptrRefreshableViewBackground 設置整個mPullRefreshListView的背景色

ptrHeaderBackground 設置下拉Header或者上拉Footer的背景色

ptrHeaderTextColor 用於設置Header與Footer中文本的顏色

ptrHeaderSubTextColor 用於設置Header與Footer中上次刷新時間的顏色

ptrShowIndicator如果為true會在mPullRefreshListView中出現icon,右上角和右下角,挺有意思的。

ptrHeaderTextAppearance , ptrSubHeaderTextAppearance分別設置拉Header或者上拉Footer中字體的類型顏色等等。

ptrRotateDrawableWhilePulling當動畫設置為rotate時,下拉是是否旋轉。

ptrScrollingWhileRefreshingEnabled刷新的時候,是否允許ListView或GridView滾動。覺得為true比較好。

ptrListViewExtrasEnabled 決定了Header,Footer以何種方式加入mPullRefreshListView,true為headView方式加入,就是滾動時刷新頭部會一起滾動。

最后2個其實對於用戶體驗還是挺重要的,如果設置的時候考慮下~。其他的屬性自己選擇就好。

注:上述屬性很多都可以代碼控制,如果有需要可以直接mPullRefreshListView.set屬性名 查看

以上為pull-to-refresh所有支持的屬性~~


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM