国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android RecyclerView 實現快速滾動的示例代碼

2019-10-22 18:28:08
字體:
來源:轉載
供稿:網友

簡評:Android Support Library 26 中終于實現了一個等待已久的功能: RecyclerView 的快速滾動 。

Android 官方早就在建議開發者使用 RecyclerView 替代 ListView,RecyclerView 也確實表現要好于 ListView,除了沒有快速滾動,就像下面這樣:

Android,RecyclerView,滾動,快速滾動

因此,之前要想在 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:// 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,滾動,快速滾動

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 陇川县| 如东县| 陇川县| 来安县| 肇东市| 武山县| 新龙县| 宁波市| 邵阳市| 蒲江县| 炎陵县| 惠东县| 新兴县| 耒阳市| 娱乐| 奈曼旗| 安宁市| 崇仁县| 岚皋县| 垦利县| 应城市| 科技| 高碑店市| 紫云| 原阳县| 安多县| 剑阁县| 双柏县| 丰都县| 新野县| 如东县| 武清区| 喜德县| 濮阳县| 靖边县| 宁波市| 灵石县| 鸡西市| 邹平县| 都兰县| 美姑县|