炫酷的列表下拉刷新效果

3,239 阅读1分钟

下拉刷新是一个很常见的功能,现在很多app提供的下拉刷新效果都千篇一律,就像Google为我们提供的SwipeRefreshlayout,看多了是不是觉得有些厌倦了?如果在用户进行下拉等待的时候,给他们提供一些有创造力的效果,绝对会给用户带来不一样的体验。今天就为大家介绍一个烟花下拉刷新效果-FireworkyPullToRefresh

Talk is cheap ,show me the gif:

image.png

demo_.gif

效果是不是很爆炸?看一下如何将这个炫酷的下拉刷新效果使用到我们的应用中:

build.gradle中添加依赖:

 compile 'com.cleveroad:fireworkypulltorefresh:1.0.3'

在xml文件中,RecyclerView的父容器使用FireworkyPullToRefresh:

<com.cleveroad.pulltorefresh.firework.FireworkyPullToRefreshLayout
        android:id="@+id/pullToRefresh"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

</com.cleveroad.ptr.FireworkyPullToRefreshLayout>

FireworkyPullToRefresh提供了如下一些可以配置的属性:

  • ptr_fireworkColors: 设置烟花的颜色

  • ptr_background: 头部下来部分的背景图片

  • ptr_rocketAnimDuration:烟花发射的动画时间

  • ptr_fireworkStyle: 设置烟花的模式classic or modern

以上四个配置可以直接在xml中使用:

<com.cleveroad.pulltorefresh.firework.FireworkyPullToRefreshLayout
        xmlns:app="http://schemas.android.com/apk/res-auto"
        ...
        app:ptr_fireworkColors="@array/fireworkColors"
        app:ptr_background="@drawable/background"
        app:ptr_rocketAnimDuration="1000">

也可以在代码中设置:

//use .config() methods:

mPullToRefresh.getConfig().setBackground(backgroundDrawable);
mPullToRefresh.getConfig().setBackground(backgroundBitmap);
mPullToRefresh.getConfig().setBackground(R.drawable.background);
mPullToRefresh.getConfig().setBackgroundColor(Color.BLACK);
mPullToRefresh.getConfig().setBackgroundColorFromResources(R.color.background);

mPullToRefresh.getConfig().setFireworkColors(colorsIntArray);
mPullToRefresh.getConfig().setFireworkColors(R.array.fireworkColors);

mPullRefreshView.getConfig().setFireworkStyle(Configuration.FireworkStyle.MODERN);

mPullToRefresh.getConfig().setRocketAnimDuration(1000L);
刷新回调

用法跟SwipeRefreshlayout很相似,通过实现接口PullToRefreshView.OnRefreshListener来添加刷新逻辑:

mPullToRefresh.setOnRefreshListener(new PullToRefreshView.OnRefreshListener() {
    @Override
    public void onRefresh() {
        //refresh your data here        
    }
});

开始或者取消动画:

mPullRefreshView.setRefreshing(isRefreshing);
自定义动画

如果你想要自己实现动画,也是可以的,重写FireworkyPullToRefreshLayout.OnChildScrollUpCallback来实现你自己的动画逻辑

mPullToRefresh.setOnChildScrollUpCallback(new FireworkyPullToRefreshLayout.OnChildScrollUpCallback() {
    @Override
    public boolean canChildScrollUp(@NonNull FireworkyPullToRefreshLayout parent, @Nullable View child) {
        //put your implementation here
    }
});

具体的实现细节可以查源码,github:github.com/Cleveroad/F…