欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

分享Android中pullToRefresh的使用心得

 更新時間:2015年12月20日 15:17:17   作者:石小峰  
這篇文章主要介紹了分享Android中pullToRefresh的使用心得的相關(guān)資料,需要的朋友可以參考下

pullToRefresh的導(dǎo)入

首先,點(diǎn)擊new按鈕 -> import Module

 

然后在 New Module界面選擇已經(jīng)在本地的含有源代碼的pullToRefresh。

打開如下圖所示的open Module Settings 按鈕

 

點(diǎn)擊app中的Dependencies 中右邊框的"+"按鈕,選擇第三個 ,如下所示

 

選擇Modules : pullToRefreshLibrary ,點(diǎn)擊OK

 然后在build.gradle(Module:app)或者你自己要寫的那個android 程序的根文件夾的build.gradle中加入下面一句話

 compile project(':pullToRefreshLibrary')

自此,pullToRefresh已經(jīng)導(dǎo)入成功,可以新建一個pullToRefrenshListView驗(yàn)證一下。

pullToRefreshListView的基本使用

pullToRefreshListView和ListView的使用基本差的不多,只不過ListView的xml要換成

com.handmark.pulltorefresh.library.PullToRefreshListView

 例子如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical">
  <com.handmark.pulltorefresh.library.PullToRefreshListView
    xmlns:ptr="http://schemas.android.com/apk/res-auto"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:id="@+id/refresh_list_view"
    ptr:ptrDrawable="@drawable/default_ptr_flip"
    ptr:ptrAnimationStyle="flip"
    ptr:ptrHeaderBackground="#383838"
    ptr:ptrHeaderTextColor="#FFFFFF" >
  </com.handmark.pulltorefresh.library.PullToRefreshListView>
</LinearLayout> 

上面的例子中pullToRefreshListView中多了幾個屬性 分別以ptr開頭,這是指定pullToRefreshListView在刷新的時候出現(xiàn)的特效,比如第一個是指定刷新時顯示的圖片,第二個是指定刷新的圖片以何種方式顯示出來,第三個是指定刷新時頭部的背景,第四個是指定刷新時頭部字體的顏色。

以上這些都可以在代碼中設(shè)置。

ListView中每個item的xml還是不變的,adapter的使用和寫法也是不變的,需要改變的只有設(shè)定刷新事件。

接下來在代碼中設(shè)定pullToRefreshListView的一些基本屬性和事件。

步驟一 綁定控件,設(shè)置屬性

綁定控件代碼如下:

 private PullToRefreshListView listview;
listview = (PullToRefreshListView) findViewById(R.id.refresh_list_view); 

設(shè)置刷新時顯示的刷新狀態(tài)

 //對pullToListView綁定adapter
listview.setAdapter(adapter);
 /*設(shè)置pullToRefreshListView的刷新模式,BOTH代表支持上拉和下拉,PULL_FROM_END代表上拉,PULL_FROM_START代表下拉 */ 
listview.setMode(PullToRefreshBase.Mode.BOTH);
initRefreshListView(); 
initRefreshListView方法設(shè)置刷新顯示的狀態(tài)
 public void initRefreshListView() {
  ILoadingLayout Labels = listview.getLoadingLayoutProxy(true, true);
  Labels.setPullLabel("快點(diǎn)拉");
  Labels.setRefreshingLabel("正在拉");
  Labels.setReleaseLabel("放開刷新");
} 

