問(wèn)題:
因?yàn)槭褂昧藛袅▎袅ǖ闹黝}框架,所以需要?jiǎng)討B(tài)修改RecyclerView的scrollbar顏色粥脚,但是scrollbar是view的私有內(nèi)部類脊岳,只能在xml中配置。如下圖
解決方法:
查看源碼顿乒,通過(guò)反射獲取scrollbar的drawable议街,然后用框架著色
public static void tintScrollbarColor(Context context, RecyclerView recyclerView) {
try {
//通過(guò)反射一層層的獲取對(duì)象,直到獲取到進(jìn)度條
Class<View> clazz = View.class;
Method method = clazz.getDeclaredMethod("getScrollCache");
method.setAccessible(true);
Object scrollabilityCacheObj = method.invoke(recyclerView);
Class<?> scrollabilityCacheClazz = scrollabilityCacheObj.getClass();
Field scrollBarField = scrollabilityCacheClazz.getField("scrollBar");
Object scrollbarObj = scrollBarField.get(scrollabilityCacheObj);
Class<?> scrollbarClass = scrollbarObj.getClass();
Field mVerticalThumbField = scrollbarClass.getDeclaredField("mVerticalThumb");
Field mHorizontalThumbField = scrollbarClass.getDeclaredField("mHorizontalThumb");
mVerticalThumbField.setAccessible(true);
mHorizontalThumbField.setAccessible(true);
Drawable mVerticalThumb = (Drawable) mVerticalThumbField.get(scrollbarObj);
Drawable mHorizontalThumb = (Drawable) mHorizontalThumbField.get(scrollbarObj);
//對(duì)顯示的drawable著色
Drawable mVerticalThumbTint = ThemeUtils.tintDrawable(mVerticalThumb,
ThemeUtils.getColorById(context, R.color.theme_color_primary));
Drawable mHorizontalThumbTint = ThemeUtils.tintDrawable(mHorizontalThumb,
ThemeUtils.getColorById(context, R.color.theme_color_primary));
//設(shè)置新的drawable
mVerticalThumbField.set(scrollbarObj, mVerticalThumbTint);
mHorizontalThumbField.set(scrollbarObj, mHorizontalThumbTint);
} catch (Exception e) {
e.printStackTrace();
}
}