本demo實現了帶有擠壓效果的地區選擇列表,能夠經過字母順序排序,分類。經過拼音或者首字母以及漢字進行搜索。android
詳細參考demo請下載:json
http://download.csdn.net/detail/sinat_33050957/9601749app
一、準備一個json文件放在assets文件夾中,文件中存儲的是全國各個城市的信息。主要包括名稱和ID。ide
二、讀取assets文件中的信息。存儲爲一個list。佈局
三、定義一個CharacterParser類,將漢字轉爲拼音。.net
四、定義一個PinyinComparator類,根據拼音來排列ListView裏面的數據類。code
五、自定義一個SideBar,做爲右側滑動的字母控件。進行選取和滑動。xml
六、對list進行排序,並將首字母提取出來,去重後,傳入自定義的SideBar中,初始化SideBar的初始字母列表。排序
七、自定義ClearEditText控件,對刪除輸入框中的內容做出處理。ci
八、佈局以下
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#f0f0f0" > <RelativeLayout android:id="@+id/addressSearchRl" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#f0f0f0" android:padding="10dp" > <sortListDemo.ClearEditText android:id="@+id/addressSearch" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/btn_white_corners_address" android:drawableLeft="@drawable/address_search" android:drawablePadding="4dp" android:hint="輸入您所在的城市" android:padding="10dp" android:singleLine="true" android:textColor="#969696" android:textSize="12sp" /> </RelativeLayout> <FrameLayout android:id="@+id/addressListRl" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@+id/addressSearchRl" android:layout_marginTop="1dp" > <ListView android:id="@+id/cityList" android:listSelector="#e4e4e4" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center" android:divider="@null" /> <TextView android:id="@+id/noCityNotice" android:layout_width="match_parent" android:layout_height="30dp" android:layout_gravity="right|top" android:background="#e4e4e4" android:gravity="center_vertical" android:text="沒有匹配的城市" android:textColor="#454545" android:visibility="gone" /> <LinearLayout android:id="@+id/title_layout" android:layout_width="fill_parent" android:layout_height="30dp" android:layout_gravity="right|top" android:background="#e4e4e4" android:orientation="vertical" > <TextView android:id="@+id/title_layout_catalog" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#e4e4e4" android:paddingBottom="5dp" android:paddingLeft="5dp" android:paddingTop="5dp" android:textColor="@android:color/black" /> </LinearLayout> <TextView android:id="@+id/txt" android:layout_width="80dp" android:layout_height="80dp" android:layout_gravity="center" android:background="#F3981C" android:gravity="center" android:textColor="@android:color/white" android:textSize="30dp" android:visibility="invisible" /> <sortListDemo.SideBar android:id="@+id/sidebar" android:layout_width="30dp" android:layout_height="match_parent" android:layout_gravity="right|center" android:layout_marginBottom="30dp" android:layout_marginTop="30dp" /> </FrameLayout> </RelativeLayout>
在該佈局中,使用TextView定義一個彈出層(@+id/txt),當滑動右側列表時,彈出當前選中的字母。由於要實現擠壓的效果,因此定義一個TextView(@+id/title_layout_catalog),當滑動時,置於上層顯示當前字母。