Pulltorefresh实现ListView下拉刷新,上拉加载

来源:互联网 发布:js同步和异步的理解 编辑:程序博客网 时间:2024/05/22 12:44
Pulltorefresh实现ListView下拉刷新,上拉加载
在做android项目的时候,我们经常要用到上拉刷新列表,下拉刷新列表的功能,这里简单介绍一个PullToRefresh的使用。

  一,下载PullToRefresh,地址:https://github.com/chrisbanes/Android-PullToRefresh。这里面,我们只要其中的Library工程。将它引入我们的工程.


需要的话可以下载我的demo:     地址:Listview_pulltorefreshDEMO


效果图:





















布局如下:

<span style="font-size:18px;"><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" >    <com.handmark.pulltorefresh.library.PullToRefreshListView        xmlns:ptr="http://schemas.android.com/apk/res-auto"        android:id="@+id/pull_refresh_list"        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:cacheColorHint="#00000000"        android:divider="#19000000"        android:dividerHeight="4dp"        android:fadingEdge="none"        android:fastScrollEnabled="false"        android:footerDividersEnabled="false"        android:headerDividersEnabled="false"        android:smoothScrollbar="true"        ptr:ptrListViewExtrasEnabled="false"        ptr:ptrAnimationStyle="flip"        ptr:ptrMode="both"        ptr:ptrScrollingWhileRefreshingEnabled="true" >    </com.handmark.pulltorefresh.library.PullToRefreshListView></RelativeLayout></span>

Activity的逻辑如下:

<span style="font-size:18px;">package com.example.zhy_pulltorefreash_chenyoca;import java.util.LinkedList;import android.app.Activity;import android.os.AsyncTask;import android.os.Bundle;import android.util.Log;import android.widget.ArrayAdapter;import android.widget.ListView;import com.handmark.pulltorefresh.library.PullToRefreshBase;import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;import com.handmark.pulltorefresh.library.PullToRefreshListView;public class PullToRefreshListActivity extends Activity{private LinkedList<String> mListItems;/** * 上拉刷新的控件 */private PullToRefreshListView mPullRefreshListView;private ArrayAdapter<String> mAdapter;private int mItemCount = 15;@Overrideprotected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);// 得到控件mPullRefreshListView = (PullToRefreshListView) findViewById(R.id.pull_refresh_list);mPullRefreshListView.setMode(Mode.BOTH);// 初始化数据initDatas();// 设置适配器mAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, mListItems);mPullRefreshListView.setAdapter(mAdapter);mPullRefreshListView.setOnRefreshListener(new OnRefreshListener2<ListView>(){@Overridepublic void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView){Log.e("TAG", "onPullDownToRefresh");//这里写下拉刷新的任务new GetDataTask().execute();}@Overridepublic void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView){Log.e("TAG", "onPullUpToRefresh");//这里写上拉加载更多的任务new GetDataTask().execute();}});}private void initDatas(){// 初始化数据和数据源mListItems = new LinkedList<String>();for (int i = 0; i < mItemCount; i++){mListItems.add("" + i);}}private class GetDataTask extends AsyncTask<Void, Void, String>{@Overrideprotected String doInBackground(Void... params){try{Thread.sleep(2000);} catch (InterruptedException e){}return "" + (mItemCount++);}@Overrideprotected void onPostExecute(String result){mListItems.add(result);mAdapter.notifyDataSetChanged();// Call onRefreshComplete when the list has been refreshed.mPullRefreshListView.onRefreshComplete();}}/** * // 设置监听事件 mPullRefreshListView .setOnRefreshListener(new * OnRefreshListener<ListView>() { *  * @Override public void onRefresh( PullToRefreshBase<ListView> refreshView) *           { 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(); } }); */}</span>













1 0
原创粉丝点击