Skip to content

Commit 91a796a

Browse files
committed
[Fix] Work around GradientDrawable missing tinting implementation on Lollipop.
1 parent 0345944 commit 91a796a

11 files changed

Lines changed: 59 additions & 10 deletions

File tree

library/src/main/java/me/zhanghai/android/fastscroll/FastScrollerBuilder.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626

2727
import androidx.annotation.NonNull;
2828
import androidx.annotation.Nullable;
29-
import androidx.appcompat.content.res.AppCompatResources;
3029
import androidx.core.util.Consumer;
3130
import androidx.core.widget.NestedScrollView;
3231
import androidx.recyclerview.widget.RecyclerView;
@@ -117,17 +116,21 @@ public FastScrollerBuilder setPopupStyle(@NonNull Consumer<TextView> popupStyle)
117116
@NonNull
118117
public FastScrollerBuilder useDefaultStyle() {
119118
Context context = mView.getContext();
120-
mTrackDrawable = AppCompatResources.getDrawable(context, R.drawable.afs_track);
121-
mThumbDrawable = AppCompatResources.getDrawable(context, R.drawable.afs_thumb);
119+
mTrackDrawable = Utils.getGradientDrawableWithTintAttr(R.drawable.afs_track,
120+
R.attr.colorControlNormal, context);
121+
mThumbDrawable = Utils.getGradientDrawableWithTintAttr(R.drawable.afs_thumb,
122+
R.attr.colorControlActivated, context);
122123
mPopupStyle = PopupStyles.DEFAULT;
123124
return this;
124125
}
125126

126127
@NonNull
127128
public FastScrollerBuilder useMd2Style() {
128129
Context context = mView.getContext();
129-
mTrackDrawable = AppCompatResources.getDrawable(context, R.drawable.afs_md2_track);
130-
mThumbDrawable = AppCompatResources.getDrawable(context, R.drawable.afs_md2_thumb);
130+
mTrackDrawable = Utils.getGradientDrawableWithTintAttr(R.drawable.afs_md2_track,
131+
R.attr.colorControlNormal, context);
132+
mThumbDrawable = Utils.getGradientDrawableWithTintAttr(R.drawable.afs_md2_thumb,
133+
R.attr.colorControlActivated, context);
131134
mPopupStyle = PopupStyles.MD2;
132135
return this;
133136
}

library/src/main/java/me/zhanghai/android/fastscroll/PopupStyles.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import android.widget.FrameLayout;
2525
import android.widget.TextView;
2626

27-
import androidx.appcompat.content.res.AppCompatResources;
2827
import androidx.core.util.Consumer;
2928

3029
public class PopupStyles {
@@ -42,8 +41,8 @@ private PopupStyles() {}
4241
layoutParams.setMarginEnd(resources.getDimensionPixelOffset(R.dimen.afs_popup_margin_end));
4342
popupView.setLayoutParams(layoutParams);
4443
Context context = popupView.getContext();
45-
popupView.setBackground(new AutoMirrorDrawable(AppCompatResources.getDrawable(context,
46-
R.drawable.afs_popup_background)));
44+
popupView.setBackground(new AutoMirrorDrawable(Utils.getGradientDrawableWithTintAttr(
45+
R.drawable.afs_popup_background, R.attr.colorControlActivated, context)));
4746
popupView.setEllipsize(TextUtils.TruncateAt.MIDDLE);
4847
popupView.setGravity(Gravity.CENTER);
4948
popupView.setIncludeFontPadding(false);

library/src/main/java/me/zhanghai/android/fastscroll/Utils.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,17 @@
1919
import android.content.Context;
2020
import android.content.res.ColorStateList;
2121
import android.content.res.TypedArray;
22+
import android.graphics.drawable.Drawable;
23+
import android.graphics.drawable.GradientDrawable;
24+
import android.os.Build;
2225

2326
import androidx.annotation.AttrRes;
2427
import androidx.annotation.ColorInt;
28+
import androidx.annotation.DrawableRes;
2529
import androidx.annotation.NonNull;
2630
import androidx.annotation.Nullable;
2731
import androidx.appcompat.content.res.AppCompatResources;
32+
import androidx.core.graphics.drawable.DrawableCompat;
2833

2934
class Utils {
3035

@@ -49,4 +54,19 @@ public static ColorStateList getColorStateListFromAttrRes(@AttrRes int attrRes,
4954
a.recycle();
5055
}
5156
}
57+
58+
// Work around the bug that GradientDrawable didn't actually implement tinting until
59+
// Lollipop MR1 (API 22).
60+
@Nullable
61+
public static Drawable getGradientDrawableWithTintAttr(@DrawableRes int drawableRes,
62+
@AttrRes int tintAttrRes,
63+
@NonNull Context context) {
64+
Drawable drawable = AppCompatResources.getDrawable(context, drawableRes);
65+
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP_MR1
66+
&& drawable instanceof GradientDrawable) {
67+
drawable = DrawableCompat.wrap(drawable);
68+
drawable.setTintList(getColorStateListFromAttrRes(tintAttrRes, context));
69+
}
70+
return drawable;
71+
}
5272
}

