Android 學習之 開源項目PullToRefresh的使用

來源:互聯網
上載者:User

標籤:private   except   activity   ast   ide   java   fast   utf-8   oncreate   








首先 下載 Android-PullToRefresh-master


 https://github.com/chrisbanes/Android-PullToRefresh




下載之後將其解壓



如今  我們用eclipse 建立一個項目取名PullToRefresh


將上面的library 引入我們的項目

引入成功之後開啟項目的project.properties檔案我們能夠看到

android.library.reference.1=../Android-PullToRefresh-master/library

這樣就表示能夠引用成功了



我們在res/layout建立 布局檔案main.xml



<?

xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#FFFFFF" android:orientation="vertical" > <!-- xmlns:ptr = "http://schemas.android.com/apk/res-auto" 為我們要使用PullToRefresh 裡面一些屬性須要引的命名空間 --> <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:dividerHeight="4dp" android:fadingEdge="none" android:fastScrollEnabled="false" android:footerDividersEnabled="false" android:headerDividersEnabled="false" android:smoothScrollbar="true"ptr:ptrMode="both" /></LinearLayout>




接著建立 MainActivity.java


package com.pulltorefresh;import java.util.Arrays;import java.util.LinkedList;import android.app.Activity;import android.os.AsyncTask;import android.os.Bundle;import android.widget.ArrayAdapter;import android.widget.ListView;import android.widget.Toast;import com.handmark.pulltorefresh.library.PullToRefreshBase;import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;import com.handmark.pulltorefresh.library.PullToRefreshBase.State;import com.handmark.pulltorefresh.library.PullToRefreshListView;import com.handmark.pulltorefresh.library.extras.SoundPullEventListener;public class MainActivity extends Activity {static final int MENU_MANUAL_REFRESH = 0;static final int MENU_DISABLE_SCROLL = 1;static final int MENU_SET_MODE = 2;static final int MENU_DEMO = 3;private LinkedList<String> mListItems;private PullToRefreshListView mPullRefreshListView;private ArrayAdapter<String> mAdapter;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);mPullRefreshListView = (PullToRefreshListView) findViewById(R.id.pull_refresh_list);/** * 實現 介面  OnRefreshListener2<ListView>  以便與監聽  滾動欄到頂部和究竟部 */mPullRefreshListView.setOnRefreshListener(new OnRefreshListener2<ListView>() {@Overridepublic void onPullDownToRefresh( PullToRefreshBase<ListView> refreshView) {Toast.makeText(MainActivity.this, "onPullDownToRefresh", Toast.LENGTH_SHORT).show();new GetDataTask().execute();}@Overridepublic void onPullUpToRefresh( PullToRefreshBase<ListView> refreshView) {Toast.makeText(MainActivity.this, "onPullUpToRefresh", Toast.LENGTH_SHORT).show();new GetDataTask().execute();}});ListView actualListView = mPullRefreshListView.getRefreshableView();// Need to use the Actual ListView when registering for Context MenuregisterForContextMenu(actualListView);mListItems = new LinkedList<String>();mListItems.addAll(Arrays.asList(mStrings));mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mListItems);/** * Add Sound Event Listener *//** *   設定下拉重新整理和上拉載入時的 鈴聲(可有可無) */SoundPullEventListener<ListView> soundListener = new SoundPullEventListener<ListView>(this);soundListener.addSoundEvent(State.PULL_TO_REFRESH, R.raw.pull_event);soundListener.addSoundEvent(State.RESET, R.raw.reset_sound);soundListener.addSoundEvent(State.REFRESHING, R.raw.refreshing_sound);mPullRefreshListView.setOnPullEventListener(soundListener);// You can also just use setListAdapter(mAdapter) or// mPullRefreshListView.setAdapter(mAdapter)actualListView.setAdapter(mAdapter);}//類比網路載入資料的   非同步請求類//private class GetDataTask extends AsyncTask<Void, Void, String[]> {//子線程請求資料@Overrideprotected String[] doInBackground(Void... params) {// Simulates a background job.try {Thread.sleep(10);} catch (InterruptedException e) {}return mStrings;}//主線程更新UI@Overrideprotected void onPostExecute(String[] result) {//向RefreshListView Item 加入一行資料  並重新整理ListView//mListItems.addLast("Added after refresh...");mListItems.addFirst("Added after refresh...");mAdapter.notifyDataSetChanged();//通知RefreshListView 我們已經更新完畢// Call onRefreshComplete when the list has been refreshed.mPullRefreshListView.onRefreshComplete();super.onPostExecute(result);}}//資料來源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" };}



眼下編碼已經完畢  我們測試一下




                      




到此我們學習完畢








Android 學習之 開源項目PullToRefresh的使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.