码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-08-19 20:48:38      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:refresh   swipe   刷新   自动刷新   

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()就可以看到刷新效果了,看效果图:
技术分享

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

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

标签:refresh   swipe   刷新   自动刷新   

原文地址:http://blog.csdn.net/djk_dong/article/details/47782871

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!