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

来源:互联网 发布:5g网络龙头股票 编辑:程序博客网 时间:2024/06/05 04:22

使用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之后再来补上啦。下班了啦啦啦啦




0 0
原创粉丝点击