├── .gitignore ├── README.md ├── SelectCityModule.iml ├── app ├── .gitignore ├── app.iml ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── socks │ │ └── selectcitymodule │ │ └── ApplicationTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── socks │ │ │ └── selectcitymodule │ │ │ ├── MainActivity.java │ │ │ ├── SortAdapter.java │ │ │ └── view │ │ │ ├── CharacterParser.java │ │ │ ├── CitySortModel.java │ │ │ ├── PinyinComparator.java │ │ │ └── SideBar.java │ └── res │ │ ├── drawable-hdpi │ │ └── ic_city_location.png │ │ ├── drawable-xhdpi │ │ └── ic_city_location.png │ │ ├── layout │ │ ├── activity_main.xml │ │ └── item_select_city.xml │ │ ├── menu │ │ └── menu_main.xml │ │ ├── mipmap-hdpi │ │ └── ic_launcher.png │ │ ├── mipmap-mdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxxhdpi │ │ └── ic_launcher.png │ │ ├── values-v21 │ │ └── styles.xml │ │ ├── values-w820dp │ │ └── dimens.xml │ │ └── values │ │ ├── arrays.xml │ │ ├── colors.xml │ │ ├── dimens.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── socks │ └── selectcitymodule │ └── ExampleUnitTest.java ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | /.idea 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .DS_Store 7 | /build 8 | /captures 9 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SelectCityModule 2 | 选择省份或者是城市模块,可以直接拿来用 3 | 4 | 效果图 5 | ![](http://i11.tietuku.com/32ab49ac382b7dfd.gif) 6 | 7 | 本项目改造自[http://blog.csdn.net/xiaanming/article/details/12684155](http://blog.csdn.net/xiaanming/article/details/12684155) 8 | 9 | 修复了几个BUG和界面效果 10 | -------------------------------------------------------------------------------- /SelectCityModule.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/app.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 8 | 9 | 10 | 11 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 22 5 | buildToolsVersion "23.0.0" 6 | 7 | defaultConfig { 8 | applicationId "com.socks.selectcitymodule" 9 | minSdkVersion 9 10 | targetSdkVersion 22 11 | versionCode 1 12 | versionName "1.0" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | } 21 | 22 | dependencies { 23 | compile fileTree(dir: 'libs', include: ['*.jar']) 24 | testCompile 'junit:junit:4.12' 25 | compile 'com.android.support:appcompat-v7:22.2.1' 26 | compile 'com.android.support:design:22.2.1' 27 | } 28 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /Users/zhaokaiqiang/Develop/sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/socks/selectcitymodule/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package com.socks.selectcitymodule; 2 | 3 | import android.app.Application; 4 | import android.test.ApplicationTestCase; 5 | 6 | /** 7 | * Testing Fundamentals 8 | */ 9 | public class ApplicationTest extends ApplicationTestCase { 10 | public ApplicationTest() { 11 | super(Application.class); 12 | } 13 | } -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 11 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /app/src/main/java/com/socks/selectcitymodule/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.socks.selectcitymodule; 2 | 3 | import android.os.Bundle; 4 | import android.support.v7.app.AppCompatActivity; 5 | import android.support.v7.widget.Toolbar; 6 | import android.view.View; 7 | import android.widget.AdapterView; 8 | import android.widget.AdapterView.OnItemClickListener; 9 | import android.widget.ListView; 10 | import android.widget.TextView; 11 | import android.widget.Toast; 12 | 13 | import com.socks.selectcitymodule.view.CharacterParser; 14 | import com.socks.selectcitymodule.view.CitySortModel; 15 | import com.socks.selectcitymodule.view.PinyinComparator; 16 | import com.socks.selectcitymodule.view.SideBar; 17 | 18 | import java.util.ArrayList; 19 | import java.util.Collections; 20 | import java.util.List; 21 | 22 | public class MainActivity extends AppCompatActivity { 23 | 24 | private ListView sortListView; 25 | private SideBar sideBar; 26 | private TextView dialog; 27 | private SortAdapter adapter; 28 | private CharacterParser characterParser; 29 | private List SourceDateList; 30 | 31 | private Toolbar toolbar; 32 | 33 | @Override 34 | protected void onCreate(Bundle savedInstanceState) { 35 | super.onCreate(savedInstanceState); 36 | setContentView(R.layout.activity_main); 37 | 38 | toolbar = (Toolbar) findViewById(R.id.toolbar); 39 | setSupportActionBar(toolbar); 40 | toolbar.setTitle("选择城市"); 41 | 42 | initViews(); 43 | } 44 | 45 | private void initViews() { 46 | characterParser = CharacterParser.getInstance(); 47 | 48 | sideBar = (SideBar) findViewById(R.id.sidrbar); 49 | dialog = (TextView) findViewById(R.id.dialog); 50 | sideBar.setTextView(dialog); 51 | sideBar.setOnTouchingLetterChangedListener(new SideBar.OnTouchingLetterChangedListener() { 52 | 53 | @Override 54 | public void onTouchingLetterChanged(String s) { 55 | int position = adapter.getPositionForSection(s.charAt(0)); 56 | if (position != -1) { 57 | sortListView.setSelection(position + 1); 58 | } 59 | 60 | } 61 | }); 62 | 63 | sortListView = (ListView) findViewById(R.id.country_lvcountry); 64 | sortListView.setOnItemClickListener(new OnItemClickListener() { 65 | 66 | @Override 67 | public void onItemClick(AdapterView parent, View view, 68 | int position, long id) { 69 | Toast.makeText(getApplication(), 70 | ((CitySortModel) adapter.getItem(position - 1)).getName(), 71 | Toast.LENGTH_SHORT).show(); 72 | } 73 | }); 74 | 75 | SourceDateList = filledData(getResources().getStringArray(R.array.provinces)); 76 | Collections.sort(SourceDateList, new PinyinComparator()); 77 | adapter = new SortAdapter(this, SourceDateList); 78 | sortListView.addHeaderView(initHeadView()); 79 | sortListView.setAdapter(adapter); 80 | } 81 | 82 | 83 | private View initHeadView() { 84 | View headView = getLayoutInflater().inflate(R.layout.item_select_city, null); 85 | TextView tv_catagory = (TextView) headView.findViewById(R.id.tv_catagory); 86 | TextView tv_city_name = (TextView) headView.findViewById(R.id.tv_city_name); 87 | tv_catagory.setText("自动定位"); 88 | tv_city_name.setText("北京"); 89 | tv_city_name.setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(R.drawable.ic_city_location), null, null, null); 90 | tv_city_name.setCompoundDrawablePadding(24); 91 | return headView; 92 | } 93 | 94 | private List filledData(String[] date) { 95 | List mSortList = new ArrayList<>(); 96 | ArrayList indexString = new ArrayList<>(); 97 | 98 | for (int i = 0; i < date.length; i++) { 99 | CitySortModel sortModel = new CitySortModel(); 100 | sortModel.setName(date[i]); 101 | String pinyin = characterParser.getSelling(date[i]); 102 | String sortString = pinyin.substring(0, 1).toUpperCase(); 103 | if (sortString.matches("[A-Z]")) { 104 | 105 | //对重庆多音字做特殊处理 106 | if (pinyin.startsWith("zhongqing")) { 107 | sortString = "C"; 108 | sortModel.setSortLetters("C"); 109 | } else { 110 | sortModel.setSortLetters(sortString.toUpperCase()); 111 | } 112 | 113 | if (!indexString.contains(sortString)) { 114 | indexString.add(sortString); 115 | } 116 | } 117 | 118 | mSortList.add(sortModel); 119 | } 120 | Collections.sort(indexString); 121 | sideBar.setIndexText(indexString); 122 | return mSortList; 123 | 124 | } 125 | 126 | } 127 | -------------------------------------------------------------------------------- /app/src/main/java/com/socks/selectcitymodule/SortAdapter.java: -------------------------------------------------------------------------------- 1 | package com.socks.selectcitymodule; 2 | 3 | import android.content.Context; 4 | import android.view.LayoutInflater; 5 | import android.view.View; 6 | import android.view.ViewGroup; 7 | import android.widget.BaseAdapter; 8 | import android.widget.SectionIndexer; 9 | import android.widget.TextView; 10 | 11 | import com.socks.selectcitymodule.view.CitySortModel; 12 | 13 | import java.util.List; 14 | 15 | public class SortAdapter extends BaseAdapter implements SectionIndexer { 16 | private List list = null; 17 | private Context mContext; 18 | 19 | public SortAdapter(Context mContext, List list) { 20 | this.mContext = mContext; 21 | this.list = list; 22 | } 23 | 24 | public int getCount() { 25 | return this.list.size(); 26 | } 27 | 28 | public Object getItem(int position) { 29 | return list.get(position); 30 | } 31 | 32 | public long getItemId(int position) { 33 | return position; 34 | } 35 | 36 | public View getView(final int position, View view, ViewGroup arg2) { 37 | ViewHolder viewHolder = null; 38 | final CitySortModel mContent = list.get(position); 39 | if (view == null) { 40 | viewHolder = new ViewHolder(); 41 | view = LayoutInflater.from(mContext).inflate(R.layout.item_select_city, null); 42 | viewHolder.tvTitle = (TextView) view.findViewById(R.id.tv_city_name); 43 | viewHolder.tvLetter = (TextView) view.findViewById(R.id.tv_catagory); 44 | view.setTag(viewHolder); 45 | } else { 46 | viewHolder = (ViewHolder) view.getTag(); 47 | } 48 | 49 | int section = getSectionForPosition(position); 50 | 51 | if (position == getPositionForSection(section)) { 52 | viewHolder.tvLetter.setVisibility(View.VISIBLE); 53 | viewHolder.tvLetter.setText(mContent.getSortLetters()); 54 | } else { 55 | viewHolder.tvLetter.setVisibility(View.GONE); 56 | } 57 | 58 | viewHolder.tvTitle.setText(this.list.get(position).getName()); 59 | 60 | return view; 61 | 62 | } 63 | 64 | 65 | final static class ViewHolder { 66 | TextView tvLetter; 67 | TextView tvTitle; 68 | } 69 | 70 | 71 | public int getSectionForPosition(int position) { 72 | return list.get(position).getSortLetters().charAt(0); 73 | } 74 | 75 | 76 | public int getPositionForSection(int section) { 77 | for (int i = 0; i < getCount(); i++) { 78 | String sortStr = list.get(i).getSortLetters(); 79 | char firstChar = sortStr.toUpperCase().charAt(0); 80 | if (firstChar == section) { 81 | return i; 82 | } 83 | } 84 | 85 | return -1; 86 | } 87 | 88 | 89 | @Override 90 | public Object[] getSections() { 91 | return null; 92 | } 93 | } -------------------------------------------------------------------------------- /app/src/main/java/com/socks/selectcitymodule/view/CharacterParser.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Filename CharacterParser.java 3 | * Company �Ϻ�����-�ֶ��ֹ�˾�� 4 | * @author LuRuihui 5 | * @version 0.1 6 | */ 7 | package com.socks.selectcitymodule.view; 8 | 9 | public class CharacterParser { 10 | private static int[] pyvalue = new int[]{-20319, -20317, -20304, -20295, -20292, -20283, -20265, -20257, -20242, -20230, -20051, -20036, -20032, 11 | -20026, -20002, -19990, -19986, -19982, -19976, -19805, -19784, -19775, -19774, -19763, -19756, -19751, -19746, -19741, -19739, -19728, 12 | -19725, -19715, -19540, -19531, -19525, -19515, -19500, -19484, -19479, -19467, -19289, -19288, -19281, -19275, -19270, -19263, -19261, 13 | -19249, -19243, -19242, -19238, -19235, -19227, -19224, -19218, -19212, -19038, -19023, -19018, -19006, -19003, -18996, -18977, -18961, 14 | -18952, -18783, -18774, -18773, -18763, -18756, -18741, -18735, -18731, -18722, -18710, -18697, -18696, -18526, -18518, -18501, -18490, 15 | -18478, -18463, -18448, -18447, -18446, -18239, -18237, -18231, -18220, -18211, -18201, -18184, -18183, -18181, -18012, -17997, -17988, 16 | -17970, -17964, -17961, -17950, -17947, -17931, -17928, -17922, -17759, -17752, -17733, -17730, -17721, -17703, -17701, -17697, -17692, 17 | -17683, -17676, -17496, -17487, -17482, -17468, -17454, -17433, -17427, -17417, -17202, -17185, -16983, -16970, -16942, -16915, -16733, 18 | -16708, -16706, -16689, -16664, -16657, -16647, -16474, -16470, -16465, -16459, -16452, -16448, -16433, -16429, -16427, -16423, -16419, 19 | -16412, -16407, -16403, -16401, -16393, -16220, -16216, -16212, -16205, -16202, -16187, -16180, -16171, -16169, -16158, -16155, -15959, 20 | -15958, -15944, -15933, -15920, -15915, -15903, -15889, -15878, -15707, -15701, -15681, -15667, -15661, -15659, -15652, -15640, -15631, 21 | -15625, -15454, -15448, -15436, -15435, -15419, -15416, -15408, -15394, -15385, -15377, -15375, -15369, -15363, -15362, -15183, -15180, 22 | -15165, -15158, -15153, -15150, -15149, -15144, -15143, -15141, -15140, -15139, -15128, -15121, -15119, -15117, -15110, -15109, -14941, 23 | -14937, -14933, -14930, -14929, -14928, -14926, -14922, -14921, -14914, -14908, -14902, -14894, -14889, -14882, -14873, -14871, -14857, 24 | -14678, -14674, -14670, -14668, -14663, -14654, -14645, -14630, -14594, -14429, -14407, -14399, -14384, -14379, -14368, -14355, -14353, 25 | -14345, -14170, -14159, -14151, -14149, -14145, -14140, -14137, -14135, -14125, -14123, -14122, -14112, -14109, -14099, -14097, -14094, 26 | -14092, -14090, -14087, -14083, -13917, -13914, -13910, -13907, -13906, -13905, -13896, -13894, -13878, -13870, -13859, -13847, -13831, 27 | -13658, -13611, -13601, -13406, -13404, -13400, -13398, -13395, -13391, -13387, -13383, -13367, -13359, -13356, -13343, -13340, -13329, 28 | -13326, -13318, -13147, -13138, -13120, -13107, -13096, -13095, -13091, -13076, -13068, -13063, -13060, -12888, -12875, -12871, -12860, 29 | -12858, -12852, -12849, -12838, -12831, -12829, -12812, -12802, -12607, -12597, -12594, -12585, -12556, -12359, -12346, -12320, -12300, 30 | -12120, -12099, -12089, -12074, -12067, -12058, -12039, -11867, -11861, -11847, -11831, -11798, -11781, -11604, -11589, -11536, -11358, 31 | -11340, -11339, -11324, -11303, -11097, -11077, -11067, -11055, -11052, -11045, -11041, -11038, -11024, -11020, -11019, -11018, -11014, 32 | -10838, -10832, -10815, -10800, -10790, -10780, -10764, -10587, -10544, -10533, -10519, -10331, -10329, -10328, -10322, -10315, -10309, 33 | -10307, -10296, -10281, -10274, -10270, -10262, -10260, -10256, -10254}; 34 | public static String[] pystr = new String[]{"a", "ai", "an", "ang", "ao", "ba", "bai", "ban", "bang", "bao", "bei", "ben", "beng", "bi", "bian", 35 | "biao", "bie", "bin", "bing", "bo", "bu", "ca", "cai", "can", "cang", "cao", "ce", "ceng", "cha", "chai", "chan", "chang", "chao", "che", 36 | "chen", "cheng", "chi", "chong", "chou", "chu", "chuai", "chuan", "chuang", "chui", "chun", "chuo", "ci", "cong", "cou", "cu", "cuan", 37 | "cui", "cun", "cuo", "da", "dai", "dan", "dang", "dao", "de", "deng", "di", "dian", "diao", "die", "ding", "diu", "dong", "dou", "du", 38 | "duan", "dui", "dun", "duo", "e", "en", "er", "fa", "fan", "fang", "fei", "fen", "feng", "fo", "fou", "fu", "ga", "gai", "gan", "gang", 39 | "gao", "ge", "gei", "gen", "geng", "gong", "gou", "gu", "gua", "guai", "guan", "guang", "gui", "gun", "guo", "ha", "hai", "han", "hang", 40 | "hao", "he", "hei", "hen", "heng", "hong", "hou", "hu", "hua", "huai", "huan", "huang", "hui", "hun", "huo", "ji", "jia", "jian", 41 | "jiang", "jiao", "jie", "jin", "jing", "jiong", "jiu", "ju", "juan", "jue", "jun", "ka", "kai", "kan", "kang", "kao", "ke", "ken", 42 | "keng", "kong", "kou", "ku", "kua", "kuai", "kuan", "kuang", "kui", "kun", "kuo", "la", "lai", "lan", "lang", "lao", "le", "lei", "leng", 43 | "li", "lia", "lian", "liang", "liao", "lie", "lin", "ling", "liu", "long", "lou", "lu", "lv", "luan", "lue", "lun", "luo", "ma", "mai", 44 | "man", "mang", "mao", "me", "mei", "men", "meng", "mi", "mian", "miao", "mie", "min", "ming", "miu", "mo", "mou", "mu", "na", "nai", 45 | "nan", "nang", "nao", "ne", "nei", "nen", "neng", "ni", "nian", "niang", "niao", "nie", "nin", "ning", "niu", "nong", "nu", "nv", "nuan", 46 | "nue", "nuo", "o", "ou", "pa", "pai", "pan", "pang", "pao", "pei", "pen", "peng", "pi", "pian", "piao", "pie", "pin", "ping", "po", "pu", 47 | "qi", "qia", "qian", "qiang", "qiao", "qie", "qin", "qing", "qiong", "qiu", "qu", "quan", "que", "qun", "ran", "rang", "rao", "re", 48 | "ren", "reng", "ri", "rong", "rou", "ru", "ruan", "rui", "run", "ruo", "sa", "sai", "san", "sang", "sao", "se", "sen", "seng", "sha", 49 | "shai", "shan", "shang", "shao", "she", "shen", "sheng", "shi", "shou", "shu", "shua", "shuai", "shuan", "shuang", "shui", "shun", 50 | "shuo", "si", "song", "sou", "su", "suan", "sui", "sun", "suo", "ta", "tai", "tan", "tang", "tao", "te", "teng", "ti", "tian", "tiao", 51 | "tie", "ting", "tong", "tou", "tu", "tuan", "tui", "tun", "tuo", "wa", "wai", "wan", "wang", "wei", "wen", "weng", "wo", "wu", "xi", 52 | "xia", "xian", "xiang", "xiao", "xie", "xin", "xing", "xiong", "xiu", "xu", "xuan", "xue", "xun", "ya", "yan", "yang", "yao", "ye", "yi", 53 | "yin", "ying", "yo", "yong", "you", "yu", "yuan", "yue", "yun", "za", "zai", "zan", "zang", "zao", "ze", "zei", "zen", "zeng", "zha", 54 | "zhai", "zhan", "zhang", "zhao", "zhe", "zhen", "zheng", "zhi", "zhong", "zhou", "zhu", "zhua", "zhuai", "zhuan", "zhuang", "zhui", 55 | "zhun", "zhuo", "zi", "zong", "zou", "zu", "zuan", "zui", "zun", "zuo"}; 56 | private StringBuilder buffer; 57 | private String resource; 58 | private static CharacterParser characterParser = new CharacterParser(); 59 | 60 | public static CharacterParser getInstance() { 61 | return characterParser; 62 | } 63 | 64 | public String getResource() { 65 | return resource; 66 | } 67 | 68 | public void setResource(String resource) { 69 | this.resource = resource; 70 | } 71 | 72 | private int getChsAscii(String chs) { 73 | int asc = 0; 74 | try { 75 | byte[] bytes = chs.getBytes("gb2312"); 76 | if (bytes == null || bytes.length > 2 || bytes.length <= 0) { 77 | throw new RuntimeException("illegal resource string"); 78 | } 79 | if (bytes.length == 1) { 80 | asc = bytes[0]; 81 | } 82 | if (bytes.length == 2) { 83 | int hightByte = 256 + bytes[0]; 84 | int lowByte = 256 + bytes[1]; 85 | asc = (256 * hightByte + lowByte) - 256 * 256; 86 | } 87 | } catch (Exception e) { 88 | System.out.println("ERROR:ChineseSpelling.class-getChsAscii(String chs)" + e); 89 | } 90 | return asc; 91 | } 92 | 93 | public String convert(String str) { 94 | String result = null; 95 | int ascii = getChsAscii(str); 96 | if (ascii > 0 && ascii < 160) { 97 | result = String.valueOf((char) ascii); 98 | } else { 99 | for (int i = (pyvalue.length - 1); i >= 0; i--) { 100 | if (pyvalue[i] <= ascii) { 101 | result = pystr[i]; 102 | break; 103 | } 104 | } 105 | } 106 | return result; 107 | } 108 | 109 | public String getSelling(String chs) { 110 | String key, value; 111 | buffer = new StringBuilder(); 112 | for (int i = 0; i < chs.length(); i++) { 113 | key = chs.substring(i, i + 1); 114 | if (key.getBytes().length >= 2) { 115 | value = (String) convert(key); 116 | if (value == null) { 117 | value = "unknown"; 118 | } 119 | } else { 120 | value = key; 121 | } 122 | buffer.append(value); 123 | } 124 | return buffer.toString(); 125 | } 126 | 127 | } 128 | -------------------------------------------------------------------------------- /app/src/main/java/com/socks/selectcitymodule/view/CitySortModel.java: -------------------------------------------------------------------------------- 1 | package com.socks.selectcitymodule.view; 2 | 3 | public class CitySortModel { 4 | 5 | private String name; 6 | private String sortLetters; 7 | 8 | public String getName() { 9 | return name; 10 | } 11 | 12 | public void setName(String name) { 13 | this.name = name; 14 | } 15 | 16 | public String getSortLetters() { 17 | return sortLetters; 18 | } 19 | 20 | public void setSortLetters(String sortLetters) { 21 | this.sortLetters = sortLetters; 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /app/src/main/java/com/socks/selectcitymodule/view/PinyinComparator.java: -------------------------------------------------------------------------------- 1 | package com.socks.selectcitymodule.view; 2 | 3 | import java.util.Comparator; 4 | 5 | public class PinyinComparator implements Comparator { 6 | 7 | public int compare(CitySortModel o1, CitySortModel o2) { 8 | if (o1.getSortLetters().equals("@") 9 | || o2.getSortLetters().equals("#")) { 10 | return -1; 11 | } else if (o1.getSortLetters().equals("#") 12 | || o2.getSortLetters().equals("@")) { 13 | return 1; 14 | } else { 15 | return o1.getSortLetters().compareTo(o2.getSortLetters()); 16 | } 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /app/src/main/java/com/socks/selectcitymodule/view/SideBar.java: -------------------------------------------------------------------------------- 1 | package com.socks.selectcitymodule.view; 2 | 3 | import android.content.Context; 4 | import android.graphics.Canvas; 5 | import android.graphics.Color; 6 | import android.graphics.Paint; 7 | import android.graphics.Typeface; 8 | import android.util.AttributeSet; 9 | import android.view.MotionEvent; 10 | import android.view.View; 11 | import android.widget.TextView; 12 | 13 | import java.util.ArrayList; 14 | import java.util.Arrays; 15 | import java.util.List; 16 | 17 | public class SideBar extends View { 18 | 19 | public static String[] INDEX_STRING = {"A", "B", "C", "D", "E", "F", "G", "H", "I", 20 | "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", 21 | "W", "X", "Y", "Z"}; 22 | 23 | private OnTouchingLetterChangedListener onTouchingLetterChangedListener; 24 | private List indexStrings; 25 | private int choose = -1; 26 | private Paint paint = new Paint(); 27 | private TextView mTextDialog; 28 | 29 | public SideBar(Context context) { 30 | this(context, null); 31 | } 32 | 33 | public SideBar(Context context, AttributeSet attrs) { 34 | this(context, attrs, 0); 35 | } 36 | 37 | public SideBar(Context context, AttributeSet attrs, int defStyle) { 38 | super(context, attrs, defStyle); 39 | init(); 40 | } 41 | 42 | private void init() { 43 | setBackgroundColor(Color.WHITE); 44 | indexStrings = Arrays.asList(INDEX_STRING); 45 | } 46 | 47 | protected void onDraw(Canvas canvas) { 48 | super.onDraw(canvas); 49 | int height = getHeight(); 50 | int width = getWidth(); 51 | int singleHeight = height / indexStrings.size(); 52 | 53 | for (int i = 0; i < indexStrings.size(); i++) { 54 | paint.setColor(Color.parseColor("#00BFFF")); 55 | paint.setTypeface(Typeface.DEFAULT_BOLD); 56 | paint.setAntiAlias(true); 57 | paint.setTextSize(24); 58 | if (i == choose) { 59 | paint.setColor(Color.parseColor("#3399ff")); 60 | paint.setFakeBoldText(true); 61 | } 62 | 63 | float xPos = width / 2 - paint.measureText(indexStrings.get(i)) / 2; 64 | float yPos = singleHeight * i + singleHeight; 65 | canvas.drawText(indexStrings.get(i), xPos, yPos, paint); 66 | paint.reset(); 67 | } 68 | 69 | } 70 | 71 | @Override 72 | public boolean dispatchTouchEvent(MotionEvent event) { 73 | final int action = event.getAction(); 74 | final float y = event.getY(); 75 | final int oldChoose = choose; 76 | final OnTouchingLetterChangedListener listener = onTouchingLetterChangedListener; 77 | final int c = (int) (y / getHeight() * indexStrings.size()); 78 | 79 | switch (action) { 80 | case MotionEvent.ACTION_UP: 81 | setBackgroundColor(Color.WHITE); 82 | choose = -1; 83 | invalidate(); 84 | if (mTextDialog != null) { 85 | mTextDialog.setVisibility(View.GONE); 86 | } 87 | break; 88 | default: 89 | if (oldChoose != c) { 90 | if (c >= 0 && c < indexStrings.size()) { 91 | if (listener != null) { 92 | listener.onTouchingLetterChanged(indexStrings.get(c)); 93 | } 94 | if (mTextDialog != null) { 95 | mTextDialog.setText(indexStrings.get(c)); 96 | mTextDialog.setVisibility(View.VISIBLE); 97 | } 98 | 99 | choose = c; 100 | invalidate(); 101 | } 102 | } 103 | 104 | break; 105 | } 106 | return true; 107 | } 108 | 109 | public void setIndexText(ArrayList indexStrings) { 110 | this.indexStrings = indexStrings; 111 | invalidate(); 112 | } 113 | 114 | 115 | public void setTextView(TextView mTextDialog) { 116 | this.mTextDialog = mTextDialog; 117 | } 118 | 119 | public void setOnTouchingLetterChangedListener( 120 | OnTouchingLetterChangedListener onTouchingLetterChangedListener) { 121 | this.onTouchingLetterChangedListener = onTouchingLetterChangedListener; 122 | } 123 | 124 | public interface OnTouchingLetterChangedListener { 125 | void onTouchingLetterChanged(String s); 126 | } 127 | 128 | } -------------------------------------------------------------------------------- /app/src/main/res/drawable-hdpi/ic_city_location.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZhaoKaiQiang/SelectCityModule/3481e71e2ba7121e9198cd4c48e56c15b82723ff/app/src/main/res/drawable-hdpi/ic_city_location.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xhdpi/ic_city_location.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZhaoKaiQiang/SelectCityModule/3481e71e2ba7121e9198cd4c48e56c15b82723ff/app/src/main/res/drawable-xhdpi/ic_city_location.png -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 13 | 14 | 17 | 18 | 25 | 26 | 38 | 39 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /app/src/main/res/layout/item_select_city.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 19 | 20 | 29 | 30 | 34 | 35 | -------------------------------------------------------------------------------- /app/src/main/res/menu/menu_main.xml: -------------------------------------------------------------------------------- 1 | 4 | 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZhaoKaiQiang/SelectCityModule/3481e71e2ba7121e9198cd4c48e56c15b82723ff/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZhaoKaiQiang/SelectCityModule/3481e71e2ba7121e9198cd4c48e56c15b82723ff/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZhaoKaiQiang/SelectCityModule/3481e71e2ba7121e9198cd4c48e56c15b82723ff/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZhaoKaiQiang/SelectCityModule/3481e71e2ba7121e9198cd4c48e56c15b82723ff/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ZhaoKaiQiang/SelectCityModule/3481e71e2ba7121e9198cd4c48e56c15b82723ff/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/values-v21/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 10 | 14 |