一个可以进页面自动显示刷新效果的SwipeRefreshLayout

SwipeRefreshLayout出来已经有一段时间了,先后换过两种刷新效果,都在V4包下面,新的刷新效果还是很赞的,很多app都采用了这种刷新效果,我最近也在往这边靠,在研究的时候发现,原始的SwipeRefreshLayout只支持手势下拉才能出现刷新效果,看到《简书》安卓客户端每次都有那种切换页面就自动出来刷新效果,自己也试了下,直接设置setRefreshing(true)这个方法是不能看到效果的,于是对源码进行了改造,不说多了,直接上代码

public class AutoSwipeRefreshLayout extends SwipeRefreshLayout {

    public AutoSwipeRefreshLayout(Context context) {
        super(context);
    }

    public AutoSwipeRefreshLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    /**
     * 自动刷新
     */
    public void autoRefresh() {
        try {
            Field mCircleView = SwipeRefreshLayout.class.getDeclaredField("mCircleView");
            mCircleView.setAccessible(true);
            View progress = (View) mCircleView.get(this);
            progress.setVisibility(VISIBLE);

            Method setRefreshing = SwipeRefreshLayout.class.getDeclaredMethod("setRefreshing", boolean.class, boolean.class);
            setRefreshing.setAccessible(true);
            setRefreshing.invoke(this, true, true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

代码看起来比较简单,确实花了不少功夫去阅读源码,除了添加了一个可以自动刷新的方法,没有修改其他的,用法也是和普通SwipeRefreshLayout一样,只不过可以在第一次加载数据的时候调用autoRefresh()就可以看到刷新效果了,看效果图:
这里写图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值