Android Scrollbar和FastScrollbar

RecyclerView26使用在上一篇文章中介绍了RecyclerView26支持fastScrollbar属性。前面定义滚动条的样式时候用到这个属性

<size android:width="14dp" android:height="50dp"></size>

如果item很多比如300条,这个fastScrollbar高度就会很小,用户不方便点而且和整体UI不协调。
下面是RecyclerView(26+才有)里面初始化FastScrollbar的方法

    @VisibleForTesting
    void initFastScroller(StateListDrawable verticalThumbDrawable,
            Drawable verticalTrackDrawable, StateListDrawable horizontalThumbDrawable,
            Drawable horizontalTrackDrawable) {
        if (verticalThumbDrawable == null || verticalTrackDrawable == null
                || horizontalThumbDrawable == null || horizontalTrackDrawable == null) {
            throw new IllegalArgumentException(
                "Trying to set fast scroller without both required drawables." + exceptionLabel());
        }

        Resources resources = getContext().getResources();
        new FastScroller(this, verticalThumbDrawable, verticalTrackDrawable,
                horizontalThumbDrawable, horizontalTrackDrawable,
                resources.getDimensionPixelSize(R.dimen.fastscroll_default_thickness),
                resources.getDimensionPixelSize(R.dimen.fastscroll_minimum_range),
                resources.getDimensionPixelOffset(R.dimen.fastscroll_margin));
    }

这里直接new FastScroller并把自身this传递过去了,所以想用反射来修改fastScroller的属性是行不通的。

.....省略一部分代码
        mVerticalThumbWidth = Math.max(defaultWidth, verticalThumbDrawable.getIntrinsicWidth());
        mVerticalTrackWidth = Math.max(defaultWidth, verticalTrackDrawable.getIntrinsicWidth());
        mHorizontalThumbHeight = Math
            .max(defaultWidth, horizontalThumbDrawable.getIntrinsicWidth());
        mHorizontalTrackHeight = Math
            .max(defaultWidth, horizontalTrackDrawable.getIntrinsicWidth());
             mScrollbarMinimumRange = scrollbarMinimumRange;
.....省略一部分代码

可以看到初始化这里面的宽度取的是设置的宽度和系统默认值比较,取最大的一个
mScrollbarMinimumRange = scrollbarMinimumRange; 这个是fastScroll的可点区域,代码无法设置读取的是系统R.dimen.fastscroll_margin默认值

设置滚动高度的代码

  /**
     * Notify the scroller of external change of the scroll, e.g. through dragging or flinging on
     * the view itself.
     *
     * @param offsetX The new scroll X offset.
     * @param offsetY The new scroll Y offset.
     */
    void updateScrollPosition(int offsetX, int offsetY) {
      int verticalVisibleLength = mRecyclerViewHeight;
      int verticalContentLength = mRecyclerView.computeVerticalScrollRange();
    .......省略一些代码

   mVerticalThumbHeight = Math.min(verticalVisibleLength,
                (verticalVisibleLength * verticalVisibleLength) / verticalContentLength);
   .......省略一些代码            

这个值是item的总体高度,所以item越高 fastscrollbar的高度也就越小,并且不能设置

RecyclerView 提供一些方法可以改变这些值,有兴趣的可以试一试

      /**
     * Retrieves the size of the scroll bar thumb in our arbitrary units.
     *
     * @return Scroll bar thumb height
     */
    @Override
    public int computeVerticalScrollExtent() {
        return computeVerticalScrollExtent;
    }

    /**
     * Compute the offset of the scroll bar thumb in our scroll bar range.
     *
     * @return Offset in scroll bar range.
     */
    @Override
    public int computeVerticalScrollOffset() {
        return offset;
    }
//
    /**
     * Computes the scroll bar range. It will simply be the number of items in the adapter
     * multiplied by the given item height. The scroll extent size is also computed since it
     * will not vary. Note: The RecyclerView must be positioned at the top or this method
     * will throw an IllegalStateException.
     *
     * @return The scroll bar range
     */
    @Override
    public int computeVerticalScrollRange() 

通过上面的方法可以确定如果要让RecyclerView支持fastScrollbar,并且还要设置高度是不可行的。

但是以前的ListView是可以做到的,这里需要注意区分Scrollbar和FastScrollbar

    <ListView
        android:id="@+id/lvCatalogue"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="5dip"
        android:fastScrollEnabled="true"
        android:scrollbarThumbVertical="@drawable/scroll_bar_drawable"/>

上面的代码就可以让listview 支持fastScrollbar,但是android:scrollbarThumbVertical这个属性仅针对
scrollbar有效,fastScrollbar不会起作用。默认item数量小于4屏幕大于1屏幕会触发android:scrollbarThumbVertical,更多数量会启动fastScrollbar

ListView的父类AbsListView有fast的属性

    private FastScroller mFastScroll;

用反射可以修改这个对象的drawable

    private void test( ListView listView ){
        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.scroll_bar_drawable);
            f.set(o,drawable);
//            Toast.makeText(this, f.getType().getName(), 1000).show();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

但是在7.0,小米和三星的机器上测试反射获取mFastScroller 对象是null。所以这个方式在高版本系统上并不通用。
在显示的fastScrollbar的颜色对比上发现 Activity样式以下属性有关

 <item name="colorAccent">
 <item name="android:textColorSecondary">

所以推测这个值是加载样式的属性生效的,自定义样式如下

    <style name="BookStoreChapter" parent="AppTheme.NoActionBar">
        <item name="android:fastScrollThumbDrawable">@drawable/scroll_bar_drawable</item>
        <item name="android:fastScrollTrackDrawable">@null</item>
    </style>

android:fastScrollThumbDrawable:滚动条样式 支持drawable 不一定非是图片
android:fastScrollTrackDrawable:滚动条背景 不设置就是@null

对加载listView的Activity 对应样式添加以上样式,效果如图
这里写图片描述

高度和设置一致,同时支持selector

这里做了一下测试,把这个样式加载recyclerview 里面 不会有作用
scrollview fastscroll同样不会起作用,也不能支持快速滑动

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值