如何在RecyclerView中使用fastScrollEnabled?

Cha*_*rma 16 android android-recyclerview

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

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

Kin*_*tel 21

通过支持库26,我们可以轻松地为RecyclerView启用快速滚动.我们来吧!

让我们一个接一个地浏览每个房产:

  1. fastScrollEnabled:布尔值,用于启用快速滚动.将此设置为true将要求我们提供以下四个属性.
  2. fastScrollHorizo​​ntalThumbDrawable:一个StateListDrawable,用于绘制可在水平轴上拖动的拇指.
  3. fastScrollHorizo​​ntalTrackDrawable:一个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'
    ....
}
Run Code Online (Sandbox Code Playgroud)

由于支持库26现已移至Google的maven存储库,因此我们将其包含在项目级build.gradle中

allprojects {
    repositories {
        jcenter()
        maven {
            url "https://maven.google.com"
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

在drawable文件夹中添加以下四个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>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

  • 我按照你的例子,但我的视图不能快速滚动,只有当我触摸拇指时.但是拇指太小了,我经常碰不到它.这是正常的吗? (6认同)
  • 小拇指是一个已知问题:https://issuetracker.google.com/issues/64729576 不幸的是已经六个月了,没有修复的迹象:( (2认同)

Ran*_*ku' 7

将属性添加到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"
/>
Run Code Online (Sandbox Code Playgroud)

制作两个drawables."轨道"可绘制可以是任何可绘制的,但"拇指"可绘制必须是可绘制的状态列表.

示例drawable/fast_scroll_track.xml文件:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="#80ffffff" />
</shape>
Run Code Online (Sandbox Code Playgroud)

示例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>
Run Code Online (Sandbox Code Playgroud)