RecyclerView 实现快速滚动

简评:Android Support Library 26 中终于实现了一个等待已久的功能:RecyclerView 的快速滚动

Android 官方早就在建议开发者使用 RecyclerView 替代 ListView,RecyclerView 也确实表现要好于 ListView,除了没有快速滚动,就像下面这样:

因此,之前要想在 RecyclerView 上实现快速滚动,往往是依赖第三方库,比如:FutureMind/recycler-fast-scrolltimusus/RecyclerView-FastScroll

现在 RecyclerView 终于原生支持了快速滚动,现在就让我们来看一下怎么实现:

首先,在 build.gradle 中添加依赖:

dependencies {
    ....
    compile 'com.android.support:design:26.0.2'
    compile 'com.android.support:recyclerview-v7:26.0.2'
    ....
}

注意 Support Library 从版本 26 开始移到了 Google 的 maven 仓库,并且 Google 计划未来将所有的仓库都只通过maven.google.com来发布。所以,需要参考官方指南使用 Google Maven 仓库。

现在,来看一看具体怎么实现 RecyclerView 的快速滚动:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.shaishavgandhi.fastscrolling.MainActivity"
    tools:showIn="@layout/activity_main">


 <android.support.v7.widget.RecyclerView
    android:id="@+id/recyclerView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:fastScrollEnabled="true"
    app:fastScrollHorizontalThumbDrawable="@drawable/thumb_drawable"
    app:fastScrollHorizontalTrackDrawable="@drawable/line_drawable"
    app:fastScrollVerticalThumbDrawable="@drawable/thumb_drawable"
    app:fastScrollVerticalTrackDrawable="@drawable/line_drawable">

 </android.support.v7.widget.RecyclerView>

</android.support.constraint.ConstraintLayout>

其中增加了几个属性:

  • fastScrollEnabled:boolean 类型,决定是否启用快速滚动,当设置为 true 时需要设置下面的四个属性。

  • fastScrollHorizontalThumbDrawable:水平滚动块。

  • fastScrollHorizontalTrackDrawable:水平滚动栏背景。

  • fastScrollVerticalThumbDrawable:竖直滚动块。

  • fastScrollVerticalTrackDrawable:竖直滚动栏背景。

接下来看一下具体的 drawable:

line_drawable.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_pressed="true"
        android:drawable="@drawable/line"/>

    <item
        android:drawable="@drawable/line"/>
</selector>

line.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle">

    <solid android:color="@android:color/darker_gray" />

    <padding
        android:top="10dp"
        android:left="10dp"
        android:right="10dp"
        android:bottom="10dp"/>
</shape>

thumb_drawable.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_pressed="true"
        android:drawable="@drawable/thumb"/>

    <item
        android:drawable="@drawable/thumb"/>
</selector>

thumb.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle">

    <corners
        android:topLeftRadius="44dp"
        android:topRightRadius="44dp"
        android:bottomLeftRadius="44dp" />

    <padding
        android:paddingLeft="22dp"
        android:paddingRight="22dp" />

    <solid android:color="@color/colorPrimaryDark" />

</shape>

效果如下:


gif

原文:Fast Scrolling with RecyclerView
扩展阅读:
现代 Android 开发资源汇总
为什么 Android 开发者都应该尝试一下 Anko?

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 172,813评论 25 708
  • afinalAfinal是一个android的ioc,orm框架 https://github.com/yangf...
    passiontim阅读 15,489评论 2 45
  • 《随笔【我的父亲母亲】》 静思语 2014-8-8 12:26 父母给了我们生命,她们用无私的爱滋润着我们,用一生...
  • 一九九五年,十月二十日,黑不溜秋的婴儿呱呱落地,他生在水墨徽州,从一开始他就该生在这里才对,自生来时便不断以哭闹的...
    Young汪杨阅读 504评论 3 1
  • 不积跬步无以成千里,生活中我们总是容易被内心的贪婪所掌控,总想着一下子吃个大胖子,却不愿付诸更多的努力。 没有我们...
    夢瑤阅读 232评论 0 0