广告
返回顶部
首页 > 资讯 > 精选 >怎么在Android中利用RecyclerView实现一个快速滚动功能
  • 866
分享到

怎么在Android中利用RecyclerView实现一个快速滚动功能

recyclerviewandroid 2023-05-30 21:05:43 866人浏览 八月长安
摘要

本篇文章给大家分享的是有关怎么在Android中利用RecyclerView实现一个快速滚动功能,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。首先,在 build.gradle

本篇文章给大家分享的是有关怎么在Android中利用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实现一个快速滚动功能,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注编程网精选频道。

--结束END--

本文标题: 怎么在Android中利用RecyclerView实现一个快速滚动功能

本文链接: https://www.lsjlt.com/news/221168.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作