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

      

  • 相关阅读:
    Android_NDK问题:APP_BUILD_SCRIPT points to an unknown file: <project_path>/jni/Android.mk
    Android开发问题集锦-Button初始为disable状态时自定义的selector不生效问题
    一步步学习Python-django开发-添加后台管理
    一步步学习Python-django开发-建立django数据库
    一步步学习Python-django开发-Mac下搭建Python-Django环境
    JAVA小知识点-Finally和Return的执行关系
    Android自定义组合控件内子控件无法显示问题
    《将博客搬至CSDN》
    idea自动生成try/catch代码块的快捷键
    转:Apache common包 CollectionUtils 使用详解
  • 原文地址:https://www.cnblogs.com/chuiyuan/p/4133831.html
Copyright © 2011-2022 走看看