Android下拉刷新SwipeRefreshLayout简单用法

之前一直都想用下拉刷新,感觉上是庞大的工程,所以搁置了。现在学习了一下其实真的超级简单。

看了《第一行代码》以及 https://www.jianshu.com/p/3c402a9e4b7d文章

看上去是真的简单。SwipeRefreshLayout下嵌套一个控件

1.布局代码
<android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/mswipeRefreshLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <WebView
            android:id="@+id/mwebview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            >
        </WebView>
</android.support.v4.widget.SwipeRefreshLayout>

混编的app,这里嵌套了一个webview

2.Activity代码
mSwipe =  findViewById(R.id.mswipeRefreshLayout);

private void setSwipe() {
        /*
        * 设置下拉刷新球颜色
        */
        mSwipe.setColorSchemeResources(R.color.swipeColor1,R.color.swipeColor2,R.color.swipeColor3,R.color.swipeColor4,R.color.swipeColor5);
        /*
        * 设置下拉刷新的监听
        */
        mSwipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                //刷新需执行的操作
            }
        });
}

 我们可以在res/values/colors.xml文件中设置我们想要的颜色

<color name="swipeColor1">#3F51B5</color>
<color name="swipeColor2">#000000</color>
......

加载完毕后需要关闭下拉刷新球

mSwipe.setRefreshing(false);

3.打开页面自动刷新
mSwipe.measure(0,0);
mSwipe.setRefreshing(true);

在onMeasure之前 单独使用setRefreshing(true)是没有效果的

 

还可以复写onWindowFocusChanged当页面获得焦点的时刷新

 @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        mSwipe.setRefreshing(true);
    }

 

也可以放到ui线程消息循环中排队

mSwipe.post(new Runnable() {

            @Override
            public void run() {
                mSwipe.setRefreshing(true);
            }
        });

 

在刷新结束后关闭刷新球

public void SwipeIsFinish(){
        if (mSwipe.isRefreshing()) {
            mSwipe.setRefreshing(false);
        }
   }

 

转载于:https://www.cnblogs.com/Steffen-xuan/p/11196474.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值