###android studio 導入 PullToRefreshjava
// 下載 https://github.com/chrisbanes/Android-PullToRefresh // 解壓,獲得 extras,sample,library(主要) Android-PullToRefresh-master -> library // 在你項目的根目錄建立一個lib目錄 [PATH] [app] [src] [res] [build] build.gradle ... [build] [gradle] [lib] // 將 library 重名名爲 pull [pull] [src] [res] [build] ... build.gradle settings.gradle ... // 修改 settings.gradle include ':app', ':lib:pull' // 修改 [app]/build.gradle dependencies { // Library compile project(':lib:pull') } // 建立 [lib]/[pull]/build.gradle apply plugin: 'android-library' android { compileSdkVersion 17 buildToolsVersion "21.1.2" sourceSets { main { manifest.srcFile 'AndroidManifest.xml' java.srcDirs = ['src'] resources.srcDirs = ['src'] aidl.srcDirs = ['aidl'] renderscript.srcDirs = ['src'] res.srcDirs = ['res'] assets.srcDirs = ['assets'] } } } // 完成
###使用 PullToRefresh 刷新 ListViewandroid
// 在 activity_main.xml 增長一個 PullToRefreshListView <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <com.handmark.pulltorefresh.library.PullToRefreshListView android:id="@+id/left_drawer" android:layout_width="240dp" android:layout_height="match_parent" android:layout_gravity="start" android:choiceMode="singleChoice" android:dividerHeight="1px" android:divider="#ff000000" android:background="#ffffffff" android:layout_alignParentTop="true" android:layout_alignParentEnd="true" android:layout_alignParentStart="true" /> </RelativeLayout> // 在 MainActivity public class MainActivity extends Activity { PullToRefreshListView lv; private String[] mListTitle = {"姓名", "性別", "年齡", "居住地", "郵箱"}; private String[] mListStr = {"雨鬆MOMO", "男", "25", "北京", "xuanyusong@gmail.com"}; ListView mListView = null; ArrayList<Map<String, Object>> mData = new ArrayList<Map<String, Object>>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 獲取 PullToRefreshListView View lv = (PullToRefreshListView) findViewById(R.id.left_drawer); // .... int lengh = mListTitle.length; for (int i = 0; i < lengh; i++) { Map<String, Object> item = new HashMap<String, Object>(); item.put("title", mListTitle[i]); item.put("text", mListStr[i]); mData.add(item); } // ... SimpleAdapter adapter = new SimpleAdapter(this, mData, android.R.layout.simple_list_item_2, new String[]{"title", "text"}, new int[]{android.R.id.text1, android.R.id.text2}); // 將 SimpleAdapter 設置到 PullToRefreshListView lv.setAdapter(adapter); // 刷新事件監聽 lv.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() { @Override public void onRefresh(PullToRefreshBase<ListView> refreshView) { // ... new AsyncTask<Void, Void, Void>() { @Override protected Void doInBackground(Void... params) { // 處理刷新任務 try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(Void reslst) { // 更行內容,通知 PullToRefresh 刷新結束 lv.onRefreshComplete(); } }.execute(); } }); } }
上面轉載自點擊查看 若是須要同時實現下拉刷新和上拉加載,實現OnRefreshListener2,重寫onPullDownToRefresh和onPullUpToRefresh方法git
//mListView是PullToRefreshListView實例 mListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() { public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView){ new GetDataTask().execute(LOAD_REFRESH); } public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView){ new GetDataTask().execute(LOAD_MORE); } } ); //設置PullRefreshListView上提加載時的加載提示 mListView.getLoadingLayoutProxy(false, true).setPullLabel("上拉加載..."); mListView.getLoadingLayoutProxy(false, true).setRefreshingLabel("正在加載..."); mListView.getLoadingLayoutProxy(false, true).setReleaseLabel("鬆開加載更多..."); // 設置PullRefreshListView下拉加載時的加載提示 mListView.getLoadingLayoutProxy(true, false).setPullLabel("下拉刷新..."); mListView.getLoadingLayoutProxy(true, false).setRefreshingLabel("正在刷新..."); mListView.getLoadingLayoutProxy(true, false).setReleaseLabel("鬆開刷新...");