主要也是学习第三方的开源组件
https://github.com/chrisbanes/Android-PullToRefresh
public class MainActivity extends Activity { private PullToRefreshListView mPullRefreshListView; private LinkedList mListItems; private ArrayAdapter<String> mAdapter; 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_main); mPullRefreshListView = (PullToRefreshListView) findViewById(R.id.pull_refresh_list); // 下拉的时候调用此方法 mPullRefreshListView.setOnRefreshListener(new OnRefreshListener<ListView>() { @Override public void onRefresh(PullToRefreshBase<ListView> refreshView) { // TODO Auto-generated method stub //获取当前时间 String label = DateUtils.formatDateTime(getApplicationContext(), System.currentTimeMillis(), DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL); //设置下拉时候的刷新时间 refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label); //刷新的时候执行的异步任务 主要用于获取数据 或者加载更多内容 new GetDataTask().execute(); } }); //上拉动的时候调用此方法 mPullRefreshListView.setOnLastItemVisibleListener(new OnLastItemVisibleListener() { @Override public void onLastItemVisible() { // TODO Auto-generated method stub Toast.makeText(getApplicationContext(), "已经到底啦~~~", 0).show(); } }); //从mPullRefreshListView 中 获取listView 对象 ListView actualListView = mPullRefreshListView.getRefreshableView(); //设置组装listView中的数据 mListItems = new LinkedList<String>(); mListItems.addAll(Arrays.asList(mStrings)); //设置adapter mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mListItems); //已下2中方式设置adapter都可以 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); } } }
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <com.handmark.pulltorefresh.library.PullToRefreshListView android:id="@+id/pull_refresh_list" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RelativeLayout>
这个demo 主要就用到了libary里面的 记得新建的项目要引用上面的libary
说不定以后会用得到。先留着。
效果图