2023年6月20日发(作者:)

androidlistview滚动条样式,⾃定义ListView滚动条样式(滑块)使⽤ListView FastScroller,默认滑块和⾃定义滑块图⽚的样⼦:设置快速滚动属性很容易,只需在布局的xml⽂件⾥设置属性即可: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 scrollthumbprivate static int MIN_PAGES = 4;以及:// Are there enough pages to require fast scroll? Recompute onlyif total count changesif (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 =laredField(“mFastScroller”);essible(true);Object o=(listView);f=e().getDeclaredField(“mThumbDrawable”);essible(true);Drawable drawable=(Drawable) (o);drawable=getResources().getDrawable();(o,drawable);xt(this, e().getName(), 1000).show();} catch (Exception e) {throw new RuntimeException(e);}这样就可以改变默认的滑块图⽚了。

2023年6月20日发(作者:)

androidlistview滚动条样式,⾃定义ListView滚动条样式(滑块)使⽤ListView FastScroller,默认滑块和⾃定义滑块图⽚的样⼦:设置快速滚动属性很容易,只需在布局的xml⽂件⾥设置属性即可: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 scrollthumbprivate static int MIN_PAGES = 4;以及:// Are there enough pages to require fast scroll? Recompute onlyif total count changesif (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 =laredField(“mFastScroller”);essible(true);Object o=(listView);f=e().getDeclaredField(“mThumbDrawable”);essible(true);Drawable drawable=(Drawable) (o);drawable=getResources().getDrawable();(o,drawable);xt(this, e().getName(), 1000).show();} catch (Exception e) {throw new RuntimeException(e);}这样就可以改变默认的滑块图⽚了。