使用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 = ;

以及:

// Are there enough pages to require fast scroll? Recompute only if total count changes
if (mItemCount != totalItemCount && visibleItemCount > ) {
  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(), ).show();
} catch (Exception e) {
throw new RuntimeException(e);
}

最新文章

  1. bzoj1023: [SHOI2008]cactus仙人掌图
  2. JS几种数组遍历方式以及性能分析对比
  3. Python记录-Pip安装
  4. soap
  5. (转)Eclipse平台技术概述
  6. 4D数据介绍
  7. Java基础知识强化之IO流笔记29:BufferedOutputStream / BufferedInputStream(字节缓冲流)之BufferedInputStream读取数据
  8. 关于&lt;%@ include file=&quot; &quot; %&gt;与&lt;jsp:include page=&quot;&quot;&gt;&lt;/jsp:include&gt;中的那些问题?
  9. 转:ASP.Net MVC:校验、AJAX与过滤器
  10. 用C#开发较完整的Windows任务管理器
  11. ThinkPHP 3.1.2 控制器的模块和操作
  12. APICloud框架—db数据库模块
  13. Hive简单安装
  14. Java Socket:Java-NIO-ServerSocketChannel
  15. Python 命令行(CLI)基础库
  16. Tomcat 报 The valid characters are defined in RFC 7230 and RFC 3986
  17. Linux下Vi/Vim的使用方法
  18. JavaScript面向对象编程小游戏---贪吃蛇
  19. Docker for Windows 里的Shared Drives 设置不生效
  20. C# Meta Programming - Let Your Code Generate Code - Introduction of The Text Template Transformation Toolkit(T4)

热门文章

  1. bootstrap的使用2
  2. springboot整合mybatis,druid,mybatis-generator插件完整版
  3. python基础01—基础数据类型
  4. tornado 07 数据库—ORM—SQLAlchemy—查询
  5. C++_标准模板库STL概念介绍4-算法
  6. 【研究】Metasploit自动攻击模块
  7. 【总结】sqlmap命令
  8. 在ASP.NET Core Web API 项目里无法访问(wwwroot)下的文件
  9. Android耗时操作
  10. GreenPlum 大数据平台--非并行备份(六)