Google官方SwipeRefreshLayout下拉重新整理的用法。,swiperefreshlayout

來源:互聯網
上載者:User

Google官方SwipeRefreshLayout下拉重新整理的用法。,swiperefreshlayout

《Android SwipeRefreshLayout:Google官方SDK包中的下拉重新整理》
下拉重新整理在如今移動開發中應用如此廣泛和普遍,以至於Google乾脆在SDK中給予支援。在android-support-v4包中,Google增加了SwipeRefreshLayout,該組件提供基礎的下拉重新整理表現能力和開放出來供開發人員調用的基本介面。

 1 package com.lixu.SwipeRefreshLayoutyongfa; 2  3 import java.util.ArrayList; 4 import android.app.Activity; 5 import android.os.AsyncTask; 6 import android.os.Bundle; 7 import android.support.v4.widget.SwipeRefreshLayout; 8 import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener; 9 import android.widget.ArrayAdapter;10 import android.widget.ListView;11 12 public class MainActivity extends Activity {13     private ArrayAdapter<String> adapter;14     private ArrayList<String> date;15     private SwipeRefreshLayout srl;16     private int count = 0;17 18     @Override19     protected void onCreate(Bundle savedInstanceState) {20         super.onCreate(savedInstanceState);21         setContentView(R.layout.activity_main);22 23         date = new ArrayList<String>();24 25         ListView lv = (ListView) findViewById(R.id.lv);26 27         srl = (SwipeRefreshLayout) findViewById(R.id.srl);28         // 設定重新整理動畫的顏色.29         srl.setColorSchemeResources(android.R.color.holo_green_light, android.R.color.holo_blue_bright,30                 android.R.color.holo_red_light);31 32         srl.setOnRefreshListener(new OnRefreshListener() {33             // SwipeRefreshLayout接管其包裹的ListView下拉事件。34             // 每一次對ListView的下拉動作,將觸發SwipeRefreshLayout的onRefresh()。35             @SuppressWarnings("unchecked")36             @Override37             public void onRefresh() {38 39                 new MyAsyncTask().execute();40 41             }42         });43         adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, date);44 45         lv.setAdapter(adapter);46     }47 48     private class MyAsyncTask extends AsyncTask {49         @Override50         protected void onPreExecute() {51             super.onPreExecute();52             // 重新整理開始53             srl.setRefreshing(true);54         }55 56         @Override57         protected Object doInBackground(Object... params) {58             // 處理一些耗時的事件59             return count++;60         }61 62         @Override63         protected void onPostExecute(Object result) {64             super.onPostExecute(result);65             // add(0,xxx)每次將更新的資料xxx添加到頭部。66             date.add(0, "" + result);67             // 重新整理適配器68             adapter.notifyDataSetChanged();69             // 重新整理完畢70             srl.setRefreshing(false);71         }72 73     }74 75 }

xml檔案:

 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2     xmlns:tools="http://schemas.android.com/tools" 3     android:layout_width="match_parent" 4     android:layout_height="match_parent" 5     tools:context="com.lixu.SwipeRefreshLayoutyongfa.MainActivity" > 6  7     <android.support.v4.widget.SwipeRefreshLayout 8         android:id="@+id/srl" 9         android:layout_width="match_parent"10         android:layout_height="match_parent" >11 12         <ListView13             android:id="@+id/lv"14             android:layout_width="match_parent"15             android:layout_height="match_parent" />16     </android.support.v4.widget.SwipeRefreshLayout>17 18 </RelativeLayout>

運行效果:

聯繫我們

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