自定义ListView FastScroller滑块图片 以及 android:fastScrollEnabled="true" 不起作用
2012-02-28 10:55
671 查看
使用ListView FastScroller,默认滑块和自定义滑块图片的样子:
设置快速滚动属性很容易,只需在布局的xml文件里设置属性即可:
<ListView android:id="@+id/listView" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:fastScrollEnabled="true"
android:focusable="true" />
但是有时候会发现设置属性无效,滚动ListView并未出现滑块。原因是该属性生效有最小记录限制。当ListView记录能够在4屏以内显示(也就是说滚动4页)就不会出现滑块。可能是api设计者认为这么少的记录不需要快速滚动。
我的依据是android源代码,见FastScroller的常量声明:
// 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;
}
通篇查看了ListView及其超累AbsListView,都未找到自定义图片的设置接口。看来是没打算让开发者更改了。但是用户要求我们自定义这个图片。那只能用非常手段了。
经过分析发现,该图片是ListView超类AbsListView的一个成员mFastScroller对象的成员mThumbDrawable。这里mThumbDrawable是Drawable类型的。mFastScroller是FastScroller类型,这个类型比较麻烦,类的声明没有modifier,也就是default(package),只能供包内的类调用。
因此反射代码写的稍微麻烦一些:
try {
Field f = AbsListView.class.getDeclaredField("mFastScroller");
f.setAccessible(true);
Object o=f.get(listView);
f=f.getType().getDeclaredField("mThumbDrawable");
f.setAccessible(true);
Drawable drawable=(Drawable) f.get(o);
drawable=getResources().getDrawable(R.drawable.icon);
f.set(o,drawable);
Toast.makeText(this, f.getType().getName(), 1000).show();
} catch (Exception e) {
throw new RuntimeException(e);
}
这样就可以改变默认的滑块图片了。
源代码见:
http://easymorse.googlecode.com/svn/tags/ListViewCustomerFastScroller-0.1/
转:http://marshal.easymorse.com/archives/3929
设置快速滚动属性很容易,只需在布局的xml文件里设置属性即可:
<ListView android:id="@+id/listView" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:fastScrollEnabled="true"
android:focusable="true" />
但是有时候会发现设置属性无效,滚动ListView并未出现滑块。原因是该属性生效有最小记录限制。当ListView记录能够在4屏以内显示(也就是说滚动4页)就不会出现滑块。可能是api设计者认为这么少的记录不需要快速滚动。
我的依据是android源代码,见FastScroller的常量声明:
// 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;
}
通篇查看了ListView及其超累AbsListView,都未找到自定义图片的设置接口。看来是没打算让开发者更改了。但是用户要求我们自定义这个图片。那只能用非常手段了。
经过分析发现,该图片是ListView超类AbsListView的一个成员mFastScroller对象的成员mThumbDrawable。这里mThumbDrawable是Drawable类型的。mFastScroller是FastScroller类型,这个类型比较麻烦,类的声明没有modifier,也就是default(package),只能供包内的类调用。
因此反射代码写的稍微麻烦一些:
try {
Field f = AbsListView.class.getDeclaredField("mFastScroller");
f.setAccessible(true);
Object o=f.get(listView);
f=f.getType().getDeclaredField("mThumbDrawable");
f.setAccessible(true);
Drawable drawable=(Drawable) f.get(o);
drawable=getResources().getDrawable(R.drawable.icon);
f.set(o,drawable);
Toast.makeText(this, f.getType().getName(), 1000).show();
} catch (Exception e) {
throw new RuntimeException(e);
}
这样就可以改变默认的滑块图片了。
源代码见:
http://easymorse.googlecode.com/svn/tags/ListViewCustomerFastScroller-0.1/
转:http://marshal.easymorse.com/archives/3929
相关文章推荐
- Android常见问题分析之自定义ListView FastScroller滑块图片 以及 android:fastScrollEnabled="true" 不起作用
- 自定义ListView FastScroller滑块图片 以及 android:fastScrollEnabled="true" 不起作用
- Android-自定义ListView FastScroller滑块图片
- Android之ListView的快速滑动模式:fastScrollEnabled以及滑块的自定义
- 自定义ListView FastScroller滑块图片
- 【转】自定义ListView FastScroller滑块图片
- Android中自定义SeekBar的背景颜色,进度条颜色,以及滑块的图片
- Android中自定义SeekBar的背景颜色,进度条颜色,以及滑块的图片
- Android中自定义SeekBar的背景颜色,进度条颜色,以及滑块的图片
- ListView自定义 FastScroller滑块
- Android中自定义SeekBar的背景颜色,进度条颜色,以及滑块的图片
- Android中自定义SeekBar的背景颜色,进度条颜色,以及滑块的图片
- Android中自定义SeekBar的背景颜色,进度条颜色,以及滑块的图片
- Android中自定义SeekBar的背景颜色,进度条颜色,以及滑块的图片
- android 自定义ScrollView实现反弹效果(以及解决和ListView之间的冲突)
- Android 对Layout_weight属性完全解析以及使用ListView来实现表格(自定义适配器)
- 我的Android进阶之旅------>如何为ListView组件加上快速滑块以及修改快速滑块图像
- 使android:fillAfter="true"在xml中起作用
- android 自定义ScrollView实现反弹效果(以及解决和ListView之间的冲突)
- android-自定义View-GridListView(仿Q空间好友动态列表图片展示方式)