Android RecyclerView 實(shí)現(xiàn)快速滾動(dòng)的示例代碼
簡評(píng):Android Support Library 26 中終于實(shí)現(xiàn)了一個(gè)等待已久的功能: RecyclerView 的快速滾動(dòng) 。
Android 官方早就在建議開發(fā)者使用 RecyclerView 替代 ListView,RecyclerView 也確實(shí)表現(xiàn)要好于 ListView,除了沒有快速滾動(dòng),就像下面這樣:
因此,之前要想在 RecyclerView 上實(shí)現(xiàn)快速滾動(dòng),往往是依賴第三方庫,比如:FutureMind/recycler-fast-scroll 或 timusus/RecyclerView-FastScroll 。
現(xiàn)在 RecyclerView 終于原生支持了快速滾動(dòng),現(xiàn)在就讓我們來看一下怎么實(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 開始移到了 Google 的 maven 倉庫,并且 Google 計(jì)劃未來將所有的倉庫都只通過 http:// maven.google.com 來發(fā)布。所以,需要參考 官方指南 使用 Google Maven 倉庫。
現(xiàn)在,來看一看具體怎么實(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)欄背景。
接下來看一下具體的 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>
效果如下:
以上就是本文的全部內(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)分頁滾動(dòng)的方法詳解
- Android使用RecyclerView實(shí)現(xiàn)水平滾動(dòng)控件
- Android代碼實(shí)現(xiàn)AdapterViews和RecyclerView無限滾動(dòng)
- RecyclerView實(shí)現(xiàn)橫向滾動(dòng)效果
相關(guān)文章
android中webview控件和javascript交互實(shí)例
這篇文章主要介紹了android中webview控件和javascript交互實(shí)例,例子中包括javascript調(diào)用java的方法,java代碼中調(diào)用javascript的方法,需要的朋友可以參考下2014-07-07Android手勢(shì)ImageView三部曲 第二部
這篇文章主要為大家詳細(xì)介紹了Android手勢(shì)ImageView三部曲的第二部,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03Android實(shí)現(xiàn)兩個(gè)數(shù)相加功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)兩個(gè)數(shù)相加功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-03-03Android開發(fā)之創(chuàng)建可點(diǎn)擊的Button實(shí)現(xiàn)方法
這篇文章主要介紹了Android創(chuàng)建可點(diǎn)擊的Button實(shí)現(xiàn)方法,實(shí)例分析了Android創(chuàng)建button按鈕過程中的界面配置,功能實(shí)現(xiàn)與相關(guān)注意事項(xiàng),需要的朋友可以參考下2016-03-03Android 一個(gè)日歷控件的實(shí)現(xiàn)代碼
本篇文章主要介紹了Android 一個(gè)日歷控件的實(shí)現(xiàn)代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-05-05Android打造屬于自己的新聞平臺(tái)(客戶端+服務(wù)器)
這篇文章主要為大家詳細(xì)介紹了Android打造屬于自己的新聞平臺(tái)的相關(guān)資料,Android實(shí)現(xiàn)新聞客戶端服務(wù)器,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-06-06Android android:exported = true 用法詳解
在本篇文章里小編給大家整理了關(guān)于Android android:exported = true 用法,需要的朋友們參考下。2019-09-09Android實(shí)現(xiàn)調(diào)用攝像頭
本文給大家分享的是,在安卓APP開發(fā)的過程中,經(jīng)常會(huì)需要調(diào)用手機(jī)自身攝像頭拍照的代碼,十分的簡單實(shí)用,有需要的小伙伴可以參考下。2015-07-07Android實(shí)現(xiàn)輕量線性與百分比圖表的方法
這篇文章主要給大家介紹了關(guān)于Android實(shí)現(xiàn)輕量線性與百分比圖表的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)學(xué)習(xí)吧。2017-12-12