转载于http://blog.csdn.net/mmjiajia132/article/details/40397813
PullToRefreshListView 用法和ListView 没有什么区别 listview能用的属性 pulltorefresh也能用
我一直认为动手是最好的学习方法...
一:首先看布局文件
- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical">
-
- <!--ptr:ptrAnimationStyle="flip"flip:翻转rotate:旋转-->
- <!--ptr:ptrShowIndicator="true"右上角右下角出现箭头-->
- <com.handmark.pulltorefresh.library.PullToRefreshListView
- xmlns:ptr="http://schemas.android.com/apk/res-auto"
- android:id="@+id/pullToRefresh"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- ptr:ptrDrawable="@drawable/default_ptr_flip"
- ptr:ptrAnimationStyle="flip"
- ptr:ptrHeaderBackground="#383838"
- ptr:ptrHeaderTextColor="#FFFFFF"
- />
-
- </LinearLayout>
ptr是pullToRefresh的配置属性 使用是需要添加xmlns:ptr="http://schemas.android.com/apk/res-auto"
ptr:ptrDrawable=“” 上拉下拉图标
ptr:ptrAnimationStyle="" 图标动画 取值: flip:翻转 rotate旋转
ptr:ptrHeaderBackground="" 上拉下拉时 头部的背景色
ptr:ptrHeaderTextColor="" 上拉下拉时 文字颜色
还有一些常用属性
ptrRefreshableViewBackground 设置整个mPullRefreshListView的背景色
ptrScrollingWhileRefreshingEnabled刷新的时候,是否允许ListView或GridView滚动。觉得为true比较好。
ptrListViewExtrasEnabled 决定了Header,Footer以何种方式加入mPullRefreshListView,true为headView方式加入,就是滚动时刷新头部会一起滚动。
注:上述属性都可以代码添加,请用pullToRefresh.set查看
二:MainActivity代码
pullToRefresh适配器Adapter和listview也是继承于BaseAdapter 看一下item的布局
item.xml
- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:padding="5dp"
- android:orientation="vertical">
-
- <TextView
- android:id="@+id/title"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textSize="18sp"
- android:textColor="#BA55D3"
- android:text="我的神"/>
-
- <TextView
- android:id="@+id/content"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textSize="14.0sp"
- android:layout_marginTop="5dp"
- android:textColor="#7CFC00"
- android:text="我的神"/>
- </LinearLayout>
pullToRefresh 通过setMode来设置是否可以上拉下拉
Mode.BOTH:同时支持上拉下拉
Mode.PULL_FROM_START:只支持下拉Pulling Down
Mode.PULL_FROM_END:只支持上拉Pulling Up
也可以用ptr:ptrMode="both"
可选值为:disabled(禁用下拉刷新),pullFromStart(仅支持下拉刷新),pullFromEnd(仅支持上拉刷新),both(二者都支持),manualOnly(只允许手动触发)
如果Mode设置成Mode.BOTH,需要设置刷新Listener为OnRefreshListener2,并实现onPullDownToRefresh()、onPullUpToRefresh()两个方法。
如果Mode设置成Mode.PULL_FROM_START或Mode.PULL_FROM_END,需要设置刷新Listener为OnRefreshListener,同时实现onRefresh()方法。
当然也可以设置为OnRefreshListener2,但是Mode.PULL_FROM_START的时候只调用onPullDownToRefresh()方法,Mode.PULL_FROM的时候只调用onPullUpToRefresh()方法.
如果想上拉、下拉刷新的时候 做一样的操作,那就用OnRefreshListener,上拉下拉的时候都调用
如果想上拉、下拉做不一样的的操作,那就在setOnRefreshListener时 用new OnRefreshListener2<ListView>
当然如果想自己设置上拉下拉中的文字 可以这样
- ILoadingLayoutstartLabels=pullToRefresh
- .getLoadingLayoutProxy(true,false);
- startLabels.setPullLabel("下拉刷新...");
- startLabels.setRefreshingLabel("正在载入...");
- startLabels.setReleaseLabel("放开刷新...");
-
- ILoadingLayoutendLabels=pullToRefresh.getLoadingLayoutProxy(
-
false,true);
- endLabels.setPullLabel("上拉刷新...");
- endLabels.setRefreshingLabel("正在载入...");
- endLabels.setReleaseLabel("放开刷新...");
当然也可以这样
- pullToRefresh.getLoadingLayoutProxy(false,true)
- .setPullLabel("上拉刷新...");
- pullToRefresh.getLoadingLayoutProxy(false,true).setReleaseLabel(
-
"放开刷新...");
- pullToRefresh.getLoadingLayoutProxy(false,true).setRefreshingLabel(
-
"正在加载...");
- pullToRefresh.getLoadingLayoutProxy(true,false)
- .setPullLabel("下拉刷新...");
- pullToRefresh.getLoadingLayoutProxy(true,false).setReleaseLabel(
-
"放开刷新...");
- pullToRefresh.getLoadingLayoutProxy(true,false).setRefreshingLabel(
-
"正在加载...");
显然在实际操作的时候也会用到其他监听
setOnScrollListener()
SCROLL_STATE_TOUCH_SCROLL 正在滚动
SCROLL_STATE_FLING 手指做了抛的动作(手指离开屏幕前,用力滑了一下)
SCROLL_STATE_IDLE 停止滚动
setOnLastItemVisibleListener
当用户拉到底时调用
setOnItemClickListener()
为pullToRefresh中每一个item设置事件
代码下载:点击下载代码
下拉上拉 图标和文字 位置改动是在PullToRefresh源代码中改的即:PullToRefreshListView.handleStyledAttributes 中lp的Gravity改为CENTER_VERTICAL
如果想要改动图标和文字的距离和布局 在这library项目下这两个文件改
pull_to_refresh_header_horizontal.xml
pull_to_refresh_header_vertical.xml
参考博客:
http://blog.csdn.net/lmj623565791/article/details/38238749
http://blog.csdn.net/harvic880925/article/details/17680305
谢谢原作者
分享到:
相关推荐
下拉刷新类库,pulltorefreshlistview
详细讲解了pullToRefreshListView的用法
一个支持顶部下拉刷新和底部上拉刷新的ListView控件,具体见这篇博客http://my.eoe.cn/bupt/archive/19864.html
使用PullToRefreshListView实现上下拉刷新,压缩包里有详细说明 框架都有
PullToRefreshListView需要的第三方library
PullToRefreshListView下拉刷新与上拉加载
PulltorefreshListView 工程源代码
本人总结的简洁的刷新加载数据处理逻辑(pulltorefreshlistview),有什么想法都可以提出来
是一个简单但是完整的下拉刷新项目,不需要积分,仅供参考和共同学习
PullToRefresh是很多项目都使用的刷新加载框架,本实例实现一个listview的刷新加载
PullToRefreshListView-源码.rar
PullToRefresh是很多项目都使用的刷新加载框架,本实例实现一个listview的刷新加载
TreeListview+PullToRefreshListView
《PullToRefresh使用详解--构建下拉刷新的listView》博文对应原源码,博客地址:http://blog.csdn.net/harvic880925/article/details/17680305,讲解了PullToRefresh构建可下拉刷新的listView的过程;
PullToRefreshListView添加活动倒计时,防止数据刷新时 ,倒计时显示错乱,本人新手,写的不好,还请大神多多指点!
PullToRefreshListView这个开源的拉动刷新的listview还是很好用的,拓展性强的特点,值得你拥有。
Android 自定义ListView,分别实现下拉刷新的ListView及上拉加载的ListView 详情可以参考我的博客 http://blog.csdn.net/sunshanai/article/details/51622917