国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android開源項目PullToRefresh下拉刷新功能詳解

2019-12-12 05:11:59
字體:
供稿:網(wǎng)友

先看看效果圖:

開源項地址:https://github.com/chrisbanes/Android-PullToRefresh 

下拉刷新這個功能我們都比較常見了,今天介紹的就是這個功能的實(shí)現(xiàn)。我將按照這個開源庫的范例來一點(diǎn)一點(diǎn)介紹,今天是介紹比較常見的PullToRefreshListView,是讓listView有下拉刷新功能。 

1.下載項目包,將library包導(dǎo)入即可,其他的包暫時不用

2.分析源碼,看我們可以設(shè)置的有哪些 

<?xml version="1.0" encoding="utf-8"?><resources> <declare-styleable name="PullToRefresh"> <!-- A drawable to use as the background of the Refreshable View --> <!-- 設(shè)置刷新view的背景 --> <attr name="ptrRefreshableViewBackground" format="reference|color" /> <!-- A drawable to use as the background of the Header and Footer Loading Views --> <!-- 設(shè)置頭部view的背景 --> <attr name="ptrHeaderBackground" format="reference|color" /> <!-- Text Color of the Header and Footer Loading Views --> <!-- 設(shè)置頭部/底部文字的顏色 --> <attr name="ptrHeaderTextColor" format="reference|color" /> <!-- Text Color of the Header and Footer Loading Views Sub Header --> <!-- 設(shè)置頭部/底部副標(biāo)題的文字顏色 --> <attr name="ptrHeaderSubTextColor" format="reference|color" /> <!-- Mode of Pull-to-Refresh that should be used --> <!-- 設(shè)置下拉刷新的模式,有多重方式可選。無刷新功能,從頂部刷新,從底部刷新,二者都有,只允許手動刷新 --> <attr name="ptrMode">  <flag name="disabled" value="0x0" />  <flag name="pullFromStart" value="0x1" />  <flag name="pullFromEnd" value="0x2" />  <flag name="both" value="0x3" />  <flag name="manualOnly" value="0x4" />  <!-- These last two are depreacted -->  <!-- 這兩個屬性不推薦了,用上面的代替即可 -->  <flag name="pullDownFromTop" value="0x1" />  <flag name="pullUpFromBottom" value="0x2" /> </attr> <!-- Whether the Indicator overlay(s) should be used --> <!-- 是否顯示指示箭頭 --> <attr name="ptrShowIndicator" format="reference|boolean" /> <!-- Drawable to use as Loading Indicator. Changes both Header and Footer. --> <!-- 指示箭頭的圖片 --> <attr name="ptrDrawable" format="reference" /> <!-- Drawable to use as Loading Indicator in the Header View. Overrides value set in ptrDrawable. --> <!-- 頂部指示箭頭的圖片,設(shè)置后會覆蓋ptrDrawable中頂部的設(shè)置 --> <attr name="ptrDrawableStart" format="reference" /> <!-- Drawable to use as Loading Indicator in the Fooer View. Overrides value set in ptrDrawable. --> <!-- 底部指示箭頭的圖片,設(shè)置后會覆蓋ptrDrawable中底部的設(shè)置 --> <attr name="ptrDrawableEnd" format="reference" /> <!-- Whether Android's built-in Over Scroll should be utilised for Pull-to-Refresh. --> <attr name="ptrOverScroll" format="reference|boolean" /> <!-- Base text color, typeface, size, and style for Header and Footer Loading Views --> <!-- 設(shè)置文字的基本字體 --> <attr name="ptrHeaderTextAppearance" format="reference" /> <!-- Base text color, typeface, size, and style for Header and Footer Loading Views Sub Header --> <!-- 設(shè)置副標(biāo)題的基本字體 --> <attr name="ptrSubHeaderTextAppearance" format="reference" /> <!-- Style of Animation should be used displayed when pulling. --> <!-- 設(shè)置下拉時標(biāo)識圖的動畫,默認(rèn)為rotate --> <attr name="ptrAnimationStyle">  <flag name="rotate" value="0x0" />  <flag name="flip" value="0x1" /> </attr> <!-- Whether the user can scroll while the View is Refreshing --> <!-- 設(shè)置刷新時是否允許滾動,一般為true --> <attr name="ptrScrollingWhileRefreshingEnabled" format="reference|boolean" /> <!--  Whether PullToRefreshListView has it's extras enabled. This allows the user to be   able to scroll while refreshing, and behaves better. It acheives this by adding  Header and/or Footer Views to the ListView. --> <!-- 允許在listview中添加頭/尾視圖 --> <attr name="ptrListViewExtrasEnabled" format="reference|boolean" /> <!--  Whether the Drawable should be continually rotated as you pull. This only  takes effect when using the 'Rotate' Animation Style. --> <!-- 當(dāng)設(shè)置rotate時,可以用這個來設(shè)置刷新時旋轉(zhuǎn)的圖片 --> <attr name="ptrRotateDrawableWhilePulling" format="reference|boolean" /> <!-- BELOW HERE ARE DEPRECEATED. DO NOT USE. --> <attr name="ptrAdapterViewBackground" format="reference|color" /> <attr name="ptrDrawableTop" format="reference" /> <attr name="ptrDrawableBottom" format="reference" /> </declare-styleable></resources>

