Android之自定义ListView滚动条样式
2013-09-25 14:19
537 查看
一、标明:不是快速滚动条(通过xml实现)
XML布局使用 android:scrollbarThumbVertical
二、通过代码方式实现:
找了找源码,
//修改快速滚动条图片
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);
} catch (Exception e) {
e.printStackTrace();
}
//修改竖向滚动条图片
try {
Field f = View.class.getDeclaredField("mScrollCache");
f.setAccessible(true);
Object scrollabilityCache = f.get(listView);
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();
}
使用ListView FastScroller,默认滑块和自定义滑块图片的样子:
![](http://img.my.csdn.net/uploads/201301/05/1357384453_9119.png)
![](http://img.my.csdn.net/uploads/201301/05/1357384367_7669.png)
设置快速滚动属性很容易,只需在布局的xml文件里设置属性即可:
[html] view
plaincopy
<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的常量声明:
[java] view
plaincopy
// Minimum number of pages to justify showing a fast scroll thumb
private static int MIN_PAGES = 4;
以及:
[java] view
plaincopy
// 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),只能供包内的类调用。
因此反射代码写的稍微麻烦一些:
[java] view
plaincopy
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);
}
XML布局使用 android:scrollbarThumbVertical
二、通过代码方式实现:
找了找源码,
//修改快速滚动条图片
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);
} catch (Exception e) {
e.printStackTrace();
}
//修改竖向滚动条图片
try {
Field f = View.class.getDeclaredField("mScrollCache");
f.setAccessible(true);
Object scrollabilityCache = f.get(listView);
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();
}
使用ListView FastScroller,默认滑块和自定义滑块图片的样子:
![](http://img.my.csdn.net/uploads/201301/05/1357384453_9119.png)
![](http://img.my.csdn.net/uploads/201301/05/1357384367_7669.png)
设置快速滚动属性很容易,只需在布局的xml文件里设置属性即可:
[html] view
plaincopy
<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的常量声明:
[java] view
plaincopy
// Minimum number of pages to justify showing a fast scroll thumb
private static int MIN_PAGES = 4;
以及:
[java] view
plaincopy
// 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),只能供包内的类调用。
因此反射代码写的稍微麻烦一些:
[java] view
plaincopy
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);
}
相关文章推荐
- Android 自定义ListView滚动条样式
- android自定义listview滚动条的样式
- Android ListView的滚动条始终显示并且滚动条样式自定义
- Android之自定义ListView滚动条样式
- Android ListView的滚动条始终显示并且滚动条样式自定义
- Android ListView的滚动条始终显示并且滚动条样式自定义
- Android ListView的滚动条始终显示并且滚动条样式自定义
- Android之自定义ListView滚动条样式
- android listview gridview等自定义滚动条样式(非反射)
- Android ListView的滚动条样式
- Android ListView的滚动条样式
- android中listview的一些样式设置,自定义ListView行间的分割线
- Android 自定义ListView每行样式
- Android滚轮控件,基于ListView实现,可以自定义样式。
- Android重写系列(2)——建立在Fragment上的ListView(自定义表格样式)
- Android之如何自定义ListView样式
- Android ListView的滚动条样式
- Android ListView的滚动条样式
- Android学习之ListView自定义样式
- Android滚轮控件,基于ListView实现,可以自定义样式。