本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),当滑动时,置于上层显示当前字母。