Android listView的下拉重新整理

來源:互聯網
上載者:User

標籤:android 下拉重新整理

Android ListView的下拉重新整理,在實際開發中是很常用的,所以這裡總結了,ListView下拉重新整理的一個Demo。

該Demo的源碼來自於github上的一個開原始碼,只不過這裡是將所需的library匯入到項目中,然後將PulltoRefreshListview提取出來,進行了注釋,看起來更簡單。。。

該開原始碼出來ListView的下拉重新整理外,還有很多其他的重新整理功能,如果有興趣,可以把代碼下下來研究一下,文章結尾會給出所有的源碼。

實現:


原始碼:


在寫該Demo之前,需要:

第一步:匯入三個Android library:如library、pulltorefreshlistfragment、pulltorefreshviewpager;

第二部:將library分別add到 pulltorefreshlistfragment pulltorefreshviewpager;

第三部:這時候將這三個Android library分別add到本項目Demo中。

然後可以寫代碼了:

布局檔案:

activity_pull_to_refresh_list.xml:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" ><!--     The PullToRefreshListView replaces a standard ListView widget. -->    <com.handmark.pulltorefresh.library.PullToRefreshListView        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" /></LinearLayout>
代碼檔案:
PullToRefreshListActivity.java:

package com.listviewpulltorefresh;import java.util.Arrays;import java.util.LinkedList;import android.app.ListActivity;import android.os.AsyncTask;import android.os.Bundle;import android.text.format.DateUtils;import android.widget.ArrayAdapter;import android.widget.ListView;import android.widget.Toast;import com.handmark.pulltorefresh.library.PullToRefreshBase;import com.handmark.pulltorefresh.library.PullToRefreshBase.OnLastItemVisibleListener;import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;import com.handmark.pulltorefresh.library.PullToRefreshListView;public final class PullToRefreshListActivity extends ListActivity {private LinkedList<String> mListItems;private PullToRefreshListView mPullRefreshListView;private ArrayAdapter<String> mAdapter;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_pull_to_refresh_list);mPullRefreshListView = (PullToRefreshListView) findViewById(R.id.pull_refresh_list);/** * 當List重新整理的時候相應該事件。 */mPullRefreshListView.setOnRefreshListener(new OnRefreshListener<ListView>() {@Overridepublic 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);/** * 該方法進行重新整理的工作,主要使用AsyncTask這個線程池來實現。。 */new GetDataTask().execute();}});/** * 當最後一個item出現的時候,出現“已滑動到底部...”提示。 */mPullRefreshListView.setOnLastItemVisibleListener(new OnLastItemVisibleListener() {@Overridepublic void onLastItemVisible() {Toast.makeText(PullToRefreshListActivity.this,"已滑動到底部...", Toast.LENGTH_SHORT).show();}});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);actualListView.setAdapter(mAdapter);}private class GetDataTask extends AsyncTask<Void, Void, String[]> {@Overrideprotected String[] doInBackground(Void... params) {try {Thread.sleep(2000);} catch (InterruptedException e) {}return mStrings;}@Overrideprotected void onPostExecute(String[] result) {mListItems.addFirst("重新整理後新增資料...");mAdapter.notifyDataSetChanged();/** * 重新整理完成後調用該方法。 */mPullRefreshListView.onRefreshComplete();super.onPostExecute(result);}}/** * ListView中資料 */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" };}

原始碼下載:

點擊下載源碼

github完整項目下載:

點擊下載源碼

源地址:

https://github.com/chrisbanes/Android-PullToRefresh#please-note-this-project-is-no-longer-being-maintained


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.