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" };
    }
    

      

  • 相关阅读:
    python之enumerate
    Python中的集合set
    SGU 分类
    太空飞行计划 最大权闭合图
    1.飞行员配对 二分图匹配(输出方案)/最大流斩
    poj1149最大流经典构图神题
    hdu1569 方格取数 求最大点权独立集
    最大独立集 最小点覆盖 最小边覆盖 最小路径覆盖 最大团
    hdu3491最小割转最大流+拆点
    hdu3987,最小割时求最少割边数
  • 原文地址:https://www.cnblogs.com/chuiyuan/p/4133831.html
Copyright © 2011-2022 走看看