小小滚动条,设计还是挺复杂。
其中有两种类型的滚动条,一条是普通的滑动时的滚动条,一种是快速滚动条
项目中曾经就出现过XML定义了 android:scrollbars="none" 但是滑动时,还是会出现
滚动条,其中是因为设置了快速滚动条的原因 android:fastScrollEnabled="true",而默认android:fastScrollEnabled是false的
关于快速滚动条还有最小页数设置,见FashScroller源码,当滚动的页数在4页之内时,是不会出现快速滚动条的
// Minimum number of pages to justify showing a fast scroll thumb
private static int MIN_PAGES = 4;
以及:
// Are there enough pages to require fast scroll? Recompute only if total count changes
if (mItemCount != totalItemCount && visibleItemCount > 0) {
mItemCount = totalItemCount;
mLongList = mItemCount / visibleItemCount >= MIN_PAGES;
}
还有就是有时可能要自定义滚动条的图片
XML定义如下:
android:scrollbarThumbVertical
android:scrollbarThumbHorizontal
代码设置如下:
//修改快速滚动条图片
try {
Field f = AbsListView.class.getDeclaredField("mFastScroller");
f.setAccessible(true);
Object o = f.get(this);
f = f.getType().getDeclaredField("mThumbDrawable");
f.setAccessible(true);
Drawable drawable = (Drawable) f.get(o);
drawable = getResources().getDrawable(R.drawable.icon);
f.set(o, drawable);
} catch (Exception e) {
e.printStackTrace();
}
//修改竖向滚动条图片
try {
Field f = View.class.getDeclaredField("mScrollCache");
f.setAccessible(true);
Object scrollabilityCache = f.get(this);
f = f.getType().getDeclaredField("scrollBar");
f.setAccessible(true);
Object scrollBarDrawable = f.get(scrollabilityCache);
f = f.getType().getDeclaredField("mVerticalThumb");
f.setAccessible(true);
Drawable drawable = (Drawable) f.get(scrollBarDrawable);
drawable = getResources().getDrawable(R.drawable.rating_progress);
f.set(scrollBarDrawable, drawable);
} catch (Exception e) {
e.printStackTrace();
}
本文出自 “妈妈100开发团队” 博客,请务必保留此出处http://mama100tech.blog.51cto.com/8943983/1708721
原文地址:http://mama100tech.blog.51cto.com/8943983/1708721