android 下拉刷新/上拉加载更多【pull-to-refresh】
然后,在刷新事件中的onRefresh中来判断用户的操作是上拉还是下拉。 如下代码 :public boolean isHeaderShown() { return getHeaderLayout().isShown(); } public boolean isFooterShown() { return getFooterLayout().isShown(); }
if (refreshView.isHeaderShown()) {
Log.i("", "下拉刷新... ");
} else { Log.i("", "上拉加载更多... "); }
// 下拉刷新时的提示文本设置 mPullRefreshListView.getLoadingLayoutProxy(true, false).setLastUpdatedLabel("下拉刷新"); mPullRefreshListView.getLoadingLayoutProxy(true, false).setPullLabel(""); mPullRefreshListView.getLoadingLayoutProxy(true, false).setRefreshingLabel("正在刷新"); mPullRefreshListView.getLoadingLayoutProxy(true, false).setReleaseLabel("放开以刷新"); // 上拉加载更多时的提示文本设置 mPullRefreshListView.getLoadingLayoutProxy(false, true).setLastUpdatedLabel("上拉加载"); mPullRefreshListView.getLoadingLayoutProxy(false, true).setPullLabel(""); mPullRefreshListView.getLoadingLayoutProxy(false, true).setRefreshingLabel("正在加载..."); mPullRefreshListView.getLoadingLayoutProxy(false, true).setReleaseLabel("放开以加载");
XML布局文件:public class MainActivity extends Activity { private LinkedList<String> mListItems; private PullToRefreshListView mPullRefreshListView; private ArrayAdapter<String> mAdapter; private String[] mStrings = { "1", "2", "3", "4" }; private ListView lvShow = null; private int y = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mPullRefreshListView = (PullToRefreshListView) findViewById(R.id.pull_refresh_list); mPullRefreshListView.setMode(Mode.BOTH); // 下拉刷新时的提示文本设置 mPullRefreshListView.getLoadingLayoutProxy(true, false).setLastUpdatedLabel("下拉刷新"); mPullRefreshListView.getLoadingLayoutProxy(true, false).setPullLabel(""); mPullRefreshListView.getLoadingLayoutProxy(true, false).setRefreshingLabel("正在刷新"); mPullRefreshListView.getLoadingLayoutProxy(true, false).setReleaseLabel("放开以刷新"); // 上拉加载更多时的提示文本设置 mPullRefreshListView.getLoadingLayoutProxy(false, true).setLastUpdatedLabel("上拉加载"); mPullRefreshListView.getLoadingLayoutProxy(false, true).setPullLabel(""); mPullRefreshListView.getLoadingLayoutProxy(false, true).setRefreshingLabel("正在加载..."); mPullRefreshListView.getLoadingLayoutProxy(false, true).setReleaseLabel("放开以加载"); // 添加 一个下拉刷新事件 mPullRefreshListView .setOnRefreshListener(new OnRefreshListener<ListView>() { @Override public void onRefresh( PullToRefreshBase<ListView> refreshView) { if (refreshView.isHeaderShown()) { new GetHeaderDataTask().execute(); } else { // 得到上一次滚动条的位置,让加载后的页面停在上一次的位置,便于用户操作 y = mListItems.size(); new GetBottomDataTask().execute(); } } }); lvShow = mPullRefreshListView.getRefreshableView(); mListItems = new LinkedList<String>(); mListItems.addAll(Arrays.asList(mStrings)); mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mListItems); lvShow.setAdapter(mAdapter); } /* * 功能 : 下拉刷新时的事件处理 */ private class GetHeaderDataTask 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("-3"); mListItems.addFirst("-4"); mListItems.addFirst("-5"); mAdapter.notifyDataSetChanged(); // 停止刷新 mPullRefreshListView.onRefreshComplete(); super.onPostExecute(result); } } /* * 功能: 上拉加载更多时的事件处理 */ private class GetBottomDataTask 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.add("5"); mListItems.add("5"); mListItems.add("5"); mListItems.add("5"); mListItems.add("5"); mListItems.add("5"); mListItems.add("5"); mListItems.add("5"); mListItems.add("5"); mListItems.add("5"); mListItems.add("5"); mListItems.add("8"); mAdapter.notifyDataSetChanged(); // 停止刷新 mPullRefreshListView.onRefreshComplete(); // 设置滚动条的位置 -- 加载更多之后,滚动条的位置应该在上一次划到的位置 lvShow.setSelectionFromTop(y, TRIM_MEMORY_BACKGROUNP); 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="fill_parent" android:layout_height="fill_parent" /> </RelativeLayout>
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。