溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么在Android中利用RecyclerView實現(xiàn)一個快速滾動功能

發(fā)布時間:2021-04-08 16:30:40 來源:億速云 閱讀:364 作者:Leah 欄目:移動開發(fā)

本篇文章給大家分享的是有關(guān)怎么在Android中利用RecyclerView實現(xiàn)一個快速滾動功能,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

首先,在 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 來發(fā)布。所以,需要參考 官方指南 使用 Google Maven 倉庫。

現(xiàn)在,來看一看具體怎么實現(xiàn) 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 類型,決定是否啟用快速滾動,當設(shè)置為 true 時需要設(shè)置下面的四個屬性。

  • 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實現(xiàn)一個快速滾動功能,小編相信有部分知識點可能是我們?nèi)粘9ぷ鲿姷交蛴玫降摹OM隳芡ㄟ^這篇文章學到更多知識。更多詳情敬請關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI