转载于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
谢谢原作者
分享到:
相关推荐
在Android开发中,PullToRefreshListView是一种常见的组件,它允许用户通过下拉列表视图来刷新数据,模拟了类似微博、微信等应用的下拉刷新功能。本篇将详细讲解如何实现这一功能,并探讨相关的设计思路和处理逻辑。...
本文将详细讲解如何在PullToRefreshListView中添加活动倒计时,并处理数据刷新时可能出现的倒计时错乱问题。 首先,我们需要理解倒计时的基本实现。Android提供了CountDownTimer类,它可以用来创建一个计时器,从...
本文将详细讲解如何在Android中实现这些布局的下拉刷新和上拉加载更多功能。 首先,`ScrollView`是最基础的可滚动视图,通常用于显示单个可滚动组件。要实现`ScrollView`的下拉刷新和上拉加载,开发者需要自定义`...
以下是对`PullToRefreshListView`库的详细讲解: 1. **库的安装与集成**:首先,你需要将`PullToRefreshListView`库添加到你的项目依赖中。如果是使用Maven或Gradle,可以在构建文件中添加对应的依赖。对于Gradle,...
本教程将针对Android-PullToRefresh-master进行详细讲解,适合初学者入门。 首先,我们需要了解什么是PullToRefresh。PullToRefresh是一种交互模式,用户通过在列表顶部下拉或底部上拉来触发刷新或加载更多数据的...
然而,为了提供更好的用户体验,很多应用都会添加下拉刷新的功能,让用户可以轻松地获取最新的数据。本篇将深入讲解如何在Android中自定义ListView来实现下拉刷新功能。 首先,我们需要理解下拉刷新的基本原理。...
本篇文章将详细讲解如何在Android项目中集成并使用PullToRefresh,以及如何自定义其行为。 首先,为了将PullToRefresh引入项目,我们需要进行以下步骤: 1. **导入PullToRefresh库**:在Android Studio中,可以...