android 如何在RecyclerView中使用fastScrollEnabled?

pftdvrlh  于 11个月前  发布在  Android
关注(0)|答案(4)|浏览(274)

我是新的ClerView,我想实现快速滚动功能在ClerView像谷歌联系人应用程序和搜索在互联网上,我发现,现在Android官方提供新的fastScrollEnabled布尔标志的ClerView。所以我的问题是,有人可以提供它的示例实现。
以下是来自google https://developer.android.com/topic/libraries/support-library/revisions.html#26-0-0的官方文档

70gysomp

70gysomp1#

有了Support Library 26,我们可以轻松地启用快速滚动功能。让我们开始吧!
让我们一个接一个地检查每个属性:
1.fastScrollEnabled:启用快速滚动的布尔值,设置为true需要提供以下四个属性。
1.fastScrollHorizontalThumbDrawable:一个StateListDrawable,用于绘制横向可拖动的拇指。
1.fastScrollHorizontalTrackDrawable:一个StateListDrawable,用于在水平轴上绘制代表滚动条的线条。
1.fastScrollVerticalThumbDrawable:StateListDrawable,用于绘制垂直轴上可拖动的拇指。
1.fastScrollVerticalTrackDrawable:StateListDrawable,用于绘制垂直轴上的滚动条线。
如果有人不知道滚动条的每个部分的名称,这里有一个图像显示它:
x1c 0d1x的数据

来源:Make your website stand out with a custom scrollbar

在build.gradle中添加

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

字符串
由于SupportLibrary 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>


在你的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>

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


**thumb.xml**

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

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

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

y53ybaqx2#

将属性添加到布局文件中的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>

djp7away

djp7away3#

如您所知,在Support Library 26.0.0中,ScrollerView支持“快速滚动”。

**注意:**请记住,它是XML。


的数据

6yoyoihd

6yoyoihd4#

对于眼睛糖果滚动条拇指(圆形边框):

<android.support.v7.widget.RecyclerView
                android:id="@+id/netflixVideoGridView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"

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

字符串

fast_scroll_thumb.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:radius="50dp" />

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

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

    <stroke
        android:width="1dp"
        android:color="#69f0ae" />

</shape>

track.xml:

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

结果:


的数据

相关问题