SwipeRefreshLayout进入页面自动调用onRefresh函数

使用SwipeRefreshLayout控件有一段儿时间了,之前看到网易云音乐中一进入页面就自动调用SwipeRefreshLayout的刷新函数动画,自己也想在项目中加上这个效果,省的还要用一个Dialog来提示进度了。小巧又可爱,不是么。

找了些资料,发现其实只要写一个方法,利用反射来调用就刷新功能就可以了。

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);//这里是为了获取刷新函数,这里设置为true,就可以
            setRefreshing.setAccessible(true);
            setRefreshing.invoke(this, true, true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

接下来使用方法就简单了,在使用的地方调用一下autoRefresh方法就可以了

demo之后再来补上啦。下班了啦啦啦啦




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值