在上一篇文章中,我和大家一起實現了類似於Android系統連絡人的分組導航和擠壓動畫功能,不過既然 文章名叫做《Android系統連絡人全特效實現》,那麼沒有快速滾動功能顯然是稱不上"全"的。 因此本篇文章我將帶領大家在上篇文章的代碼基礎上改進,加入快速滾動功能。
如果還沒有看過我 上一篇文章,請抓緊去閱讀一下 Android系統連絡人全特效實現(上),分組導航和擠壓動畫 。
其實 ListView本身是有一個快速滾動屬性的,可以通過在XML中設定 android:fastScrollEnabled="true"來啟用。包括以前老版本的Android連絡人中都是使用這種 方式來進行快速滾動的。效果如下圖所示:
不過這種快速滾動方式比較 醜陋,到後來很多手機廠商在定製自己ROM的時候都將預設快速滾動改成了類似iPhone上A-Z字母錶快速滾動 的方式。這裡我們怎麼能落後於時代的潮流呢!我們的快速滾動也要使用A-Z字母表的方式!
下面就 來開始實現,首先開啟上次的ContactsDemo工程,修改activity_main.xml布局檔案。由於我們要在介面上 加入字母表,因此我們需要一個Button,將這個Button的背景設為一張A-Z排序的圖片,然後居靠右對齊。另 外還需要一個TextView,用於在彈出式分組布局上顯示當前的分組,預設是gone掉的,只有手指在字母表上 滑動時才讓它顯示出來。修改後的布局檔案代碼如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ListView android:id="@+id/contacts_list_view" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:scrollbars="none" android:fadingEdge="none" > </ListView> <LinearLayout android:id="@+id/title_layout" android:layout_width="fill_parent" android:layout_height="18dip" android:layout_alignParentTop="true" android:background="#303030" > <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginLeft="10dip" android:textColor="#ffffff" android:textSize="13sp" /> </LinearLayout> <Button android:id="@+id/alphabetButton" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_alignParentRight="true" android:background="@drawable/a_z" /> <RelativeLayout android:id="@+id/section_toast_layout" android:layout_width="70dip" android:layout_height="70dip" android:layout_centerInParent="true" android:background="@drawable/section_toast" android:visibility="gone" > <TextView android:id="@+id/section_toast_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:textColor="#fff" android:textSize="30sp" /> </RelativeLayout> </RelativeLayout>