fast scroll custom thumb
how can i set custom thumb for fast scroll in lis开发者_StackOverflowtview.
you can set that into the style.xml
<style name="Theme_app" parent="@android:style/Theme.Holo.Light">
<item name="android:fastScrollThumbDrawable">@drawable/fastscroll_thumb_holo</item>
</style>
than create an xml in Drawable for the image
<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>
Setting the Drawable from styles is the way to go. However, if you want to do this programmatically here are two methods that should be useful:
/**
* 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;
}
hard code for android kitkat (field "mThumbDrawable" in FastScroller (android kitkat) is not exist)
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();
}
If you have a look at the source code for Android 2.2r1 (revision 1), there is a class called android.widget.FastScroller
, which has the method useThumbDrawable()
.
I just downloaded the source from a blog called MobileBytes which contains it, maybe you could import that into your project and implement it? (or upgrade your API to 2.2 r1)
I'm using the android:fastScrollThumbDrawable
but I not know why it isn't working, so searching on web i found here a hard code solution, I not know if it works on old API but in my case was solved the problem. Note I'm using API 18 like target and a device with API 17 to test.
the code:
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();
}
精彩评论