zoukankan      html  css  js  c++  java
  • 最简单的pulltorefresh例子

    public final class PullToRefreshListActivity extends ListActivity {
    
    	private LinkedList<String> mListItems;
    	private PullToRefreshListView mPullRefreshListView;
    	private ArrayAdapter<String> mAdapter;
    
    	/** Called when the activity is first created. */
    	@Override
    	public void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_ptr_list);
    
    		mPullRefreshListView = (PullToRefreshListView) findViewById(R.id.pull_refresh_list);
    		mPullRefreshListView.setMode(Mode.BOTH);
    
    		mPullRefreshListView.setOnRefreshListener(new OnRefreshListener2<ListView>() {
    
    			@Override
    			public void onPullDownToRefresh(
    					PullToRefreshBase<ListView> refreshView) {
    				String label = DateUtils.formatDateTime(getApplicationContext(), System.currentTimeMillis(),
    						DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);
    
    				// Update the LastUpdatedLabel
    				refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);
    
    				// Do work to refresh the list here.
    				new GetDataTask().execute();
    				
    			}
    
    			@Override
    			public void onPullUpToRefresh(
    					PullToRefreshBase<ListView> refreshView) {
    				String label = DateUtils.formatDateTime(getApplicationContext(), System.currentTimeMillis(),
    						DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);
    
    				// Update the LastUpdatedLabel
    				refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);
    
    				// Do work to refresh the list here.
    				new GetDataTask().execute();
    				
    			}
    			
    		});
    
    		// Add an end-of-list listener
    		mPullRefreshListView.setOnLastItemVisibleListener(new OnLastItemVisibleListener() {
    
    			@Override
    			public void onLastItemVisible() {
    				Toast.makeText(PullToRefreshListActivity.this, "End of List!", Toast.LENGTH_SHORT).show();
    			}
    		});
    		//Anything returned here has already been added to the content view
    		ListView actualListView = mPullRefreshListView.getRefreshableView();
    
    		// Need to use the Actual ListView when registering for Context Menu
    		//可以用作举报,要进行注册
    		registerForContextMenu(actualListView);
    
    		mListItems = new LinkedList<String>();
    		mListItems.addAll(Arrays.asList(mStrings));
    
    		mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mListItems);
    
    		//mPullRefreshListView.setOnPullEventListener(null);
    
    		// You can also just use setListAdapter(mAdapter) or
    		// mPullRefreshListView.setAdapter(mAdapter)
    		actualListView.setAdapter(mAdapter);
    	}
    
    	private class GetDataTask extends AsyncTask<Void, Void, String[]> {
    
    		@Override
    		protected String[] doInBackground(Void... params) {
    			// Simulates a background job.,可以查看定位 ?
    			try {
    				Thread.sleep(4000);
    			} catch (InterruptedException e) {
    			}
    			return mStrings;
    		}
    
    		@Override
    		protected void onPostExecute(String[] result) {
    			mListItems.addFirst("Added after refresh...");
    			mAdapter.notifyDataSetChanged();
    
    			// Call onRefreshComplete when the list has been refreshed.
    			mPullRefreshListView.onRefreshComplete();
    
    			super.onPostExecute(result);
    		}
    	}
    
    	//可以用户设置长按的,用来举报什么的
    //	@Override
    //	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    //		AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
    //	
    //		menu.setHeaderTitle("Item: " + getListView().getItemAtPosition(info.position));
    //		menu.add("Item 1");
    //		menu.add("Item 2");
    //		menu.add("Item 3");
    //		menu.add("Item 4");
    //
    //		super.onCreateContextMenu(menu, v, menuInfo);
    //	}
    
    
    	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" };
    }
    

      

  • 相关阅读:
    设计模式学习——代理模式(Proxy Pattern)之 强制代理(强校验,防绕过)
    设计模式学习——代理模式(Proxy Pattern)
    设计模式学习——抽象工厂模式(Abstract Factory Pattern)
    最长字符串系列汇总
    窗口的最大值与最小值更新结构(滑动窗口)
    归并排序和归并排序应用(逆序对+小和)
    位运算在编程题的一些作用
    链表的排序(归并排序+快慢指针)
    Manacher算法解决最长回文子串长度问题
    回文数字的验证
  • 原文地址:https://www.cnblogs.com/chuiyuan/p/4133831.html
Copyright © 2011-2022 走看看