先看看效果图:
开源项地址:https://github.com/chrisbanes/Android-PullToRefresh
下拉刷新这个功能我们都比较常见了,今天介绍的就是这个功能的实现。我将按照这个开源库的范例来一点一点介绍,今天是介绍比较常见的PullToRefreshListVIEw,是让ListVIEw有下拉刷新功能。
1.下载项目包,将library包导入即可,其他的包暂时不用
2.分析源码,看我们可以设置的有哪些
<?xml version="1.0" enCoding="utf-8"?><resources> <declare-styleable name="PullToRefresh"> <!-- A drawable to use as the background of the Refreshable VIEw --> <!-- 设置刷新vIEw的背景 --> <attr name="ptrRefreshableVIEwBackground" format="reference|color" /> <!-- A drawable to use as the background of the header and Footer Loading VIEws --> <!-- 设置头部vIEw的背景 --> <attr name="ptrheaderBackground" format="reference|color" /> <!-- Text color of the header and Footer Loading VIEws --> <!-- 设置头部/底部文字的颜色 --> <attr name="ptrheaderTextcolor" format="reference|color" /> <!-- Text color of the header and Footer Loading VIEws Sub header --> <!-- 设置头部/底部副标题的文字颜色 --> <attr name="ptrheaderSubTextcolor" format="reference|color" /> <!-- Mode of Pull-to-Refresh that should be used --> <!-- 设置下拉刷新的模式,有多重方式可选。无刷新功能,从顶部刷新,从底部刷新,二者都有,只允许手动刷新 --> <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. --> <!-- 顶部指示箭头的图片,设置后会覆盖ptrDrawable中顶部的设置 --> <attr name="ptrDrawableStart" format="reference" /> <!-- Drawable to use as Loading Indicator in the Fooer VIEw. OverrIDes value set in ptrDrawable. --> <!-- 底部指示箭头的图片,设置后会覆盖ptrDrawable中底部的设置 --> <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 --> <!-- 设置文字的基本字体 --> <attr name="ptrheaderTextAppearance" format="reference" /> <!-- Base text color,and style for header and Footer Loading VIEws Sub header --> <!-- 设置副标题的基本字体 --> <attr name="ptrSubheaderTextAppearance" format="reference" /> <!-- Style of Animation should be used displayed when pulling. --> <!-- 设置下拉时标识图的动画,默认为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 --> <!-- 设置刷新时是否允许滚动,一般为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. --> <!-- 当设置rotate时,可以用这个来设置刷新时旋转的图片 --> <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>
看到有这么多可以设置的属性,别以为真的就可以定制了。真正要定制还得到layout中改变刷新布局
3.开始用它建立自己的工程
设置布局文件
就是插入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.找到这个控件,并且设置监听器
这里面用到了一个日期的工具类,其实就是设置上次下拉的时间的。此外在下拉后会触发一个异步任务
/** * 设置下拉刷新的ListvIEw的动作 */ private voID initPTRListVIEw() { mpullRefreshListVIEw = (PullToRefreshListVIEw) findVIEwByID(R.ID.pull_refresh_List); //设置拉动监听器 mpullRefreshListVIEw.setonRefreshListener(new OnRefreshListener<ListVIEw>() { @OverrIDe public voID onRefresh(PullToRefreshBase<ListVIEw> refreshVIEw) { //设置下拉时显示的日期和时间 String label = DateUtils.formatDateTime(getApplicationContext(),System.currentTimeMillis(),DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_ABBREV_ALL); // 更新显示的label refreshVIEw.getLoadingLayoutProxy().setLastUpdatedLabel(label); // 开始执行异步任务,传入适配器来进行数据改变 new GetDataTask(mpullRefreshListVIEw,mAdapter,mListItems).execute(); } }); // 添加滑动到底部的监听器 mpullRefreshListVIEw.setonLastItemVisibleListener(new OnLastItemVisibleListener() { @OverrIDe public voID onLastItemVisible() { Toast.makeText(getApplication(),"已经到底了",Toast.LENGTH_SHORT).show(); } }); //mpullRefreshListVIEw.isScrollingWhileRefreshingEnabled();//看刷新时是否允许滑动 //在刷新时允许继续滑动 mpullRefreshListVIEw.setScrollingWhileRefreshingEnabled(true); //mpullRefreshListVIEw.getMode();//得到模式 //上下都可以刷新的模式。这里有两个选择:Mode.PulL_FROM_START,Mode.BOTH,PulL_FROM_END mpullRefreshListVIEw.setMode(Mode.BOTH); /** * 设置反馈音效 */ 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,并且设置适配器
//普通的ListvIEw对象 private ListVIEw actualListVIEw; //添加一个链表数组,来存放string数组,这样就可以动态增加string数组中的内容了 private linkedList<String> mListItems; //给ListvIEw添加一个普通的适配器 private ArrayAdapter<String> mAdapter;
这里用到了一个linkedList的对象,这个是一个类似于ArrayList的链表数组,比较方便在开头和末尾添加String
/** * 设置ListvIEw的适配器 */ private voID initListVIEw() { //通过getRefreshableVIEw()来得到一个ListvIEw对象 actualListVIEw = mpullRefreshListVIEw.getRefreshableVIEw(); String []data = new String[] {"androID","ios","wp","java","c++","c#"}; mListItems = new linkedList<String>(); //把string数组中的string添加到链表中 mListItems.addAll(Arrays.asList(data)); mAdapter = new ArrayAdapter<>(getApplicationContext(),androID.R.layout.simple_List_item_1,mListItems); actualListVIEw.setAdapter(mAdapter); }
3.写一个异步任务,来模仿从网络加载数据
这里要注意的是,加载完后要出发刷新完成和通知适配器改变的方法
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 :通过异步任务来加载网络中的数据,进行更新 * @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 自动生成的构造函数存根 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); //得到当前的模式 Mode mode = mpullRefreshListVIEw.getCurrentMode(); if(mode == Mode.PulL_FROM_START) { mListItems.addFirst("这是刷新出来的数据"); } else { mListItems.addLast("这是刷新出来的数据"); } // 通知数据改变了 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; //添加一个链表数组,来存放string数组,这样就可以动态增加string数组中的内容了 private linkedList<String> mListItems; //给ListvIEw添加一个普通的适配器 private ArrayAdapter<String> mAdapter; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_main); initVIEw(); //一打开应用就自动刷新,下面语句可以写到刷新按钮里面 mpullRefreshListVIEw.setRefreshing(true); //new GetDataTask(mpullRefreshListVIEw,mListItems).execute(); //mpullRefreshListVIEw.setRefreshing(false); } private voID initVIEw() { initPTRListVIEw(); initListVIEw(); } /** * 设置下拉刷新的ListvIEw的动作 */ private voID initPTRListVIEw() { mpullRefreshListVIEw = (PullToRefreshListVIEw) findVIEwByID(R.ID.pull_refresh_List); //设置拉动监听器 mpullRefreshListVIEw.setonRefreshListener(new OnRefreshListener<ListVIEw>() { @OverrIDe public voID onRefresh(PullToRefreshBase<ListVIEw> refreshVIEw) { //设置下拉时显示的日期和时间 String label = DateUtils.formatDateTime(getApplicationContext(),R.raw.refreshing_sound); mpullRefreshListVIEw.setonPullEventListener(soundListener); } /** * 设置ListvIEw的适配器 */ private voID initListVIEw() { //通过getRefreshableVIEw()来得到一个ListvIEw对象 actualListVIEw = mpullRefreshListVIEw.getRefreshableVIEw(); String []data = new String[] {"androID",mListItems); actualListVIEw.setAdapter(mAdapter); }}
源码下载:http://xiazai.jb51.net/201609/yuanma/AndroidListView(jb51.net).rar
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。
总结以上是内存溢出为你收集整理的Android开源项目PullToRefresh下拉刷新功能详解全部内容,希望文章能够帮你解决Android开源项目PullToRefresh下拉刷新功能详解所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)