2010-05-03 80 views

回答

6

你可以設置成style.xml

<style name="Theme_app" parent="@android:style/Theme.Holo.Light"> 
    <item name="android:fastScrollThumbDrawable">@drawable/fastscroll_thumb_holo</item> 

</style> 

比圖像

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_pressed="true" android:drawable="@drawable/fastscroll_thumb_pressed_holo" /> 
    <item android:drawable="@drawable/fastscroll_thumb_default_holo" /> 
</selector> 
0

如果您查看Android 2.2r1(修訂版1)的源代碼,則有一個名爲android.widget.FastScroller的類,該類具有方法useThumbDrawable()

我只是downloaded the source from a blog called MobileBytes它包含它,也許你可以將它導入到你的項目並實現它? (或者升級您的API,以2.2 R1)

0

我使用的是創建一個可繪製XML 但我不知道爲什麼它不工作,所以在網上搜索我發現here一個硬代碼解決方案,我不知道它是否適用於舊的API,但在我的情況是解決了這個問題。注意我正在使用API​​ 18,如目標和帶有API 17的設備來測試。

代碼:

try { 
    Field f = AbsListView.class.getDeclaredField("mFastScroller"); 
    f.setAccessible(true); 
    Object o = f.get(<<your listView here>>); 
    f = f.getType().getDeclaredField("mThumbDrawable"); 
    f.setAccessible(true); 
    Drawable drawable = (Drawable) f.get(o); 
    drawable = getResources().getDrawable(R.drawable.<<your thumb drawable here can be a selector>>); 
    f.set(o, drawable); 
} catch (Exception e) { 
    e.printStackTrace(); 
} 
1
爲Android奇巧

硬編碼(場 「mThumbDrawable」 在FastScroller(Android的奇巧)是不存在的)

try { 
      java.lang.reflect.Field f = AbsListView.class.getDeclaredField("mFastScroller"); 
      f.setAccessible(true); 
      Object o = f.get(root.findViewById(R.id.beam_contact_listview)); 
      if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { 
       f = f.getType().getDeclaredField("mThumbImage"); 
      } else { 
       f = f.getType().getDeclaredField("mThumbDrawable"); 
      } 
      f.setAccessible(true); 
      Drawable drawable = (Drawable) f.get(o); 
      drawable = getResources().getDrawable(R.drawable.sv_fastscroll); 
      f.set(o, drawable); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
+1

mThumbImage不是KitKat +上的Drawable,而是ImageButton。 – 2014-10-19 03:23:30

+0

@JaredRummler感謝您的幫助 – 2014-10-20 06:18:34

3

設置從樣式繪製對象是必經之路走。但是,如果您想以編程方式執行此操作,請使用以下兩種方法:

/** 
* Set a ListView or GridView fast scroll thumb image. 
* 
* @param listView The {@link android.widget.ListView} or {@link android.widget.GridView} 
* @param thumb The fast-scroll drawable 
* @return {@code true} if successfully set. 
*/ 
public static boolean setFastScrollThumbImage(AbsListView listView, Drawable thumb) { 
    try { 
     Field f; 
     if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) { 
      f = AbsListView.class.getDeclaredField("mFastScroll"); 
     } else { 
      f = AbsListView.class.getDeclaredField("mFastScroller"); 
     } 
     f.setAccessible(true); 
     Object o = f.get(listView); 
     if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) { 
      f = f.getType().getDeclaredField("mThumbImage"); 
      f.setAccessible(true); 
      ImageView iv = (ImageView) f.get(o); 
      iv.setImageDrawable(thumb); 
     } else { 
      f = f.getType().getDeclaredField("mThumbDrawable"); 
      f.setAccessible(true); 
      Drawable drawable = (Drawable) f.get(o); 
      drawable = thumb; 
      f.set(o, drawable); 
     } 
     return true; 
    } catch (Exception ignored) { 
    } 
    return false; 
} 

/** 
* Set a ListView or GridView fast scroll thumb color. 
* 
* @param listView The {@link android.widget.ListView} or {@link android.widget.GridView} 
* @param color The color for the fast-scroll thumb 
* @return {@code true} if successfully set. 
*/ 
public static boolean setFastScrollThumbColor(AbsListView listView, int color) { 
    try { 
     Field f; 
     if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) { 
      f = AbsListView.class.getDeclaredField("mFastScroll"); 
     } else { 
      f = AbsListView.class.getDeclaredField("mFastScroller"); 
     } 
     f.setAccessible(true); 
     Object o = f.get(listView); 
     if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) { 
      f = f.getType().getDeclaredField("mThumbImage"); 
      f.setAccessible(true); 
      ImageView iv = (ImageView) f.get(o); 
      iv.setColorFilter(color, android.graphics.PorterDuff.Mode.SRC_ATOP); 
     } else { 
      f = f.getType().getDeclaredField("mThumbDrawable"); 
      f.setAccessible(true); 
      final Drawable drawable = (Drawable) f.get(o); 
      drawable.setColorFilter(color, android.graphics.PorterDuff.Mode.SRC_ATOP); 
     } 
     return true; 
    } catch (Exception ignored) { 
    } 
    return false; 
}