Android 下拉刷新SwipeRefreshLayout的setColorSchemeColors没效果的原因

前言

设置下拉刷新进度条颜色的时候 发现SwipeRefreshLayoutsetColorSchemeColors一直没有效果
在寻找问题的过程中看到了源码 如下:

源码分析

 /**
     * @deprecated Use {@link #setColorSchemeResources(int...)}
     */
    @Deprecated
    public void setColorScheme(@ColorRes int... colors) {
        setColorSchemeResources(colors);
    }

    /**
     * Set the color resources used in the progress animation from color resources.
     * The first color will also be the color of the bar that grows in response
     * to a user swipe gesture.
     *
     * @param colorResIds
     */
    public void setColorSchemeResources(@ColorRes int... colorResIds) {
        final Context context = getContext();
        int[] colorRes = new int[colorResIds.length];
        for (int i = 0; i < colorResIds.length; i++) {
            colorRes[i] = ContextCompat.getColor(context, colorResIds[i]);
        }
        setColorSchemeColors(colorRes);
    }

    /**
     * Set the colors used in the progress animation. The first
     * color will also be the color of the bar that grows in response to a user
     * swipe gesture.
     *
     * @param colors
     */
    public void setColorSchemeColors(@ColorInt int... colors) {
        ensureTarget();
        mProgress.setColorSchemeColors(colors);
    }

从源码中 我们可以看到:

  • 第一个方法setColorScheme()这个方法已经过期 不赞成使用!
  • 第二个方法setColorSchemeResources(@ColorRes int... colorResIds)需要传一个颜色资源,也就是我们常用的values资源文件夹里的color文件
    例如:
<resources>
    <color name="colorPrimary">#FF18C35E</color>
    <color name="colorPrimaryDark">#FF18C35E</color>
    <color name="colorAccent">#FF18C35E</color>
    <color name="colorDividingLine">#e1e1e1</color>
</resources>
  • 第三个方法setColorSchemeColors(@ColorInt int... colors)需要传一个颜色值,也就是#FF18C35E这种

正确使用姿势:

//初始化
SwipeRefreshLayout mSwipeRefreshLayout = findViewById(R.id.swiperefresh);
//在onCreate方法设置setColorSchemeResources()
mSwipeRefreshLayout.setColorSchemeResources(R.color.colorAccent,R.color.colorPrimaryDark);
//在onCreate方法设置setColorSchemeColors()
mSwipeRefreshLayout.setColorSchemeColors(Color.parseColor("#FF18C35E"),Color.parseColor("#e1e1e1"));

总结:如果使用setColorSchemeColors()方法,参数传的是颜色值,用setColorSchemeResources()参数传资源id。而setColorSchemeResources()其实也是先通过资源id得到颜色值,最后调用setColorSchemeColors()方法。

设置完成后的效果图:

这里写图片描述

与君共勉

我要一步一步往上爬
在最高点乘着叶片往前飞
任风吹干流过的泪和汗
我要一步一步往上爬
等待阳光静静看着它的脸
小小的天有大大的梦想
我有属于我的天
任风吹干流过的泪和汗
总有一天我有属于我的天
这里写图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

吕氏春秋i

你的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值