2012-08-24 71 views
10

我有一個ListView,和一個自定義樣式,基本上看起來像霍洛,但黃色的口音,而不是藍色。爲ListView自定義overscroll發光/邊緣顏色?

當我滾動到列表的底部或頂部時,我得到不合身的藍色。我爲overscroll_glow.pngoverscroll_edge.png製作了自定義繪圖,但我不知道如何使用它們。

我怎樣才能讓我的ListView使用這些可繪製而不是系統的?

OverScroller這個班會有幫助嗎?

回答

4

試過嗎? setOverscrollFooter(Drawable)

更新1

哎呀。沒關係。您可能已經看過this discussion

發光行爲包裹在EdgeEffect class中。您可以通過在您的ListView上調用setOverScrollMode(OVER_SCROLL_NEVER)來禁用默認的邊緣效果,然後滾動您自己的EdgeEffect類(其細節在此處超出我的範圍)。

更新2

哎喲。在AbsListView中創建的EdgeEffect對象上的調用深藏在滾動邏輯中。可能會很艱難。一個簡單的解決方法是向AbsListView中的setEdgeEffect(EdgeEffect)方法發送一個針對Android團隊的功能請求...

+1

試過了頁腳,不是我所需要的。 所以我做這件事的唯一方法是創建一個自定義的'AbsListView'類?太喜歡我喜歡:( –

+0

嗯,我現在確信,它可以通過繼承ListView和重寫一堆方法('onOverScrolled()'是重要的)),但我不知道它是否值得它:( – heycosmo

8

可以使用反射式的解決方案,以自己的方式破解改變這種發光顏色:

int glowDrawableId = context.getResources().getIdentifier("overscroll_glow", "drawable", "android"); 
Drawable androidGlow = context.getResources().getDrawable(glowDrawableId); 
androidGlow.setColorFilter(brandColor, PorterDuff.Mode.MULTIPLY); 

我遇到同樣的問題,這似乎是一個很好的解決方案,至少直到Google添加了一個用於更改顏色(或品牌顏色)的API:http://evendanan.net/android/branding/2013/12/09/branding-edge-effect/

+2

乘法不好,因爲它會將你的顏色與默認的藍色混合在一起,你應該使用SrcAtop,它將完全替換原來的顏色 –

+0

對於ScrollView而言,這個代碼在哪裏調用? – chochim

+0

Hey Menny感謝您的帖子我似乎無法得到這個工作我有上下文設置像這樣的'Context context = this;' – iqueqiorio

0

我添加了此答案,因爲這是該主題的頂級結果之一,並且頂級答案不再正確。 那麼,它實際上是棒棒糖之前的版本,但如預期,當您使用Java反射你得到咬傷回來了,這是發生了什麼,如果你使用的代碼

int glowDrawableId = context.getResources().getIdentifier("overscroll_glow", "drawable", "android"); 
Drawable androidGlow = context.getResources().getDrawable(glowDrawableId); 
androidGlow.setColorFilter(context.getResources().getColor(R.color.gm_color), PorterDuff.Mode.MULTIPLY); 

這將在棒棒堂+設備崩潰,因爲該資源不再存在。所以對於android 5。+你應該使用:

 <item name="android:colorEdgeEffect">@color/custom_color</item> 

在你的主題,在values-v21/themes.xml文件重寫,如果你不分鐘21版

3

這裏針對你的解決方案:

public static void ChangeEdgeEffect(Context cxt, View list, int color){ 

    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 

     EdgeEffect edgeEffectTop = new EdgeEffect(cxt); 
     edgeEffectTop.setColor(color); 
     EdgeEffect edgeEffectBottom = new EdgeEffect(cxt); 
     edgeEffectBottom.setColor(color); 

     try { 
      Field f1 = AbsListView.class.getDeclaredField("mEdgeGlowTop"); 
      f1.setAccessible(true); 
      f1.set(list, edgeEffectTop); 

      Field f2 = AbsListView.class.getDeclaredField("mEdgeGlowBottom"); 
      f2.setAccessible(true); 
      f2.set(list, edgeEffectBottom); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

    }else{ 
     int glowDrawableId = cxt.getResources().getIdentifier("overscroll_glow", "drawable", "android"); 
     Drawable androidGlow = cxt.getResources().getDrawable(glowDrawableId); 
     assert androidGlow != null; 
     androidGlow.setColorFilter(cxt.getResources().getColor(color), PorterDuff.Mode.SRC_ATOP); 
    } 
} 
+0

此方法運行良好,謝謝。 – BArtWell

3

請不要忘記改變overscroll邊緣線:

 final int edgeDrawableId = res.getIdentifier("overscroll_edge", "drawable", "android"); 
     final Drawable overscrollEdge = res.getDrawable(edgeDrawableId); 
     overscrollEdge.setColorFilter(res.getColor(colorID), android.graphics.PorterDuff.Mode.SRC_ATOP);