使用 appcompat 时设置回收器视图边缘发光前棒棒糖



我正在寻找一种方法,在使用 appcompat 材料主题时,在回收器视图中设置滚动指示器颜色的样式。

在内部,它使用EdgeEffect设置为内部可样式属性,除非您已经在棒棒糖上(具有讽刺意味),否则无法设置该属性。

使用反射不起作用,设置边缘效果的颜色也只能在棒棒糖上。

在我的API21应用程序上,它从主要材料颜色中提取,在Kitkat上它是白色的,在此之前它是全息蓝色,我希望统一我的设计。

关于它是如何完成的任何想法?

使用以下命令设置边缘效果发光颜色。适用于所有支持 EdgeEffect (API 14+) 的平台版本,否则将静默失败。

void themeRecyclerView(Context context, RecyclerView recyclerView) {
    int yourColor = Color.parseColor("#your_color");
    try {
        final Class<?> clazz = RecyclerView.class;
        for (final String name : new String[]{"ensureTopGlow", "ensureBottomGlow", "ensureLeftGlow", "ensureRightGlow"}) {
            Method method = clazz.getDeclaredMethod(name);
            method.setAccessible(true);
            method.invoke(recyclerView);
        }
        for (final String name : new String[]{"mTopGlow", "mBottomGlow", "mRightGlow", "mLeftGlow"}) {
            final Field field = clazz.getDeclaredField(name);
            field.setAccessible(true);
            final Object edge = field.get(recyclerView);
            final Field fEdgeEffect = edge.getClass().getDeclaredField("mEdgeEffect");
            fEdgeEffect.setAccessible(true);
            setEdgeEffectColor((EdgeEffect) fEdgeEffect.get(edge), yourColor);
        }
    } catch (final Exception | NoClassDefFoundError ignored) {
    }
}
void setEdgeEffectColor(EdgeEffect edgeEffect, int color) {
    try {
        if (Build.VERSION.SDK_INT >= 21) {
            edgeEffect.setColor(color);
            return;
        }
        for(String name : new String[]{"mEdge", "mGlow"}){
            final Field field = EdgeEffect.class.getDeclaredField(name);
            field.setAccessible(true);
            final Drawable drawable = (Drawable) field.get(edgeEffect);
            drawable.setColorFilter(color, PorterDuff.Mode.SRC_IN);
            drawable.setCallback(null);
        }
    } catch (final Exception | NoClassDefFoundError ignored) {
    }
}

感谢@Lukas诺瓦克提供了大部分代码。

正如 Lukas 所说,这些方法必须从RecyclerViewonScrollListener中调用:

recyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
      @Override
      public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
           super.onScrollStateChanged(recyclerView, newState);
           EdgeChanger.setEdgeGlowColor(recycler, getResources().getColor(R.color.your_color));
      }
});

感谢林哈特@Tomáš指出这一点。下面的解决方案仅适用于在 API>21 中更改边缘颜色。它可以与AppCompat一起使用,但更改颜色的效果仅在棒棒糖及以上版本中可见。


我找到了一种使用反射来设置颜色的方法。例如,下面是用于更改顶部和底部边缘颜色的代码:

public static void setEdgeGlowColor(final RecyclerView recyclerView, final int color) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        try {
            final Class<?> clazz = RecyclerView.class;
            for (final String name : new String[] {"ensureTopGlow", "ensureBottomGlow"}) {
                Method method = clazz.getDeclaredMethod(name);
                method.setAccessible(true);
                method.invoke(recyclerView);
            }
            for (final String name : new String[] {"mTopGlow", "mBottomGlow"}) {
                final Field field = clazz.getDeclaredField(name);
                field.setAccessible(true);
                final Object edge = field.get(recyclerView); // android.support.v4.widget.EdgeEffectCompat
                final Field fEdgeEffect = edge.getClass().getDeclaredField("mEdgeEffect");
                fEdgeEffect.setAccessible(true);
                ((EdgeEffect) fEdgeEffect.get(edge)).setColor(color);
            }
        } catch (final Exception ignored) {}
    }
}

与具有ListView或ScrollView等其他组件的解决方案不同,这里必须调用包私有方法ensureTopGlowensureBottomGlow等,并在onScrollStateChanged RecyclerView.OnScrollListener方法中调用上面的setEdgeEffectColor(RecyclerView recycler, int color)

例如:

recyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
      @Override
      public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
           super.onScrollStateChanged(recyclerView, newState);
           EdgeChanger.setEdgeGlowColor(recycler, getResources().getColor(R.color.your_color));
      }
});

默认情况下,Android 在开始滚动时调用ensure*Glow方法。在这些方法中,使用默认颜色初始化新EdgeEffect,但前提是尚未初始化。为了防止这种行为,您必须调用ensure*Glow方法,然后更改边缘的颜色,因此将忽略EdgeEffect的后续初始化(如上面的setEdgeGlowColor方法)

EdgeEffect 使用的是可绘制对象,

因此您可以按照本文中所述更改可绘制对象,但它会影响上下文中的所有EdgeEffect类。

基本上它只是关于介绍和调用此方法,但是本文描述了一些陷阱,因此我建议您先阅读它。

static void brandGlowEffect(Context context, int brandColor) {
      //glow
      int glowDrawableId = context.getResources().getIdentifier("overscroll_glow", "drawable", "android");
      Drawable androidGlow = context.getResources().getDrawable(glowDrawableId);
      androidGlow.setColorFilter(brandColor, PorterDuff.Mode.SRC_IN);
      //edge
      int edgeDrawableId = context.getResources().getIdentifier("overscroll_edge", "drawable", "android");
      Drawable androidEdge = context.getResources().getDrawable(edgeDrawableId);
      androidEdge.setColorFilter(brandColor, PorterDuff.Mode.SRC_IN);
}

我写了实用程序类EdgeChanger,它是我之前的文章,@Jared Hummler代码和@Eugen Pechanec代码的混合体。

此实用程序类使用反射来更改边缘发光的颜色

ScrollView, NestedScrollView, ListView, ViewPager and RecyclerView

并且在使用 AppCompat 时与棉花糖、棒棒糖和棒棒糖之前的设备配合使用,因此您无需使用 EdgeEffectOverride 等第三方库或使用不同的布局。

仅当您想在 onCreate() 之后更改边缘发光颜色时才使用此选项,否则您应该使用 setTheme 和具有不同颜色属性 colorPrimary 或 colorEdgeEffect 的不同主题。

public class EdgeChanger {
private static final Class<?> CLASS_SCROLL_VIEW = ScrollView.class;
private static Field SCROLL_VIEW_FIELD_EDGE_GLOW_TOP;
private static Field SCROLL_VIEW_FIELD_EDGE_GLOW_BOTTOM;
private static final Class<?> CLASS_LIST_VIEW = AbsListView.class;
private static Field LIST_VIEW_FIELD_EDGE_GLOW_TOP;
private static Field LIST_VIEW_FIELD_EDGE_GLOW_BOTTOM;
private static final Class<?> CLASS_NESTED_SCROLL_VIEW = NestedScrollView.class;
private static Field NESTED_SCROLL_VIEW_FIELD_EDGE_GLOW_TOP;
private static Field NESTED_SCROLL_VIEW_FIELD_EDGE_GLOW_BOTTOM;
private static Method NESTED_SCROLL_VIEW_METHOD_ENSURE_GLOWS;
private static final Class<?> CLASS_RECYCLER_VIEW = RecyclerView.class;
private static Field RECYCLER_VIEW_FIELD_EDGE_GLOW_TOP;
private static Field RECYCLER_VIEW_FIELD_EDGE_GLOW_BOTTOM;
private static Method RECYCLER_VIEW_METHOD_ENSURE_GLOW_TOP;
private static Method RECYCLER_VIEW_METHOD_ENSURE_GLOW_BOTTOM;
private static final Class<?> CLASS_VIEW_PAGER = ViewPager.class;
private static Field VIEW_PAGER_FIELD_EDGE_GLOW_LEFT;
private static Field VIEW_PAGER_FIELD_EDGE_GLOW_RIGHT;
static {
    Field edgeGlowTop = null, edgeGlowBottom = null;
    Method ensureGlowTop = null, ensureGlowBottom = null;
    for (Field f : CLASS_SCROLL_VIEW.getDeclaredFields()) {
        switch (f.getName()) {
            case "mEdgeGlowTop":
                f.setAccessible(true);
                edgeGlowTop = f;
                break;
            case "mEdgeGlowBottom":
                f.setAccessible(true);
                edgeGlowBottom = f;
                break;
        }
    }
    SCROLL_VIEW_FIELD_EDGE_GLOW_TOP = edgeGlowTop;
    SCROLL_VIEW_FIELD_EDGE_GLOW_BOTTOM = edgeGlowBottom;
    for (Field f : CLASS_LIST_VIEW.getDeclaredFields()) {
        switch (f.getName()) {
            case "mEdgeGlowTop":
                f.setAccessible(true);
                edgeGlowTop = f;
                break;
            case "mEdgeGlowBottom":
                f.setAccessible(true);
                edgeGlowBottom = f;
                break;
        }
    }
    LIST_VIEW_FIELD_EDGE_GLOW_TOP = edgeGlowTop;
    LIST_VIEW_FIELD_EDGE_GLOW_BOTTOM = edgeGlowBottom;
    for (Field f : CLASS_NESTED_SCROLL_VIEW.getDeclaredFields()) {
        switch (f.getName()) {
            case "mEdgeGlowTop":
                f.setAccessible(true);
                edgeGlowTop = f;
                break;
            case "mEdgeGlowBottom":
                f.setAccessible(true);
                edgeGlowBottom = f;
                break;
        }
    }
    for (Method m : CLASS_NESTED_SCROLL_VIEW.getDeclaredMethods()) {
        switch (m.getName()) {
            case "ensureGlows":
                m.setAccessible(true);
                ensureGlowTop = m;
                break;
        }
    }
    NESTED_SCROLL_VIEW_FIELD_EDGE_GLOW_TOP = edgeGlowTop;
    NESTED_SCROLL_VIEW_FIELD_EDGE_GLOW_BOTTOM = edgeGlowBottom;
    NESTED_SCROLL_VIEW_METHOD_ENSURE_GLOWS = ensureGlowTop;
    for (Field f : CLASS_RECYCLER_VIEW.getDeclaredFields()) {
        switch (f.getName()) {
            case "mTopGlow":
                f.setAccessible(true);
                edgeGlowTop = f;
                break;
            case "mBottomGlow":
                f.setAccessible(true);
                edgeGlowBottom = f;
                break;
        }
    }
    for (Method m : CLASS_RECYCLER_VIEW.getDeclaredMethods()) {
        switch (m.getName()) {
            case "ensureTopGlow":
                m.setAccessible(true);
                ensureGlowTop = m;
                break;
            case "ensureBottomGlow":
                m.setAccessible(true);
                ensureGlowBottom = m;
                break;
        }
    }
    RECYCLER_VIEW_FIELD_EDGE_GLOW_TOP = edgeGlowTop;
    RECYCLER_VIEW_FIELD_EDGE_GLOW_BOTTOM = edgeGlowBottom;
    RECYCLER_VIEW_METHOD_ENSURE_GLOW_TOP = ensureGlowTop;
    RECYCLER_VIEW_METHOD_ENSURE_GLOW_BOTTOM = ensureGlowBottom;
    for (Field f : CLASS_VIEW_PAGER.getDeclaredFields()) {
        switch (f.getName()) {
            case "mLeftEdge":
                f.setAccessible(true);
                edgeGlowTop = f;
                break;
            case "mRightEdge":
                f.setAccessible(true);
                edgeGlowBottom = f;
                break;
        }
    }
    VIEW_PAGER_FIELD_EDGE_GLOW_LEFT = edgeGlowTop;
    VIEW_PAGER_FIELD_EDGE_GLOW_RIGHT = edgeGlowBottom;
}
public static void setEdgeGlowColor(AbsListView listView, int color) {
    try {
        setEdgeEffectColor(LIST_VIEW_FIELD_EDGE_GLOW_TOP.get(listView), color);
        setEdgeEffectColor(LIST_VIEW_FIELD_EDGE_GLOW_BOTTOM.get(listView), color);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
public static void setEdgeGlowColor(ScrollView scrollView, int color) {
    try {
        setEdgeEffectColor(SCROLL_VIEW_FIELD_EDGE_GLOW_TOP.get(scrollView), color);
        setEdgeEffectColor(SCROLL_VIEW_FIELD_EDGE_GLOW_BOTTOM.get(scrollView), color);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
public static void setEdgeGlowColor(final ViewPager viewPager, final int color) {
    try {
        setEdgeEffectColor(VIEW_PAGER_FIELD_EDGE_GLOW_LEFT.get(viewPager), color);
        setEdgeEffectColor(VIEW_PAGER_FIELD_EDGE_GLOW_RIGHT.get(viewPager), color);
    } catch (final Exception e) {
        e.printStackTrace();
    }
}
public static void setEdgeGlowColor(NestedScrollView scrollView, int color) {
    try {
        NESTED_SCROLL_VIEW_METHOD_ENSURE_GLOWS.invoke(scrollView);
        setEdgeEffectColor(NESTED_SCROLL_VIEW_FIELD_EDGE_GLOW_TOP.get(scrollView), color);
        setEdgeEffectColor(NESTED_SCROLL_VIEW_FIELD_EDGE_GLOW_BOTTOM.get(scrollView), color);
    } catch (final Exception | NoClassDefFoundError e) {
        e.printStackTrace();
    }
}
public static void setEdgeGlowColor(final RecyclerView recyclerView, final int color) {
    try {
        RECYCLER_VIEW_METHOD_ENSURE_GLOW_TOP.invoke(recyclerView);
        RECYCLER_VIEW_METHOD_ENSURE_GLOW_BOTTOM.invoke(recyclerView);
        setEdgeEffectColor(RECYCLER_VIEW_FIELD_EDGE_GLOW_TOP.get(recyclerView), color);
        setEdgeEffectColor(RECYCLER_VIEW_FIELD_EDGE_GLOW_BOTTOM.get(recyclerView), color);
    } catch (final Exception | NoClassDefFoundError e) {
        e.printStackTrace();
    }
}
private static void setEdgeEffectColor(Object object, int color) {
    try {
        EdgeEffect edgeEffect = null;
        if (object instanceof EdgeEffectCompat) {
            final Field fEdgeEffect = object.getClass().getDeclaredField("mEdgeEffect");
            fEdgeEffect.setAccessible(true);
            edgeEffect = (EdgeEffect) fEdgeEffect.get(object);
        } else if (object instanceof EdgeEffect) {
            edgeEffect = (EdgeEffect) object;
        }
        if (Build.VERSION.SDK_INT >= 21) {
            edgeEffect.setColor(color);
        } else {
            for (String name : new String[] {"mEdge", "mGlow"}) {
                final Field field = EdgeEffect.class.getDeclaredField(name);
                field.setAccessible(true);
                final Drawable drawable = (Drawable) field.get(edgeEffect);
                drawable.setColorFilter(color, PorterDuff.Mode.SRC_IN);
                drawable.setCallback(null);
            }
        }
    } catch (final Exception | NoClassDefFoundError e) {
        e.printStackTrace();
    }
}

}

如果您使用的是 ProGuard,请不要忘记添加以下规则(不要重命名您希望使用反射处理的字段):

-keepnames class android.widget.ScrollView { *; }
-keepnames class android.widget.AbsListView { *; }
-keepnames class android.support.v4.widget.NestedScrollView { *; }
-keepnames class android.support.v7.widget.RecyclerView { *; }
-keepnames class android.support.v4.view.ViewPager { *; }
-keepnames class android.widget.EdgeEffect { *; }
-keepnames class android.support.v4.widget.EdgeEffectCompat { *; }

我也找不到一种方法来设置回收器视图的滚动颜色。

因此,一个可能的解决方案是为 v21 之前和 v21 之后使用不同的布局文件。

  • v21 之前:使用列表视图并通过 https://github.com/AndroidAlliance/EdgeEffectOverride 设置滚动颜色
  • 后 v21:使用回收器视图并通过 colorAccent 设置滚动颜色

缺点是你的代码会很混乱,你需要有两个不同的适配器来回收器视图/列表视图。

最新更新