How to change the color of overscroll edge and overscroll glow

前端 未结 4 1639
小鲜肉
小鲜肉 2020-12-14 14:55

How to Change color of over-scroll edge and over-scroll glow or how to change the white color (default color) of android release 5.0 lollipop?

4条回答
  •  情话喂你
    2020-12-14 15:16

    On LOLLIPOP the edge glow inherits from colorPrimary. After the view is created the edge glow color can only be changed through reflection. This can be useful when you load colors dynamically using Palette.

    EDIT: TL;DR: Download the whole class from here: https://github.com/consp1racy/android-commons/blob/71b5c65689786b1d52d701d81d8c7445495807c3/commons/src/main/java/net/xpece/android/widget/XpEdgeEffect.java

    PROGUARD SETUP: If you're going to use this on widgets from support library you need to keep the field names. Quickest way to do it is the following (although still wasteful):

    -keepclassmembers class android.support.v4.widget.EdgeEffectCompat {
        ;    
    }
    

    Create a utility class with the following code:

    private static final Class CLASS_SCROLL_VIEW = ScrollView.class;
    private static final Field SCROLL_VIEW_FIELD_EDGE_GLOW_TOP;
    private static final Field SCROLL_VIEW_FIELD_EDGE_GLOW_BOTTOM;
    
    private static final Class CLASS_LIST_VIEW = AbsListView.class;
    private static final Field LIST_VIEW_FIELD_EDGE_GLOW_TOP;
    private static final Field LIST_VIEW_FIELD_EDGE_GLOW_BOTTOM;
    
    static {
      Field edgeGlowTop = null, edgeGlowBottom = 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;
    }
    
    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public static void setEdgeGlowColor(AbsListView listView, int color) {
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        try {
          EdgeEffect ee;
          ee = (EdgeEffect) LIST_VIEW_FIELD_EDGE_GLOW_TOP.get(listView);
          ee.setColor(color);
          ee = (EdgeEffect) LIST_VIEW_FIELD_EDGE_GLOW_BOTTOM.get(listView);
          ee.setColor(color);
        } catch (Exception ex) {
          ex.printStackTrace();
        }
      }
    }
    
    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public static void setEdgeGlowColor(ScrollView scrollView, int color) {
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        try {
          EdgeEffect ee;
          ee = (EdgeEffect) SCROLL_VIEW_FIELD_EDGE_GLOW_TOP.get(scrollView);
          ee.setColor(color);
          ee = (EdgeEffect) SCROLL_VIEW_FIELD_EDGE_GLOW_BOTTOM.get(scrollView);
          ee.setColor(color);
        } catch (Exception ex) {
          ex.printStackTrace();
        }
      }
    }
    

提交回复
热议问题