android Android-PullToRefresh 下拉刷新

1.github下載地址 https://github.com/chrisbanes/Android-PullToRefresh

2、使用方法

     listview  佈局文件

複製代碼
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.aa.MainActivity"
    tools:ignore="MergeRootFrame" >

    <com.handmark.pulltorefresh.library.PullToRefreshListView
        xmlns:ptr="http://schemas.android.com/apk/res-auto"
        android:id="@+id/listview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center"
        android:listSelector="#00000000"
        ptr:ptrMode="both" />

</LinearLayout>
複製代碼


 常用的方法

複製代碼
package com.example.aa;
import java.util.ArrayList;
import java.util.List;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnLastItemVisibleListener;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import android.support.v7.app.ActionBarActivity;
import android.text.format.DateUtils;
import android.widget.ListView;
import android.widget.Toast;
import android.os.AsyncTask;
import android.os.Bundle;

public class MainActivity extends ActionBarActivity {
    PullToRefreshListView pullToRefreshListView  ;
    ListView listView  ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        pullToRefreshListView = (PullToRefreshListView) findViewById( R.id.listview ) ;

        pullToRefreshListView.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); 

                //設置刷新圖標 下拉的時候顯示的內容
                refreshView.getLoadingLayoutProxy().setLoadingDrawable(getResources().getDrawable( R.drawable.ic_launcher) );

                //下拉完成後,還沒有刷新時 顯示的內容
                refreshView.getLoadingLayoutProxy().setReleaseLabel( "默默地麼麼噠!!" );

                //鬆開手,正在刷新時 ,顯示的內容
                refreshView.getLoadingLayoutProxy().setRefreshingLabel(  "啦啦啦啦啦" );

                Toast.makeText( MainActivity.this , "刷新了",  Toast.LENGTH_SHORT ).show(); 
                new GetDataTask().execute(  ) ;
            }
        });

        //listview 滑到 最後一項
        pullToRefreshListView.setOnLastItemVisibleListener( new OnLastItemVisibleListener() {
            @Override
            public void onLastItemVisible() {
                Toast.makeText( MainActivity.this , "listview到底了",  Toast.LENGTH_SHORT ).show() ; 
            }
        });

        pullToRefreshListView.setMode( Mode.PULL_FROM_START );

        listView = pullToRefreshListView.getRefreshableView() ;

        listView.setAdapter( new Adapter( this , getData() ));

        /**
         * 程序進來就執行刷新數據,自動執行刷新
         */
        pullToRefreshListView.setRefreshing(); 

    }

    /**
     * @author admin
     *  pullToRefreshListView.onRefreshComplete();  這一句最好放在異步裏面寫 
     *  
     */
    private class GetDataTask extends AsyncTask<Void, Void, String> { 
        @Override 
        protected String doInBackground(Void... params) { 
            try { 
                Thread.sleep( 500 ); 
            } catch (InterruptedException e) { 
            } 
            return "" ; 
        } 

        @Override 
        protected void onPostExecute(String result) { 
            super.onPostExecute(result); 
            pullToRefreshListView.onRefreshComplete(); 
        } 
    } 

    List<String> getData(){
        List<String> list = new ArrayList<String>() ;
        for( int i = 0 ; i < 100 ; i ++) {
            list.add( "ddd") ;
        }
        return list ;
    }

}
複製代碼

 

   刷新模式

複製代碼
    //向下拉
    pullToRefreshListView.setMode( Mode.PULL_FROM_START );

    //向上拉
    pullToRefreshListView.setMode( Mode.PULL_FROM_END );

    //同時使用 下拉  和 上拉
    pullToRefreshListView.setMode( Mode.BOTH );

    /不啓用刷新功能
    pullToRefreshListView.setMode( Mode.DISABLED );
複製代碼

  

    獲取當前的刷新模式

//獲取當前的刷新模式
if( pullToRefreshListView.getMode() == Mode.BOTH ){
    Toast.makeText( MainActivity.this , "當前的刷新模式是 " + pullToRefreshListView.getMode() ,
                    Toast.LENGTH_SHORT ).show(); 
  }

 

   設置刷新時的聲音

複製代碼
         /**
         * Add Sound Event Listener
         */
        SoundPullEventListener<ListView> soundListener = new SoundPullEventListener<ListView>(this);    
        soundListener.addSoundEvent(State.PULL_TO_REFRESH, R.raw.a1 );   //開始刷新顯示的聲音    
        soundListener.addSoundEvent(State.RESET, R.raw.a2 );             //刷新完成後,顯示的聲音 
        soundListener.addSoundEvent(State.REFRESHING, R.raw.a3 );        //正在刷新顯示的聲音
        pullToRefreshListView.setOnPullEventListener(soundListener);
複製代碼

 

 

 設置 下拉刷新 和 上拉加載 更多 的監聽方法

