当前位置: 移动技术网 > IT编程>移动开发>Android > Android RecyclerView 实现快速滚动的示例代码

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

2019年07月24日  | 移动技术网IT编程  | 我要评论

连载漫画,完美国际免费外挂,厦门乐购班车

简评:android support library 26 中终于实现了一个等待已久的功能: recyclerview 的快速滚动 。

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

因此,之前要想在 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:// 来发布。所以,需要参考 官方指南 使用 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>

效果如下:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网