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

      

  • 相关阅读:
    自学华为IoT物联网_10 IoT联接管理平台配置及开发实验1
    自学华为IoT物联网_09 OceanConnect业务流程
    自学华为IoT物联网_08 IoT连接管理平台介绍
    自学华为IoT物联网_07 物联网安全
    自学华为IoT物联网_06 智慧家庭物联网常见问题及解决方案
    自学华为IoT物联网_05 能源工业物联网常见问题及解决方案
    自学华为IoT物联网_04 车联网常见问题及解决方案
    自学华为IoT物联网_03 公共事业物联网常见问题及解决方案
    自学华为IoT物联网_02 常见物联网通信技术
    OpenDCIM-19.01操作手册
  • 原文地址:https://www.cnblogs.com/chuiyuan/p/4133831.html
Copyright © 2011-2022 走看看