複製代碼
    pullToRefreshListView.setOnRefreshListener( new Refresh() ) ;

     /**
     * 監聽方法
     * @author admin
     */
    class Refresh implements OnRefreshListener2<ListView> {
        //下拉
        @Override
        public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {

        }

        //上拉
        @Override
        public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {

        }
    }
複製代碼

    

      設置正在刷新時,listview是否可以滾動

   //正在刷新的時候,listView 禁止滾動
   pullToRefreshListView.setScrollingWhileRefreshingEnabled( false );
        
   //正在刷新的時候,listView 可以滾動
    pullToRefreshListView.setScrollingWhileRefreshingEnabled( true );

 

   設置刷新時顯示的字體的顏色

複製代碼
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.aa.MainActivity"
    tools:ignore="MergeRootFrame" >

    <com.handmark.pulltorefresh.library.PullToRefreshListView
        xmlns:ptr="http://schemas.android.com/apk/res-auto"
        android:id="@+id/listview"
        android:background="#ffffff"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center"
        android:listSelector="#00000000"
        ptr:ptrHeaderTextColor="#FF9900"
        ptr:ptrHeaderSubTextColor="#330099"
        ptr:ptrMode="both" />

    <!-- ptrHeaderTextColor   刷新提示顯示的顏色 -->
    <!-- ptrHeaderSubTextColor 刷新提示子選項顏色值 -->

</LinearLayout>
複製代碼

   運行結果

  

 

分別設置 下拉 和 上拉 顯示的字體

複製代碼
        //得到下拉時候顯示的ILoadingLayout
        ILoadingLayout startLayout = pullToRefreshListView.getLoadingLayoutProxy( true , false ) ; 
        startLayout.setPullLabel("你可勁拉,拉...下拉");// 剛下拉時,顯示的提示 
        startLayout.setRefreshingLabel("好嘞,正在刷新...下拉");// 刷新時 
        startLayout.setReleaseLabel("你敢放,我就敢刷新...下拉");// 下來達到一定距離時,顯示的提示 

        //得到上拉時候顯示的ILoadingLayout
        ILoadingLayout endLayout = pullToRefreshListView.getLoadingLayoutProxy( false , true  ) ;
        endLayout.setPullLabel("你可勁拉,拉... 上拉");// 剛下拉時,顯示的提示 
        endLayout.setRefreshingLabel("好嘞,正在刷新...上拉");// 刷新時 
        endLayout.setReleaseLabel("你敢放,我就敢刷新...上拉");// 下來達到一定距離時,顯示的提示 
複製代碼

 

     常用的 xml 配置

複製代碼
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.aa.MainActivity"
    tools:ignore="MergeRootFrame" >

    <com.handmark.pulltorefresh.library.PullToRefreshListView
        xmlns:ptr="http://schemas.android.com/apk/res-auto"
        android:id="@+id/listview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#ffffff"
        android:gravity="center"
        android:listSelector="#00000000"
        ptr:ptrHeaderBackground="@drawable/background"
        ptr:ptrHeaderSubTextColor="#330099"
        ptr:ptrHeaderTextColor="#B26B00"
        ptr:ptrListViewExtrasEnabled="false"
        ptr:ptrMode="both"
        ptr:ptrRefreshableViewBackground="@drawable/b2"
        ptr:ptrRotateDrawableWhilePulling="false"
        ptr:ptrScrollingWhileRefreshingEnabled="true"
        ptr:ptrShowIndicator="true" />

    <!-- ptrHeaderTextColor   刷新提示顯示的顏色 -->
    <!-- ptrHeaderSubTextColor 刷新提示子選項顏色值 -->
    <!-- ptr:ptrHeaderBackground 上拉背景圖 -->
    <!-- ptrShowIndicator    右上角顯示的小圖標 -->
    <!-- ptrRefreshableViewBackground  整個listview的背景 -->
    <!-- ptrScrollingWhileRefreshingEnabled  刷新的時候,是否允許ListView或GridView滾動。覺得爲true比較好。 -->
    <!-- ptrListViewExtrasEnabled  Footer以何種方式加入mPullRefreshListView,true爲headView方式加入,就是滾動時刷新頭部會一起滾動。 -->
    <!-- ptrRotateDrawableWhilePulling  當動畫設置爲rotate時,下拉是是否旋轉。 -->

    <!-- ptr:ptrAnimationStyle  的取值:flip(翻轉動畫), rotate(旋轉動畫) 。 -->
    <!-- ptr:ptrDrawable  則就是設置圖標了。 -->

</LinearLayout>
複製代碼

 

 運行結果

 

 3、不太常用的東西

   1、如何 關閉 log 日誌輸出 ?

     PullToRefresh 默認是開啓日誌輸出的 。 在  PullToRefreshBase 裏面可以看到 static final boolean DEBUG = true ; 

      true : 輸出日誌 。   false : 不輸出日誌

原文出處
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章