How to add a scrollview edge color effect in Android Lollipop?

后端 未结 6 668
说谎
说谎 2020-12-13 05:32

In my app I change the overscroll glow effect color like this:

int glowDrawableId = contexto.getResources().getIdentifier(\"overscroll_glow\", \"drawable\",          


        
6条回答
  •  眼角桃花
    2020-12-13 06:05

    The "android:colorEdgeEffect" solution works perfectly, and is much better than the previous hacks. However, it cannot be used if the edge color needs to be changed prorgrammatically.

    It is possible, though, to use reflection to do so, setting the EdgeEffect objects directly in the AbsListView or ScrollView instances. For example:

    EdgeEffect edgeEffectTop = new EdgeEffect(this);
    edgeEffectTop.setColor(Color.RED);
    
    EdgeEffect edgeEffectBottom = new EdgeEffect(this);
    edgeEffectBottom.setColor(Color.RED);
    
    try {
        Field f1 = AbsListView.class.getDeclaredField("mEdgeGlowTop");
        f1.setAccessible(true);
        f1.set(listView, edgeEffectTop);
    
        Field f2 = AbsListView.class.getDeclaredField("mEdgeGlowBottom");
        f2.setAccessible(true);
        f2.set(listView, edgeEffectBottom);
    } catch (Exception e) {
        e.printStackTrace();
    }
    

    EdgeEffect.setColor() was added in Lollipop.

    Same caveats as any reflection-based solution, though.

提交回复
热议问题