這里通過getLoadingLayoutProxy 方法來指定上拉和下拉時顯示的狀態(tài)的區(qū)別,第一個true 代表下來狀態(tài) ,第二個true 代表上拉的狀態(tài) 。如果想?yún)^(qū)分上拉和下拉狀態(tài)的不同,可以分別設(shè)置getLoadingLayoutProxy ,例子如下:

 public void initRefreshListView(){  
  ILoadingLayout startLabels = pullToRefresh  
        .getLoadingLayoutProxy(true, false);  
  startLabels.setPullLabel("下拉刷新");  
  startLabels.setRefreshingLabel("正在拉");  
  startLabels.setReleaseLabel("放開刷新");  
  ILoadingLayout endLabels = pullToRefresh.getLoadingLayoutProxy(  
        false, true);  
  endLabels.setPullLabel("上拉刷新");  
  endLabels.setRefreshingLabel("正在載入..."); 
  endLabels.setReleaseLabel("放開刷新..."); 

這樣pullToRefreshListView刷新時狀態(tài)就設(shè)定好了。

步驟二 pullToRefreshListView監(jiān)聽事件的設(shè)置

這里主要設(shè)置setOnRefreshListener 事件,根據(jù)剛才設(shè)置的不同的刷新模式,在里面寫的匿名內(nèi)部類也不一樣。

 規(guī)則如下:

 如果Mode設(shè)置成Mode.BOTH,需要設(shè)置刷新Listener為OnRefreshListener2,并實(shí)現(xiàn)onPullDownToRefresh()、onPullUpToRefresh()兩個方法。 

如果Mode設(shè)置成Mode.PULL_FROM_START或Mode.PULL_FROM_END,需要設(shè)置刷新Listener為OnRefreshListener,同時實(shí)現(xiàn)onRefresh()方法。

當(dāng)然也可以設(shè)置為OnRefreshListener2,但是Mode.PULL_FROM_START的時候只調(diào)用onPullDownToRefresh()方法,Mode.PULL_FROM_END的時候只調(diào)用onPullUpToRefresh()方法.

這樣在進(jìn)入該Activity時候,手動上拉和下拉就會實(shí)現(xiàn)刷新和加載。

 如果想剛進(jìn)入Activity的時候就執(zhí)行加載,則要調(diào)用如下方法

 listview.setRefreshing();

接下來只需要在onPullDownToRefresh和onPullUpToRefresh 編寫要獲取listview新數(shù)據(jù)的方法。

 我這里的例子如下:

 listview.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
   @Override
   public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
      adapter.addToTop();
      new FinishRefresh().execute();
}
   @Override
   public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
      adapter.addToBottom();
      new FinishRefresh().execute();
}
}); 

我這里在自定義的adapter中寫了2個新方法 addToTop 和addToBottom 分別在頭部加入數(shù)據(jù)和在尾部加入數(shù)據(jù)
 方法如下:

private void addToTop() {
  for (int i = 0; i < 2; i++) {
     Item item = new Item();
     item.setText("在頭部加入第" + i + "數(shù)據(jù)");
     item.setImageid(R.mipmap.ic_launcher);
     listItems.add(i, item);
  }
}
private void addToBottom() {
  for (int i = 0; i < 2; i++) {
     Item item = new Item();
     item.setText("在尾部加入第" + i + "數(shù)據(jù)");
     item.setImageid(R.mipmap.ic_launcher);
     listItems.add(item);
   }
} 

這里并沒有考慮去重的問題,就是每次刷新結(jié)束后會顯示出刷新的結(jié)果,當(dāng)再次刷新后,又會執(zhí)行和上次一樣的結(jié)果,實(shí)際上,這是不符合邏輯的,當(dāng)?shù)诙卧谒⑿碌臅r候應(yīng)該進(jìn)行判斷,如果數(shù)據(jù)一樣就不把數(shù)據(jù)加入到list當(dāng)中。

接下來 new FinishRefresh().execute(); 是這里我比較疑惑的一個固定寫法,在這個com.handmark.pulltorefresh.library.PullToRefreshListView 框架下,執(zhí)行onRefreshComplete();方法必須在異步下執(zhí)行,不能和主進(jìn)程一起執(zhí)行,如果直接在下拉,上拉監(jiān)聽方法中寫入onRefreshComplete(); 則在實(shí)際刷新中刷新狀態(tài)的顯示header是不會收回去的,換句話說 刷新一直不會完成。

所以要在繼承AsyncTask的類下調(diào)用onRefreshComplete();

 private class FinishRefresh extends AsyncTask<Void, Void, Void> {
    @Override
    protected Void doInBackground(Void... params) {
      try {
        Thread.sleep(1000);
      } catch (InterruptedException e) {

      }
      return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
      listview.onRefreshComplete();
      adapter.notifyDataSetChanged();
    }
  }

至此,pullToRefreshListview就實(shí)現(xiàn)了簡單的上拉,下拉使用

步驟三 pullToRefresListView 的其他監(jiān)聽方法

關(guān)于步驟三今天時間有限,先給大家分享到這里,后續(xù)持續(xù)更新。

相關(guān)文章

最新評論