Android RecyclerView 實(shí)現(xiàn)快速滾動(dòng)的示例代碼
簡(jiǎn)評(píng):Android Support Library 26 中終于實(shí)現(xiàn)了一個(gè)等待已久的功能: RecyclerView 的快速滾動(dòng) 。
Android 官方早就在建議開(kāi)發(fā)者使用 RecyclerView 替代 ListView,RecyclerView 也確實(shí)表現(xiàn)要好于 ListView,除了沒(méi)有快速滾動(dòng),就像下面這樣:

因此,之前要想在 RecyclerView 上實(shí)現(xiàn)快速滾動(dòng),往往是依賴第三方庫(kù),比如:FutureMind/recycler-fast-scroll 或 timusus/RecyclerView-FastScroll 。
現(xiàn)在 RecyclerView 終于原生支持了快速滾動(dòng),現(xiàn)在就讓我們來(lái)看一下怎么實(shí)現(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 開(kāi)始移到了 Google 的 maven 倉(cāng)庫(kù),并且 Google 計(jì)劃未來(lái)將所有的倉(cāng)庫(kù)都只通過(guò) http:// maven.google.com 來(lái)發(fā)布。所以,需要參考 官方指南 使用 Google Maven 倉(cāng)庫(kù)。
現(xiàn)在,來(lái)看一看具體怎么實(shí)現(xiàn) RecyclerView 的快速滾動(dòng):
<?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>
其中增加了幾個(gè)屬性:
- fastScrollEnabled: boolean 類型,決定是否啟用快速滾動(dòng),當(dāng)設(shè)置為 true 時(shí)需要設(shè)置下面的四個(gè)屬性。
- fastScrollHorizontalThumbDrawable: 水平滾動(dòng)塊。
- fastScrollHorizontalTrackDrawable: 水平滾動(dòng)欄背景。
- fastScrollVerticalThumbDrawable: 豎直滾動(dòng)塊。
- fastScrollVerticalTrackDrawable: 豎直滾動(dòng)欄背景。
接下來(lái)看一下具體的 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>
效果如下:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- RecyclerView實(shí)現(xiàn)縱向和橫向滾動(dòng)
- RecyclerView實(shí)現(xiàn)抖音縱向滾動(dòng)ViewPager效果
- Android RecyclerView 滾動(dòng)到中間位置的方法示例
- Android使用Recyclerview實(shí)現(xiàn)圖片水平自動(dòng)循環(huán)滾動(dòng)效果
- XRecyclerView實(shí)現(xiàn)下拉刷新、滾動(dòng)到底部加載更多等功能
- Android_RecyclerView實(shí)現(xiàn)上下滾動(dòng)廣告條實(shí)例(帶圖片)
- Android中RecyclerView實(shí)現(xiàn)分頁(yè)滾動(dòng)的方法詳解
- Android使用RecyclerView實(shí)現(xiàn)水平滾動(dòng)控件
- Android代碼實(shí)現(xiàn)AdapterViews和RecyclerView無(wú)限滾動(dòng)
- RecyclerView實(shí)現(xiàn)橫向滾動(dòng)效果
相關(guān)文章
Android回調(diào)與觀察者模式的實(shí)現(xiàn)原理
這篇文章主要為大家詳細(xì)介紹了Android回調(diào)與觀察者模式的實(shí)現(xiàn)原理,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04
Android自定義View實(shí)現(xiàn)繪制虛線的方法詳解
這篇文章主要給大家介紹了Android自定義View實(shí)現(xiàn)繪制虛線的方法,在繪制過(guò)程中走了一些彎路才實(shí)現(xiàn)了虛線的效果,所以想著總結(jié)分享出來(lái),方便有需要的朋友和自己在需要的時(shí)候參考學(xué)習(xí),下面來(lái)一起看看吧。2017-04-04
Android程序開(kāi)發(fā)仿新版QQ鎖屏下彈窗功能
最近做了一個(gè)項(xiàng)目,其中涉及到這樣一個(gè)功能:新版的qq能在鎖屏下彈窗顯示qq消息,下面小編抽時(shí)間把實(shí)現(xiàn)代碼分享給大家感興趣的朋友參考下吧2016-09-09
Android百度地圖應(yīng)用之創(chuàng)建顯示地圖
這篇文章主要為大家詳細(xì)介紹了Android百度地圖應(yīng)用之創(chuàng)建顯示地圖,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-06-06
Android中FTP上傳、下載的功能實(shí)現(xiàn)(含進(jìn)度)
本篇文章主要介紹了Android中FTP上傳、下載(含進(jìn)度),具有一定的參考價(jià)值,有需要的可以了解一下。2016-11-11
Android酷炫動(dòng)畫(huà)效果之3D星體旋轉(zhuǎn)效果
本文要實(shí)現(xiàn)的3D星體旋轉(zhuǎn)效果是從CoverFlow演繹而來(lái),不過(guò)CoverFlow只是對(duì)圖像進(jìn)行轉(zhuǎn)動(dòng),我這里要實(shí)現(xiàn)的效果是要對(duì)所有的View進(jìn)行類似旋轉(zhuǎn)木馬的轉(zhuǎn)動(dòng)2018-05-05

