標籤:des android style blog http color io os ar
開源地址: https://github.com/chrisbanes/Android-PullToRefresh
簡介:PullToRefresh是一款支援ListView,GridView,ViewPager,ScrollView,WebView等一切可以拖動,並實現上下左右拖動重新整理資料的架構,廢話不多說,上代碼;
[基本的android依賴項目存放在library中,支援fragment列表,ViewPager列表的存放在extras包中]
[依賴項目匯入如左圖]
1.支援ListView的實現:
1)XML實現
<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 android:id="@+id/my_ptr_lv" android:layout_width="match_parent" android:layout_height="match_parent" android:cacheColorHint="#0000" android:divider="#0FF" android:dividerHeight="1dp" android:smoothScrollbar="true" android:fastScrollEnabled="false" android:footerDividersEnabled="false" android:headerDividersEnabled="false" /></RelativeLayout>
2)JAVA代碼
public class PullListSampleActivity extends Activity {private PullToRefreshListView mPullToRefreshLv;private ArrayAdapter<String> mAdapter;private LinkedList<String> mListItems;private String[] mStrings = { "Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam", "Abondance", "Ackawi","Acorn", "Adelost", "Affidelice au Chablis", "Afuega'l Pitu", "Airag", "Airedale", "Aisy Cendre","Allgauer Emmentaler", "Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam", "Abondance", "Ackawi","Acorn", "Adelost", "Affidelice au Chablis", "Afuega'l Pitu", "Airag", "Airedale", "Aisy Cendre","Allgauer Emmentaler" };@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_pull_list_sample);//填充資料mPullToRefreshLv=(PullToRefreshListView) findViewById(R.id.my_ptr_lv);mListItems=new LinkedList<String>(Arrays.asList(mStrings));mAdapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,android.R.id.text1,mListItems);mPullToRefreshLv.setAdapter(mAdapter);//下拉重新整理的聲音支援SoundPullEventListener<ListView> soundPullEventListener=new SoundPullEventListener<ListView>(this);soundPullEventListener.addSoundEvent(State.PULL_TO_REFRESH, R.raw.pull_event);soundPullEventListener.addSoundEvent(State.REFRESHING, R.raw.refreshing_sound);soundPullEventListener.addSoundEvent(State.RESET, R.raw.reset_sound);mPullToRefreshLv.setOnPullEventListener(soundPullEventListener);//重新整理時調用的監聽器mPullToRefreshLv.setOnRefreshListener(new OnRefreshListener<ListView>() {@Overridepublic void onRefresh(PullToRefreshBase<ListView> refreshView) {String label=DateUtils.formatDateTime(PullListSampleActivity.this, System.currentTimeMillis(),DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);//設定頭部LabelmPullToRefreshLv.getLoadingLayoutProxy().setLastUpdatedLabel(label);new GetListDataTask().execute();}});mPullToRefreshLv.setOnLastItemVisibleListener(new OnLastItemVisibleListener() {@Overridepublic void onLastItemVisible() {Toast.makeText(PullListSampleActivity.this,"all data loaded !", 800).show();}});}class GetListDataTask extends AsyncTask<Void, Void, String[]>{@Overrideprotected String[] doInBackground(Void... params) {//mock get datastry {Thread.sleep(3000);} catch (InterruptedException e) {e.printStackTrace();}return mStrings;}@Overrideprotected void onPostExecute(String[] result) {mListItems.addAll(Arrays.asList(result));mAdapter.notifyDataSetChanged();//手動關閉頭部mPullToRefreshLv.onRefreshComplete();super.onPostExecute(result);}}}
2.支援GridView
1)XML實現
ptr:ptrMode="both"表示上下/左右都可以重新整理
ptr:ptrDrawable表示重新整理時顯示的表徵圖
<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.PullToRefreshGridView xmlns:ptr="http://schemas.android.com/apk/res-auto" android:id="@+id/my_ptr_gv" android:layout_width="match_parent" android:layout_height="match_parent" android:cacheColorHint="#0000" android:divider="#0FF" android:dividerHeight="1dp" android:smoothScrollbar="true" android:fastScrollEnabled="false" android:footerDividersEnabled="false" android:headerDividersEnabled="false" android:numColumns="auto_fit" ptr:ptrMode="both" ptr:ptrDrawable="@drawable/default_ptr_rotate" /></RelativeLayout>
2)JAVA代碼實現
public class PullGridSampleActivity extends Activity {private PullToRefreshGridView mPullToRefreshGridView;private ArrayAdapter<String> mAdapter;private LinkedList<String> mListItems;private String[] mStrings = { "Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam", "Abondance", "Ackawi","Acorn", "Adelost", "Affidelice au Chablis", "Afuega'l Pitu", "Airag", "Airedale", "Aisy Cendre","Allgauer Emmentaler" };@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_pull_grid_sample);mPullToRefreshGridView=(PullToRefreshGridView) findViewById(R.id.my_ptr_gv);mListItems=new LinkedList<String>();mAdapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,android.R.id.text1,mListItems);mPullToRefreshGridView.setAdapter(mAdapter);//支援當沒有資料的時候替代的View 具體代碼根據自己的業務TextView textView=new TextView(this);LayoutParams params=new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);params.gravity=Gravity.CENTER;textView.setLayoutParams(params);textView.setText("The data is empty !");mPullToRefreshGridView.setEmptyView(textView);SoundPullEventListener<GridView> pullEventListener=new SoundPullEventListener<GridView>(this);pullEventListener.addSoundEvent(State.PULL_TO_REFRESH,R.raw.pull_event);pullEventListener.addSoundEvent(State.RESET,R.raw.reset_sound);pullEventListener.addSoundEvent(State.REFRESHING,R.raw.refreshing_sound);mPullToRefreshGridView.setOnPullEventListener(pullEventListener);mPullToRefreshGridView.setOnRefreshListener(new OnRefreshListener2<GridView>() {@Overridepublic void onPullDownToRefresh(PullToRefreshBase<GridView> refreshView) {String label=DateUtils.formatDateTime(getApplicationContext(),System.currentTimeMillis(),DateUtils.FORMAT_24HOUR);refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);new GetDataTask().execute();}@Overridepublic void onPullUpToRefresh(PullToRefreshBase<GridView> refreshView) {String label=DateUtils.formatDateTime(getApplicationContext(),System.currentTimeMillis(),DateUtils.FORMAT_24HOUR);refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);new GetDataTask().execute();}});}class GetDataTask extends AsyncTask<Void, Void, String[]>{@Overrideprotected String[] doInBackground(Void... params) {try {Thread.sleep(2000);} catch (InterruptedException e) {e.printStackTrace();}return mStrings;}@Overrideprotected void onPostExecute(String[] result) {mListItems.addAll(Arrays.asList(result));mAdapter.notifyDataSetChanged();mPullToRefreshGridView.onRefreshComplete();}}}
3.對ViewPager的實現
1)XML
<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.extras.viewpager.PullToRefreshViewPager xmlns:ptr="http://schemas.android.com/apk/res-auto" android:id="@+id/my_ptr_viewpager" android:layout_width="match_parent" android:layout_height="match_parent" ptr:ptrAnimationStyle="flip" ptr:ptrMode="both" ptr:ptrHeaderBackground="#FFFF" /></RelativeLayout>
2)Java實現
public class PullViewPagerSample extends Activity implements OnRefreshListener<ViewPager>{private PullToRefreshViewPager mPullToRefreshViewPager;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_pull_viewpager_sample);mPullToRefreshViewPager=(PullToRefreshViewPager) findViewById(R.id.my_ptr_viewpager);mPullToRefreshViewPager.getRefreshableView().setAdapter(new MyPagerAdapter());mPullToRefreshViewPager.setOnRefreshListener(this);}class MyPagerAdapter extends PagerAdapter{private final int[] sDrawables = { R.drawable.wallpaper, R.drawable.wallpaper, R.drawable.wallpaper,R.drawable.wallpaper, R.drawable.wallpaper, R.drawable.wallpaper };@Overridepublic int getCount() {return sDrawables.length;}@Overridepublic Object instantiateItem(ViewGroup container, int position) {ImageView imageView=new ImageView(container.getContext());imageView.setImageResource(sDrawables[position]);container.addView(imageView,LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT);return imageView;}@Overridepublic boolean isViewFromObject(View arg0, Object arg1) {return arg0==(View)arg1;}@Overridepublic void destroyItem(ViewGroup container, int position, Object object) {container.removeView((View) object);}}@Overridepublic void onRefresh(PullToRefreshBase<ViewPager> refreshView) {try {Thread.sleep(4000);//類比載入時間} catch (InterruptedException e) {e.printStackTrace();}mPullToRefreshViewPager.onRefreshComplete();}}
Android開源--PullToRefresh