如何在 RecyclerView 中使用 fastScrollEnabled? [英] How to use fastScrollEnabled in RecyclerView?

查看:37
本文介绍了如何在 RecyclerView 中使用 fastScrollEnabled?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am new to RecyclerView and I want to implement the fast scroll feature in RecyclerView like google contact application and search on the internet and i found that now Android provide officially New fastScrollEnabled boolean flag for RecyclerView. So my question is can someone provide the sample implementation of it.

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

Here is the official document from google https://developer.android.com/topic/libraries/support-library/revisions.html#26-0-0

推荐答案

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

With Support Library 26, we can easily enable fast scrolling for RecyclerView. Let’s get to it!

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

Let’s go over each property one by one :

  1. fastScrollEnabled :启用快速滚动的布尔值.将此设置为 true 将要求我们提供以下四个属性.
  2. fastScrollHorizo​​ntalThumbDrawable :一个 StateListDrawable,用于绘制可拖动的拇指水平轴.
  3. fastScrollHorizo​​ntalTrackDrawable :一个 StateListDrawable,用于绘制表示滚动条的线水平轴.
  4. fastScrollVerticalThumbDrawable:一个 StateListDrawable,用于绘制可在垂直轴上拖动的拇指.
  5. fastScrollVerticalTrackDrawable :一个 StateListDrawable,用于绘制表示滚动条的线纵轴.
  1. fastScrollEnabled : boolean value to enable the fast scrolling. Setting this as true will require that we provide the following four properties.
  2. fastScrollHorizontalThumbDrawable : A StateListDrawable that will be used to draw the thumb which will be draggable across the horizontal axis.
  3. fastScrollHorizontalTrackDrawable : A StateListDrawable that will be used to draw the line that will represent the scrollbar on horizontal axis.
  4. fastScrollVerticalThumbDrawable : A StateListDrawable that will be used to draw the thumb which will be draggable on vertical axis.
  5. fastScrollVerticalTrackDrawable : A StateListDrawable that will be used to draw the line that will represent the scrollbar on vertical axis.

在 build.gradle 中添加

add in build.gradle

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

既然支持库 26 现已移至 Google 的 maven 存储库,让我们将其包含在我们的项目级别 build.gradle

Since Support Library 26 has now been moved to Google’s maven repository, let’s include that in our project level build.gradle

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

activity_main.xml

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>

在 drawable 文件夹中添加以下四个 xml 文件,

add below four xml file in your drawable folder,

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>

这篇关于如何在 RecyclerView 中使用 fastScrollEnabled?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