【用户界面:android-Ultra-Pull-To-Refresh】之一:android-Ultra-Pull-To-Refresh开篇

作者:郭孝星
微博:郭孝星的新浪微博
邮箱:allenwells@163.com
博客:http://blog.csdn.net/allenwells
github:https://github.com/AllenWell

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

一 功能介绍

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

  • 继承于 ViewGroup, Content可以包含任何View。
  • 简洁完善的 Header 抽象,方便进行拓展,构建符合需求的头部。

二 功能展示

技术分享

技术分享

技术分享

技术分享

技术分享

技术分享

三 使用方法

(1) 添加依赖模块

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

(2) 定义PtrFrameLayout

定义PtrFrameLayout有两种方式:

  • 在布局文章中定义
  • 在代码中定义

在布局文章中定义

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

在代码中定义

// 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文件,如下所示:

<resources>
    <string-array name="storehouse">
        <item>0,35,12,42,</item>
        <item>12,42,24,35,</item>
        <item>24,35,12,28,</item>
        <item>0,35,12,28,</item>
        <item>0,21,12,28,</item>
        <item>12,28,24,21,</item>
        <item>24,35,24,21,</item>
        <item>24,21,12,14,</item>
        <item>0,21,12,14,</item>
        <item>0,21,0,7,</item>
        <item>12,14,0,7,</item>
        <item>12,14,24,7,</item>
        <item>24,7,12,0,</item>
        <item>0,7,12,0,</item>
    </string-array>
</resources>

(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.
     * <p/>
     * {@link in.srain.cube.views.ptr.PtrDefaultHandler#checkContentCanBePulledDown}
     */
    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的框架分析和源码分析。

版权声明:本文为博主原创文章,未经博主允许不得转载。

文章来自:http://blog.csdn.net/allenwells/article/details/47038391
© 2021 jiaocheng.bubufx.com  联系我们
ICP备案:鲁ICP备09046678号-3