標籤:下拉重新整理
很久沒用開源項目,重新複習一下下拉重新整理 也是想總結下一個項目,應該需要那些東西, 為自己打下基礎, 你也可以自己去下 library,關於源碼我會在後面放在
pulltorefresh(下拉重新整理) 有一個類是 PullToRefreshListView 因為我只講下拉重新整理,所以只取了一部分的類。 首先複製 將一個包
粘貼進 你的工程
需要注意 這個類還需要一些資源檔:
layout : header.xml 頭部出現的布局 pull_to_refresh_header.xml 重新整理的時候出現的布局 如果你們老闆不喜歡可以自己定製
drawable-hdpi:目錄下 有兩張圖片 pulltorefresh_down_arrow.png pulltorefresh_up_arrow.png 看名字知道 一個是下拉時圖片。一個下拉到一個距離 出現的
還需要一個res/values/attrs.xml 自訂標籤
還有res/values/Strings.xml
加入 這裡你可以編輯 下拉時候出現的文字
<string name="pull_to_refresh_pull_label">Pull to refresh...</string> <string name="pull_to_refresh_release_label">Release to refresh...</string> <string name="pull_to_refresh_refreshing_label">Loading...</string> <string name="pull_to_refresh_tap_label">Tap to refresh...</string>
其實對於這個開源項目,你可以理解為一個線性布局,因為它內建了一個Listview ,就和ListFagment一樣。
相信大家都已經把項目建好了,我在上面已經說過了,可以當作一個線性布局,so 你 就拿著當線性布局用就行了 先寫布局
、<pre name="code" class="html"><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" > <pulltorefresh.PullToRefreshListView android:id="@+id/ptrlv" android:layout_width="fill_parent" android:layout_height="fill_parent"> </pulltorefresh.PullToRefreshListView></RelativeLayout>
<pre name="code" class="html">pulltorefresh.PullToRefreshListView 是 PullToRefreshListView類的全名 為了避免出錯 ,直接複製包名 開啟 PullToRefreshListView 子節點 直接右擊一下 選擇 copy qualifiled Name<span style="font-size:18px;">開始寫java代碼吧 </span>既然在xml 裡定義了,肯定要在java代碼中執行個體化,<pre name="code" class="java"> private PullToRefreshListView pullToRefreshListView;pullToRefreshListView = (PullToRefreshListView) findViewById(R.id.ptrlv);
然後通過 getRefreshableView(); 得到一個 listview然後用adapter填充資料
接著 下拉重新整理有個方法 setOnRefreshListener 下拉監聽 通常情況下:請求網路新資料、解析、展示
全部代碼
package com.example.day24;import pulltorefresh.PullToRefreshBase.OnRefreshListener;import pulltorefresh.PullToRefreshListView;import android.app.Activity;import android.app.ActionBar;import android.app.Fragment;import android.os.AsyncTask;import android.os.Bundle;import android.view.LayoutInflater;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.ViewGroup;import android.widget.ArrayAdapter;import android.widget.ListView;import android.os.Build;public class MainActivity extends Activity { private PullToRefreshListView pullToRefreshListView; /** 將會被執行個體化為自訂的LinearLayout(PullToRefreshListView)中得到的ListView */private ListView lv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); pullToRefreshListView = (PullToRefreshListView) findViewById(R.id.ptrlv); // 從LinearLayout中得到一個ListView lv= pullToRefreshListView.getRefreshableView(); // 給ListView填充資料 ArrayAdapter<String>adapter=new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1, initData()); lv.setAdapter(adapter); pullToRefreshListView.setOnRefreshListener(new OnRefreshListener() {@Overridepublic void onRefresh() {//重新整理操作,通常情況下:請求網路新資料、解析、展示//造假:非同步.如果要給doInBackground()傳參,可以在execute()中傳參new MyAsyncT().execute();}}); }//listview中得假資料private String[] initData() {String [] datas=new String[10];for (int i = 0; i < datas.length; i++) {datas[i]="item"+i;}return datas;}/**泛型1:doInBackground()參數:::String url * 泛型2:onProgressUpdate()參數:::Integer progress * 泛型3:doInBackground()傳回值類型, * 同時也是onPostExecute()參數類型 通常:String byte[] Bitmap*/class MyAsyncT extends AsyncTask<String, Integer, String>{@Overrideprotected String doInBackground(String... params) {try {Thread.sleep(2000);} catch (InterruptedException e) {e.printStackTrace();}return null;}@Overrideprotected void onPostExecute(String result) {//非同步任務完成工作。告訴PullToRefreshListView任務完成pullToRefreshListView.onRefreshComplete();super.onPostExecute(result);}}}下一章 將下拉重新整理 上拉載入更多一起寫上
點擊下載源碼
安卓 (1)教你怎麼使用下拉重新整理