library/src/main/res/drawable/afs_md2_thumb.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
~ limitations under the License.
1717
-->
1818

19+
<!--
20+
~ Any changes to the tint here needs to be mirrored in the call to
21+
~ Utils.getGradientDrawableWithTintAttr() as well.
22+
-->
1923
<shape
2024
xmlns:android="http://schemas.android.com/apk/res/android"
2125
android:shape="rectangle"

library/src/main/res/drawable/afs_md2_track.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
~ limitations under the License.
1717
-->
1818

19+
<!--
20+
~ Any changes to the tint here needs to be mirrored in the call to
21+
~ Utils.getGradientDrawableWithTintAttr() as well.
22+
-->
1923
<shape
2024
xmlns:android="http://schemas.android.com/apk/res/android"
2125
android:shape="rectangle"

library/src/main/res/drawable/afs_popup_background.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
~ limitations under the License.
1717
-->
1818

19+
<!--
20+
~ Any changes to the tint here needs to be mirrored in the call to
21+
~ Utils.getGradientDrawableWithTintAttr() as well.
22+
-->
1923
<shape
2024
xmlns:android="http://schemas.android.com/apk/res/android"
2125
android:shape="rectangle"

library/src/main/res/drawable/afs_thumb.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
~ limitations under the License.
1717
-->
1818

19+
<!--
20+
~ Any changes to the tint here needs to be mirrored in the call to
21+
~ Utils.getGradientDrawableWithTintAttr() as well.
22+
-->
1923
<shape
2024
xmlns:android="http://schemas.android.com/apk/res/android"
2125
android:shape="rectangle"

library/src/main/res/drawable/afs_thumb_stateful.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616
~ limitations under the License.
1717
-->
1818

19+
<!--
20+
~ It was later discovered that this XML drawable doesn't work on Lollipop (API 21) due to the bug
21+
~ that GradientDrawable didn't actually implement tinting until Lollipop MR1 (API 22). So if you
22+
~ need to support API 21, you'll need to work around it yourself.
23+
-->
1924
<selector xmlns:android="http://schemas.android.com/apk/res/android">
2025

2126
<item android:state_pressed="true">

library/src/main/res/drawable/afs_track.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
~ limitations under the License.
1717
-->
1818

19+
<!--
20+
~ Any changes to the tint here needs to be mirrored in the call to
21+
~ Utils.getGradientDrawableWithTintAttr() as well.
22+
-->
1923
<shape
2024
xmlns:android="http://schemas.android.com/apk/res/android"
2125
android:shape="rectangle"

sample/src/main/java/me/zhanghai/android/fastscroll/sample/RecyclerViewListFragment.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
package me.zhanghai.android.fastscroll.sample;
1818

1919
import androidx.annotation.NonNull;
20-
import androidx.recyclerview.widget.LinearLayoutManager;
21-
import androidx.recyclerview.widget.RecyclerView;
2220

2321
public class RecyclerViewListFragment extends RecyclerViewFragment {
2422

0 commit comments

Comments
 (0)