android-ultra-pull-to-refresh 简单使用,【用户界面:android-Ultra-Pull-To-Refresh】之一:android-Ultra-Pull-To-Refr...

本篇文章是【用户界面:android-Ultra-Pull-To-Refresh】的开篇文章,主要介绍和展示android-Ultra-Pull-To-Refresh的相关功能,并介绍在项目中集成该框架的相关方法。

一 功能介绍

android-Ultra-Pull-To-Refresh提供了一个下拉刷新的技术框架。它的主要功能如下所示:

继承于 ViewGroup, Content可以包含任何View。

简洁完善的 Header 抽象,方便进行拓展,构建符合需求的头部。

二 功能展示

e9e4372071d85554c4e6078e80d82c16.gif

013f5c2a745861009241eec546d277f3.gif

e3358b427f318cc96fa15113c9d5759c.gif

127ace051f00509d52cd81af524114e8.gif

a58060512644176055ea409a00a9f329.gif

c77d36a37116d53a71596cf5eb68ffad.gif

三 使用方法

(1) 添加依赖模块

compile ‘in.srain.cube:ultra-ptr:1.0.10‘

(2) 定义PtrFrameLayout

定义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" >

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">

android:id="@+id/store_house_ptr_image"

android:layout_width="match_parent"

android:layout_height="match_parent" />

在代码中定义

// 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);

(3) 初始化StoreHouseHeader

初始化StoreHouseHeader有两种方式:

使用字符串进行初始化

使用字符串数组初始化

使用字符串进行初始化

// header

final StoreHouseHeader header = new StoreHouseHeader(getContext());

header.setPadding(0, LocalDisplay.dp2px(15), 0, 0);

/**

* using a string, support: A-Z 0-9 - .

* you can add more letters by {@link in.srain.cube.views.ptr.header.StoreHousePath#addChar}

*/

header.initWithString(‘Alibaba‘);

使用字符串数组初始化

header.initWithStringArray(R.array.storehouse);

R.array.storehouse是xml文件,如下所示:

0,35,12,42,

12,42,24,35,

24,35,12,28,

0,35,12,28,

0,21,12,28,

12,28,24,21,

24,35,24,21,

24,21,12,14,

0,21,12,14,

0,21,0,7,

12,14,0,7,

12,14,24,7,

24,7,12,0,

0,7,12,0,

(4) 处理刷新

我们可以通过注册PtrHandler进行刷新的处理,PtrHandler是一个接口,如下所示:

public interface PtrHandler {

/**

* Check can do refresh or not. For example the content is empty or the first child is in view.

*

* [email protected] [email protected]Down}

*/

public boolean checkCanDoRefresh(final PtrFrameLayout frame, final View content, final View header);

/**

* When refresh begin

*

*@param frame

*/

public void onRefreshBegin(final PtrFrameLayout frame);

}

注册PtrHandler,如下所示:

ptrFrame.setPtrHandler(new PtrHandler() {

@Override

public void onRefreshBegin(PtrFrameLayout frame) {

frame.postDelayed(new Runnable() {

@Override

public void run() {

ptrFrame.refreshComplete();

}

}, 1800);

}

@Override

public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {

return PtrDefaultHandler.checkContentCanBePulledDown(frame, content, header);

}

});

除此之外,我们还可以通过向PtrFrameLayout实现和注册PtrUIHandler来展现更多的UI效果,如下所示:

public interface PtrUIHandler {

/**

* When the content view has reached top and refresh has been completed, view will be reset.

*

*@param frame

*/

public void onUIReset(PtrFrameLayout frame);

/**

* prepare for loading

*

*@param frame

*/

public void onUIRefreshPrepare(PtrFrameLayout frame);

/**

* perform refreshing UI

*/

public void onUIRefreshBegin(PtrFrameLayout frame);

/**

* perform UI after refresh

*/

public void onUIRefreshComplete(PtrFrameLayout frame);

public void onUIPositionChange(PtrFrameLayout frame, boolean isUnderTouch, byte status, int oldPosition, int currentPosition, float oldPercent, float currentPercent);

}

小技巧

和ViewPager配合使用

disableWhenHorizontalMove();

和LongPressed配合使用

setInterceptEventWhileWorking();

开篇文章就大致介绍这么多,后续的文章会继续做android-Ultra-Pull-To-Refresh的框架分析和源码分析。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值