android 下拉重新整理,android下拉
1、github
原作者: https://github.com/chrisbanes/Android-PullToRefresh
我自己的: https://github.com/zyj1609wz/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) { } }
設定重新整理時顯示的字型的顏色
<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 : 不輸出日誌