您的位置:首页 > 移动开发 > Android开发

(4.2.38) android-Ultra-Pull-To-Refresh 使用简介

2017-01-02 00:02 288 查看
项目地址https://github.com/liaohuqiu/Android-Ultra-Pull-To-Refresh该项目只包含下拉刷新,可以包裹任何控件,如果需要添加上拉加载,参看下篇。引入方法1:直接导入lib库:ptr-lib,可到项目地址下载,然后将lib添加到项目
compile project(':ptr-lib')
方法2:中央库依赖compile 'in.srain.cube:ultra-ptr:1.0.11'
配置
[align=left]有6个参数可配置:[/align][align=left]阻尼系数[/align][align=left]默认: 1.7f,越大,感觉下拉时越吃力。[/align][align=left]触发刷新时移动的位置比例[/align][align=left]默认,1.2f,移动达到头部高度1.2倍时可触发刷新操作。[/align][align=left]回弹延时[/align][align=left]默认 200ms,回弹到刷新高度所用时间[/align][align=left]头部回弹时间[/align][align=left]默认1000ms[/align][align=left]刷新是保持头部[/align][align=left]默认值 true.[/align][align=left]下拉刷新 / 释放刷新[/align][align=left]默认为释放刷新[/align][html] view plain copy <in.srain.cube.views.ptr.PtrFrameLayout      android:id="@+id/store_house_ptr_frame"      xmlns:cube_ptr="http://schemas.android.com/apk/res-auto"      android:layout_width="match_parent"      android:layout_height="match_parent"         cube_ptr:ptr_resistance="1.7"      cube_ptr:ptr_ratio_of_header_height_to_refresh="1.2"      cube_ptr:ptr_duration_to_close="300"      cube_ptr:ptr_duration_to_close_header="2000"      cube_ptr:ptr_keep_header_when_refresh="true"      cube_ptr:ptr_pull_to_fresh="false" >         <LinearLayout          android:id="@+id/store_house_ptr_image_content"          android:layout_width="match_parent"          android:layout_height="match_parent"          android:background="@color/cube_mints_333333"          android:clickable="true"          android:padding="10dp">             <in.srain.cube.image.CubeImageView              android:id="@+id/store_house_ptr_image"              android:layout_width="match_parent"              android:layout_height="match_parent" />      </LinearLayout>     </in.srain.cube.views.ptr.PtrFrameLayout>  也可以在代码中配置
[java] view plain copy // the following are default settings  mPtrFrame.setResistance(1.7f);  mPtrFrame.setRatioOfHeaderHeightToRefresh(1.2f);  mPtrFrame.setDurationToClose(200);  mPtrFrame.setDurationToCloseHeader(1000);  // default is false  mPtrFrame.setPullToRefresh(false);  // default is true  mPtrFrame.setKeepHeaderWhenRefresh(true);  [java] view plain copy 刷新时,保持内容不动,仅头部下移, setPinContent()  
下拉刷新功能接口,对下拉刷新功能的抽象,包含以下两个方法。
public void onRefreshBegin(final PtrFrameLayout frame)
刷新回调函数,用户在这里写自己的刷新功能实现,处理业务数据的刷新。
public boolean checkCanDoRefresh(final PtrFrameLayout frame, final View content, final View header)
判断是否可以下拉刷新。 UltraPTR 的 Content 可以包含任何内容,用户在这里判断决定是否可以下拉。例如,如果 Content 是 TextView,则可以直接返回 true,表示可以下拉刷新。如果 Content 是 ListView,当第一条在顶部时返回 true,表示可以下拉刷新。如果 Content 是 ScrollView,当滑动到顶部时返回 true,表示可以刷新。案例示例:此段代码在设置了rvView(RecycleView)相关配置之后之后进行设置即可,
[java] view plain copy mPtrFrame.setPtrHandler(new PtrHandler() {             @Override             public void onRefreshBegin(PtrFrameLayout frame) {                // currentPage = 1;               //  mPresenter.start(currentPage,type);                 mPtrFrame.refreshComplete();                 if(currentPage==1){                     mDataList.clear();                 }                 mDataList.addAll(entity.getNewslist());                 mAdapter.notifyDataSetChanged();             }               @Override             public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {                 return PtrDefaultHandler.checkContentCanBePulledDown(frame, content, header);             }         });         mPtrFrame.setResistance(1.7f);         mPtrFrame.setRatioOfHeaderHeightToRefresh(1.2f);         mPtrFrame.setDurationToClose(200);         mPtrFrame.setDurationToCloseHeader(1000);         // default is false         mPtrFrame.setPullToRefresh(false);         // default is true         mPtrFrame.setKeepHeaderWhenRefresh(true);         mPtrFrame.postDelayed(new Runnable() {             @Override             public void run() {                 mPtrFrame.autoRefresh();             }         }, 100);  
2、当下拉刷新中有 ViewPager 时,比如 ListView 上面加个 Banner 广告条,这时候其实是会有滑动冲突的,那么网上搜很多都说要重写 ViewPager 或者 PtrFrameLayout,而且大部分都是达不到效果的,其实细心的你可能会发现,PtrFrameLayout 已经提供好方法来解决这一问题了,只是我们平时比较容易忽略:Q&A好了,只需要这样就能解决冲突了:
ptrFrame.disableWhenHorizontalMove(true);
文/L_Xian(简书作者)原文链接:http://www.jianshu.com/p/edb2cde8201a著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: