Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android開源--PullToRefresh

Android開源--PullToRefresh

編輯:關於Android編程

 

簡介:PullToRefresh是一款支持ListView,GridView,ViewPager,ScrollView,WebView等一切可以拖動,並實現上下左右拖動刷新數據的框架,廢話不多說,上代碼;

\

[基本的android依賴項目存放在library中,支持fragment列表,ViewPager列表的存放在extras包中]

 

\

[依賴項目導入如左圖]

 

1.支持ListView的實現:

1)XML實現

 

 

 

2)JAVA代碼

 

public class PullListSampleActivity extends Activity {
	
	private PullToRefreshListView mPullToRefreshLv;
	private ArrayAdapter mAdapter;
	private LinkedList 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 };
	
	@Override
	protected 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(Arrays.asList(mStrings));
		mAdapter=new ArrayAdapter(this,android.R.layout.simple_list_item_1,android.R.id.text1,mListItems);
		mPullToRefreshLv.setAdapter(mAdapter);
		//下拉刷新的聲音支持
		SoundPullEventListener soundPullEventListener=new SoundPullEventListener(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() {

			@Override
			public void onRefresh(PullToRefreshBase refreshView) {
				
				String label=DateUtils.formatDateTime(PullListSampleActivity.this, System.currentTimeMillis(),
						DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);
				//設置頭部Label
				mPullToRefreshLv.getLoadingLayoutProxy().setLastUpdatedLabel(label);
				new GetListDataTask().execute();
			}
			
		});
		
		mPullToRefreshLv.setOnLastItemVisibleListener(new OnLastItemVisibleListener() {

			@Override
			public void onLastItemVisible() {
				Toast.makeText(PullListSampleActivity.this,all data loaded !, 800).show();
				
			}
			
		});
	}
	
	class GetListDataTask extends AsyncTask{
		
		@Override
		protected String[] doInBackground(Void... params) {
			//mock get datas
			try {
				Thread.sleep(3000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			return mStrings;
		}
		
		@Override
		protected 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表示刷新時顯示的圖標

 



    


2)JAVA代碼實現

 

 

public class PullGridSampleActivity extends Activity {
	
	private PullToRefreshGridView mPullToRefreshGridView;
	private ArrayAdapter mAdapter;
	private LinkedList 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 };
	
	@Override
	protected 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();
		mAdapter=new ArrayAdapter(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 pullEventListener=new SoundPullEventListener(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() {

			@Override
			public void onPullDownToRefresh(
					PullToRefreshBase refreshView) {
				String label=DateUtils.formatDateTime(getApplicationContext(),
						System.currentTimeMillis(),DateUtils.FORMAT_24HOUR);
				refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);
				new GetDataTask().execute();
			}

			@Override
			public void onPullUpToRefresh(
					PullToRefreshBase refreshView) {
				String label=DateUtils.formatDateTime(getApplicationContext(),
						System.currentTimeMillis(),DateUtils.FORMAT_24HOUR);
				refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);
				new GetDataTask().execute();
			}

		});
		
	}
	
	class GetDataTask extends AsyncTask{

		@Override
		protected String[] doInBackground(Void... params) {
			try {
				Thread.sleep(2000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			return mStrings;
		}
		
		@Override
		protected void onPostExecute(String[] result) {
			mListItems.addAll(Arrays.asList(result));
			mAdapter.notifyDataSetChanged();
			
			mPullToRefreshGridView.onRefreshComplete();
			
		}
		
	}
	
}

3.對ViewPager的實現

1)XML

 



    


2)Java實現

 

 

public class PullViewPagerSample extends Activity implements OnRefreshListener{
	
	private PullToRefreshViewPager mPullToRefreshViewPager;
	
	@Override
	protected 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 };

		
		@Override
		public int getCount() {
			return sDrawables.length;
		}
		
		@Override
		public 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;
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0==(View)arg1;
		}
		
		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			container.removeView((View) object);
		}
		
	}

	@Override
	public void onRefresh(PullToRefreshBase refreshView) {
		try {
			Thread.sleep(4000);//模擬加載時間
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		mPullToRefreshViewPager.onRefreshComplete();
	}
	
}


 


 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved