如何在 RecyclerView 中使用 fastScrollEnabled?

新手上路,请多包涵

我是 RecyclerView 的新手,我想在 RecyclerView 中实现快速滚动功能,例如谷歌联系人应用程序并在互联网上搜索,我发现现在 Android 正式提供新的 fastScrollEnabled 布尔标志用于 RecyclerView。所以我的问题是有人可以提供它的示例实现吗?

这是来自谷歌的官方文档 https://developer.android.com/topic/libraries/support-library/revisions.html#26-0-0

原文由 Chandan Sharma 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 968
2 个回答

使用 Support Library 26,我们可以轻松地为 RecyclerView 启用快速滚动。让我们开始吧!

让我们一一检查每个属性:

  1. fastScrollEnabled : 启用快速滚动的布尔值。将其设置为 true 将需要我们提供以下四个属性。
  2. fastScrollHorizontalThumbDrawable :一个 StateListDrawable ,用于绘制可在水平轴上拖动的拇指。
  3. fastScrollHorizontalTrackDrawable :一个 StateListDrawable ,用于绘制表示水平轴上滚动条的线。
  4. fastScrollVerticalThumbDrawable :一个 StateListDrawable ,用于绘制可在垂直轴上拖动的拇指。
  5. fastScrollVerticalTrackDrawable :一个 StateListDrawable ,用于绘制表示垂直轴上滚动条的线。

在 build.gradle 中添加

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

由于 Support Library 26 现已移至 Google 的 Maven 存储库,让我们将其包含在我们的项目级别 build.gradle 中

allprojects {
    repositories {
        jcenter()
        maven {
            url "https://maven.google.com"
        }
    }
}

activity_main.xml

 <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>

在您的可绘制文件夹中添加以下四个 xml 文件,

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>

行.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>

拇指.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>

原文由 Kintan Patel 发布,翻译遵循 CC BY-SA 3.0 许可协议

将属性添加到布局文件中的 RecyclerView 标记:

 <android.support.v7.widget.RecyclerView
    xmlns:app="http://schemas.android.com/apk/res-auto"
    ...
    app:fastScrollEnabled="true"
    app:fastScrollHorizontalThumbDrawable="@drawable/fast_scroll_thumb"
    app:fastScrollHorizontalTrackDrawable="@drawable/fast_scroll_track"
    app:fastScrollVerticalThumbDrawable="@drawable/fast_scroll_thumb"
    app:fastScrollVerticalTrackDrawable="@drawable/fast_scroll_track"
/>

制作两个可绘制对象。 “轨道”可绘制对象可以是任何可绘制对象,但“拇指”可绘制对象必须是状态列表可绘制对象。

示例 drawable/fast_scroll_track.xml 文件:

 <shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#80ffffff" />
</shape>

示例 drawable/fast_scroll_thumb.xml 文件:

 <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <shape android:shape="rectangle">
            <solid android:color="#ff0" />
        </shape>
    </item>
    <item>
        <shape android:shape="rectangle">
            <solid android:color="#fff" />
        </shape>
    </item>
</selector>

原文由 Randy Sugianto ‘Yuku’ 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题