mercredi 7 janvier 2015

How to change FastScrollerThumbDrawable of a ListView on Android 2.3

My goal is to replace the listview's default fastScrollThumbDrawable to a custom Drawable. I found this link and the style for Activity takes effect on Android4.0 and later versions.


But on Android2.3 the xml style method is unavailable. I tried the reflection method from this link .But this still unavailable.From the android source code 2.3.6 I find "mThumbDrawalbe" member variable exactly exists in FastScroller.java. And mFastScroller exists in Abslistview.java. So the "mThumberDrawable" really exists. But FastScroller is a package private class .Can't java reflection access package private class's member variable?


There are some references: Custom Listview Fast Scrollbar in android


AbsListView.java src: http://ift.tt/1yCWOhe


FastScroller.java src: http://ift.tt/1DzCnRw


Here's my method under android 2.3.



private void setFastScrollerThumb(ListView view){
if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
try {
java.lang.reflect.Field f = AbsListView.class.getDeclaredField("mFastScroller");
f.setAccessible(true);
Object fastScrollerObj= f.get(view);
Class <?> fastScrollerCls=f.getType();
Field [] allFileds=fastScrollerCls.getDeclaredFields();

for(Field item:allFileds){
Log.i("ZYStudio", "item is:"+item.getName()+"|"+item.getType().getName());
}
//'allFields' result empty
f=fastScrollerCls.getDeclaredField("mThumbDrawable");
f.setAccessible(true);
Drawable drawable = (Drawable) f.get(fastScrollerObj);
drawable = getResources().getDrawable(R.drawable.novel_catagory_fastscroll_thumb);
f.set(fastScrollerObj, drawable);
} catch (Exception e) {
e.printStackTrace();
Log.i("ZYStudio", "fastscrollerThumb occured:"+e.getClass().getName());
}
}
}





Aucun commentaire:

Enregistrer un commentaire