Android RecyclerView 实现快速滚动的示例代码

作者:Hevin 时间:2023-02-17 05:32:50 

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

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

Android RecyclerView 实现快速滚动的示例代码

因此,之前要想在 RecyclerView 上实现快速滚动,往往是依赖第三方库,比如:FutureMind/recycler-fast-scroll 或 timusus/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 计划未来将所有的仓库都只通过 http:// 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>

效果如下:

Android RecyclerView 实现快速滚动的示例代码

来源:https://zhuanlan.zhihu.com/p/29173895?utm_source=tuicool&utm_medium=referral

标签:RecyclerView,滚动
0
投稿

猜你喜欢

  • 浅谈java封装

    2023-05-05 22:48:20
  • Android UI实现SlidingMenu侧滑菜单效果

    2021-12-08 16:03:34
  • No ‘Access-Control-Allow-Origin‘ header is present跨域及解决

    2022-07-12 13:33:08
  • intellij idea14打包apk文件和查看sha1值

    2022-05-25 13:18:37
  • SpringBoot如何使用RateLimiter通过AOP方式进行限流

    2023-09-16 18:18:44
  • SpringBoot雪花算法主键ID传到前端后精度丢失问题的解决

    2022-07-18 02:30:47
  • wpf将表中数据显示到datagrid示例

    2023-06-13 04:14:06
  • C#9.0推出的4个新特性介绍

    2021-10-10 07:49:29
  • java开发RocketMQ之NameServer路由管理源码分析

    2023-10-27 20:56:12
  • Java实现AWT四大事件的详细过程

    2023-11-28 18:39:52
  • java验证电话号码的方法

    2023-04-01 21:44:41
  • Java中String、StringBuffer、StringBuilder的区别介绍

    2023-11-20 18:31:32
  • springboot自动配置原理以及spring.factories文件的作用详解

    2021-12-20 20:19:27
  • java使用hadoop实现关联商品统计

    2022-11-05 05:55:43
  • spring Boot打包部署到远程服务器的tomcat中

    2023-01-14 21:45:28
  • JDK8并行流及串行流区别原理详解

    2023-06-25 16:23:39
  • 基于Android XML解析与保存的实现

    2023-05-25 18:54:32
  • C语言实现稀疏矩阵

    2023-04-17 15:00:45
  • android 中 SQLiteOpenHelper的封装使用详解

    2022-11-09 12:36:57
  • 海量数据去重排序bitmap(位图法)在java中实现的两种方法

    2022-10-10 17:27:36
  • asp之家 软件编程 m.aspxhome.com