看到有這么多可以設(shè)置的屬性,別以為真的就可以定制了。真正要定制還得到layout中改變刷新布局 

3.開始用它建立自己的工程
 設(shè)置布局文件
 就是插入PullToRefreshListView

<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" tools:context="${relativePackage}.${activityClass}"  android:background="#000000"><!-- The PullToRefreshListView replaces a standard ListView widget. --> <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:cacheColorHint="#000000" android:divider="#19000000" android:dividerHeight="4dp" android:fadingEdge="none" android:fastScrollEnabled="false" android:footerDividersEnabled="false" android:headerDividersEnabled="false" android:smoothScrollbar="true"  ptr:ptrAnimationStyle="rotate" ptr:ptrHeaderTextColor="#ffffff" ptr:ptrHeaderSubTextColor="#00ffff" ptr:ptrHeaderBackground="@null" ptr:ptrDrawable="@drawable/ic_launcher"/> </RelativeLayout>

開始編寫代碼 

1.找到這個控件,并且設(shè)置監(jiān)聽器 

這里面用到了一個日期的工具類,其實(shí)就是設(shè)置上次下拉的時間的。此外在下拉后會觸發(fā)一個異步任務(wù) 

 /** * 設(shè)置下拉刷新的listview的動作 */ private void initPTRListView() { mPullRefreshListView = (PullToRefreshListView) findViewById(R.id.pull_refresh_list); //設(shè)置拉動監(jiān)聽器 mPullRefreshListView.setOnRefreshListener(new OnRefreshListener<ListView>() {  @Override  public void onRefresh(PullToRefreshBase<ListView> refreshView) {  //設(shè)置下拉時顯示的日期和時間  String label = DateUtils.formatDateTime(getApplicationContext(), System.currentTimeMillis(),   DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);  // 更新顯示的label  refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);  // 開始執(zhí)行異步任務(wù),傳入適配器來進(jìn)行數(shù)據(jù)改變  new GetDataTask(mPullRefreshListView, mAdapter,mListItems).execute();  } }); // 添加滑動到底部的監(jiān)聽器 mPullRefreshListView.setOnLastItemVisibleListener(new OnLastItemVisibleListener() {    @Override  public void onLastItemVisible() {  Toast.makeText(getApplication(), "已經(jīng)到底了", Toast.LENGTH_SHORT).show();  } });  //mPullRefreshListView.isScrollingWhileRefreshingEnabled();//看刷新時是否允許滑動 //在刷新時允許繼續(xù)滑動 mPullRefreshListView.setScrollingWhileRefreshingEnabled(true); //mPullRefreshListView.getMode();//得到模式 //上下都可以刷新的模式。這里有兩個選擇:Mode.PULL_FROM_START,Mode.BOTH,PULL_FROM_END mPullRefreshListView.setMode(Mode.BOTH);  /**  * 設(shè)置反饋音效  */ 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); }

2.從上面的那個控件中,得到它包含的listView,并且設(shè)置適配器 

 //普通的listview對象 private ListView actualListView; //添加一個鏈表數(shù)組,來存放string數(shù)組,這樣就可以動態(tài)增加string數(shù)組中的內(nèi)容了 private LinkedList<String> mListItems; //給listview添加一個普通的適配器 private ArrayAdapter<String> mAdapter;

這里用到了一個LinkedList的對象,這個是一個類似于ArrayList的鏈表數(shù)組,比較方便在開頭和末尾添加String 

 /** * 設(shè)置listview的適配器 */ private void initListView() { //通過getRefreshableView()來得到一個listview對象 actualListView = mPullRefreshListView.getRefreshableView();  String []data = new String[] {"android","ios","wp","java","c++","c#"}; mListItems = new LinkedList<String>(); //把string數(shù)組中的string添加到鏈表中 mListItems.addAll(Arrays.asList(data));  mAdapter = new ArrayAdapter<>(getApplicationContext(),   android.R.layout.simple_list_item_1, mListItems); actualListView.setAdapter(mAdapter); }

 

3.寫一個異步任務(wù),來模仿從網(wǎng)絡(luò)加載數(shù)據(jù)

這里要注意的是,加載完后要出發(fā)刷新完成和通知適配器改變的方法

package com.kale.ptrlistviewtest;import java.util.LinkedList;import android.os.AsyncTask;import android.widget.ArrayAdapter;import com.handmark.pulltorefresh.library.PullToRefreshListView;import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;/** * @author:Jack Tony * @tips :通過異步任務(wù)來加載網(wǎng)絡(luò)中的數(shù)據(jù),進(jìn)行更新 * @date :2014-10-14 */public class GetDataTask extends AsyncTask<Void, Void, Void>{ private PullToRefreshListView mPullRefreshListView; private ArrayAdapter<String> mAdapter; private LinkedList<String> mListItems;  public GetDataTask(PullToRefreshListView listView,  ArrayAdapter<String> adapter,LinkedList<String> listItems) { // TODO 自動生成的構(gòu)造函數(shù)存根 mPullRefreshListView = listView; mAdapter = adapter; mListItems = listItems; }  @Override protected Void doInBackground(Void... params) { //模擬請求 try {  Thread.sleep(2000); } catch (InterruptedException e) { } return null; }  @Override protected void onPostExecute(Void result) { // TODO 自動生成的方法存根 super.onPostExecute(result); //得到當(dāng)前的模式 Mode mode = mPullRefreshListView.getCurrentMode(); if(mode == Mode.PULL_FROM_START) {  mListItems.addFirst("這是刷新出來的數(shù)據(jù)"); } else {  mListItems.addLast("這是刷新出來的數(shù)據(jù)"); } // 通知數(shù)據(jù)改變了 mAdapter.notifyDataSetChanged(); // 加載完成后停止刷新 mPullRefreshListView.onRefreshComplete();  } }

貼上acitivty中的全部代碼 

MainActivity.java 

package com.kale.ptrlistviewtest;import java.util.Arrays;import java.util.LinkedList;import android.app.Activity;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.Mode;import com.handmark.pulltorefresh.library.PullToRefreshBase.OnLastItemVisibleListener;import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;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 {  //一個可以下拉刷新的listView對象 private PullToRefreshListView mPullRefreshListView; //普通的listview對象 private ListView actualListView; //添加一個鏈表數(shù)組,來存放string數(shù)組,這樣就可以動態(tài)增加string數(shù)組中的內(nèi)容了 private LinkedList<String> mListItems; //給listview添加一個普通的適配器 private ArrayAdapter<String> mAdapter;  @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);  initView(); //一打開應(yīng)用就自動刷新,下面語句可以寫到刷新按鈕里面 mPullRefreshListView.setRefreshing(true); //new GetDataTask(mPullRefreshListView, mAdapter, mListItems).execute(); //mPullRefreshListView.setRefreshing(false); } private void initView() { initPTRListView(); initListView(); }  /** * 設(shè)置下拉刷新的listview的動作 */ private void initPTRListView() { mPullRefreshListView = (PullToRefreshListView) findViewById(R.id.pull_refresh_list); //設(shè)置拉動監(jiān)聽器 mPullRefreshListView.setOnRefreshListener(new OnRefreshListener<ListView>() {  @Override  public void onRefresh(PullToRefreshBase<ListView> refreshView) {  //設(shè)置下拉時顯示的日期和時間  String label = DateUtils.formatDateTime(getApplicationContext(), System.currentTimeMillis(),   DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL);  // 更新顯示的label  refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(label);  // 開始執(zhí)行異步任務(wù),傳入適配器來進(jìn)行數(shù)據(jù)改變  new GetDataTask(mPullRefreshListView, mAdapter,mListItems).execute();  } }); // 添加滑動到底部的監(jiān)聽器 mPullRefreshListView.setOnLastItemVisibleListener(new OnLastItemVisibleListener() {    @Override  public void onLastItemVisible() {  Toast.makeText(getApplication(), "已經(jīng)到底了", Toast.LENGTH_SHORT).show();  } });  //mPullRefreshListView.isScrollingWhileRefreshingEnabled();//看刷新時是否允許滑動 //在刷新時允許繼續(xù)滑動 mPullRefreshListView.setScrollingWhileRefreshingEnabled(true); //mPullRefreshListView.getMode();//得到模式 //上下都可以刷新的模式。這里有兩個選擇:Mode.PULL_FROM_START,Mode.BOTH,PULL_FROM_END mPullRefreshListView.setMode(Mode.BOTH);  /**  * 設(shè)置反饋音效  */ 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); }  /** * 設(shè)置listview的適配器 */ private void initListView() { //通過getRefreshableView()來得到一個listview對象 actualListView = mPullRefreshListView.getRefreshableView();  String []data = new String[] {"android","ios","wp","java","c++","c#"}; mListItems = new LinkedList<String>(); //把string數(shù)組中的string添加到鏈表中 mListItems.addAll(Arrays.asList(data));  mAdapter = new ArrayAdapter<>(getApplicationContext(),   android.R.layout.simple_list_item_1, mListItems); actualListView.setAdapter(mAdapter); }}

源碼下載:http://xiazai.VeVB.COm/201609/yuanma/AndroidListView(VeVB.COm).rar

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 明水县| 浦城县| 三明市| 博白县| 潼南县| 娱乐| 上思县| 探索| 洛隆县| 界首市| 淄博市| 南江县| 冷水江市| 龙山县| 中西区| 聂荣县| 通辽市| 城口县| 改则县| 翁牛特旗| 工布江达县| 崇左市| 类乌齐县| 大宁县| 陈巴尔虎旗| 东阳市| 秭归县| 上犹县| 中卫市| 株洲县| 广丰县| 新昌县| 乌苏市| 南川市| 多伦县| 江阴市| 吉水县| 玉屏| 蒙阴县| 武鸣县| 科尔|