GeezerVison is showing github.com — 添加通讯录索引效果 · MasterLing/AndroidCustomView@20c13f1
File treeExpand file treeCollapse file tree Open diff view settings java/com/allen/androidcustomview
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: app/build.gradle +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -30,4 +30,5 @@ dependencies { | 30 | 30 | compile 'com.android.support.constraint:constraint-layout:1.0.2' | 31 | 31 | compile 'com.android.support:recyclerview-v7:24.0.0-alpha1' | 32 | 32 | testCompile 'junit:junit:4.12' | | 33 | + compile 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.34' | 33 | 34 | } |
Collapse file Copy file name to clipboardExpand all lines: app/src/main/AndroidManifest.xml +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 33 | 33 | <activity android:name=".activity.DragBallActivity" /> | 34 | 34 | <activity android:name=".activity.FingerprintActivity" /> | 35 | 35 | <activity android:name=".activity.BannerActivity" /> | | 36 | + <activity android:name=".activity.HoverItemActivity"/> | 36 | 37 | </application> | 37 | 38 |
| 38 | 39 | </manifest> |
Collapse file Copy file name to clipboard +151Lines changed: 151 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +package com.allen.androidcustomview.activity; | | 2 | + | | 3 | +import android.os.Bundle; | | 4 | +import android.support.annotation.Nullable; | | 5 | +import android.support.v7.app.AppCompatActivity; | | 6 | +import android.support.v7.widget.LinearLayoutManager; | | 7 | +import android.support.v7.widget.RecyclerView; | | 8 | +import android.util.Log; | | 9 | +import android.widget.TextView; | | 10 | + | | 11 | +import com.allen.androidcustomview.R; | | 12 | +import com.allen.androidcustomview.adapter.HoverAdapter; | | 13 | +import com.allen.androidcustomview.bean.UserBean; | | 14 | +import com.allen.androidcustomview.utils.CharacterParser; | | 15 | +import com.allen.androidcustomview.utils.PinyinComparator; | | 16 | +import com.allen.androidcustomview.widget.HoverItemDecoration; | | 17 | +import com.allen.androidcustomview.widget.IndexView; | | 18 | + | | 19 | +import java.util.ArrayList; | | 20 | +import java.util.Collections; | | 21 | +import java.util.List; | | 22 | + | | 23 | +/** | | 24 | + * <pre> | | 25 | + * @author : xiaoyao | | 26 | + | | 27 | + * date : 2018/04/16 | | 28 | + * desc : | | 29 | + * version : 1.0 | | 30 | + * </pre> | | 31 | + */ | | 32 | + | | 33 | +public class HoverItemActivity extends AppCompatActivity { | | 34 | + private RecyclerView recyclerView; | | 35 | + private IndexView indexView; | | 36 | + private TextView showTextDialog; | | 37 | + | | 38 | + private HoverAdapter adapter; | | 39 | + | | 40 | + private List<UserBean> userBeans = new ArrayList<>(); | | 41 | + | | 42 | + private String[] names = new String[]{"阿妹","打黑牛","张三","李四","王五","田鸡","孙五"}; | | 43 | + | | 44 | + /** | | 45 | + * 汉字转换成拼音的类 | | 46 | + */ | | 47 | + private CharacterParser characterParser; | | 48 | + /** | | 49 | + * 根据拼音来排列ListView里面的数据类 | | 50 | + */ | | 51 | + private PinyinComparator pinyinComparator; | | 52 | + | | 53 | + private LinearLayoutManager layoutManager; | | 54 | + @Override | | 55 | + protected void onCreate(@Nullable Bundle savedInstanceState) { | | 56 | + super.onCreate(savedInstanceState); | | 57 | + setContentView(R.layout.activity_hover_item); | | 58 | + | | 59 | + characterParser = CharacterParser.getInstance(); | | 60 | + pinyinComparator = new PinyinComparator(); | | 61 | + | | 62 | + | | 63 | + userBeans = filledData(getData()); | | 64 | + | | 65 | + recyclerView = (RecyclerView) findViewById(R.id.recycler_view); | | 66 | + indexView = (IndexView) findViewById(R.id.index_view); | | 67 | + showTextDialog = (TextView) findViewById(R.id.show_text_dialog); | | 68 | + | | 69 | + layoutManager=new LinearLayoutManager(this); | | 70 | + recyclerView.setLayoutManager(layoutManager); | | 71 | + recyclerView.addItemDecoration(new HoverItemDecoration(this, new HoverItemDecoration.BindItemTextCallback() { | | 72 | + @Override | | 73 | + public String getItemText(int position) { | | 74 | + return userBeans.get(position).getSortLetters(); | | 75 | + } | | 76 | + })); | | 77 | + | | 78 | + adapter = new HoverAdapter(userBeans); | | 79 | + | | 80 | + recyclerView.setAdapter(adapter); | | 81 | + | | 82 | + initIndexView(); | | 83 | + } | | 84 | + | | 85 | + /** | | 86 | + * 初始化右边字幕索引view | | 87 | + */ | | 88 | + private void initIndexView() { | | 89 | + indexView.setShowTextDialog(showTextDialog); | | 90 | + indexView.setOnTouchingLetterChangedListener(new IndexView.OnTouchingLetterChangedListener() { | | 91 | + @Override | | 92 | + public void onTouchingLetterChanged(String letter) { | | 93 | + // 该字母首次出现的位置 | | 94 | + int position = getPositionForSection(letter); | | 95 | + if (position != -1) { | | 96 | + layoutManager.scrollToPositionWithOffset(position,0); | | 97 | + layoutManager.setStackFromEnd(false); | | 98 | + } | | 99 | + } | | 100 | + }); | | 101 | + } | | 102 | + | | 103 | + public int getPositionForSection(String section) { | | 104 | + for (int i = 0; i < userBeans.size(); i++) { | | 105 | + String sortStr = userBeans.get(i).getSortLetters(); | | 106 | + if (sortStr.equals(section)) { | | 107 | + return i; | | 108 | + } | | 109 | + } | | 110 | + return -1; | | 111 | + } | | 112 | + | | 113 | + private List<UserBean> getData() { | | 114 | + List<UserBean> userBeans = new ArrayList<>(); | | 115 | + for (int i = 0; i < 50; i++) { | | 116 | + UserBean userBean = new UserBean(); | | 117 | + userBean.setUserName(names[i%7]); | | 118 | + userBeans.add(userBean); | | 119 | + } | | 120 | + | | 121 | + return userBeans; | | 122 | + } | | 123 | + | | 124 | + private List<UserBean> filledData(List<UserBean> sortList) { | | 125 | + | | 126 | + for (int i = 0; i < sortList.size(); i++) { | | 127 | + | | 128 | + if ("".equals(sortList.get(i).getUserName())) { | | 129 | + sortList.get(i).setSortLetters("#"); | | 130 | + } else { | | 131 | + // 汉字转换成拼音 | | 132 | + String pinyin = characterParser.getSelling(sortList.get(i).getUserName()); | | 133 | + String sortString = pinyin.substring(0, 1).toUpperCase(); | | 134 | + | | 135 | + // 正则表达式,判断首字母是否是英文字母 | | 136 | + if (sortString.matches("[A-Z]")) { | | 137 | + sortList.get(i).setSortLetters(sortString.toUpperCase()); | | 138 | + } else { | | 139 | + sortList.get(i).setSortLetters("#"); | | 140 | + } | | 141 | + } | | 142 | + | | 143 | + } | | 144 | + | | 145 | + // 根据a-z进行排序源数据 | | 146 | + Collections.sort(sortList, pinyinComparator); | | 147 | + | | 148 | + return sortList; | | 149 | + } | | 150 | + | | 151 | +} |
Collapse file Copy file name to clipboardExpand all lines: app/src/main/java/com/allen/androidcustomview/activity/MainActivity.java +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -115,7 +115,7 @@ public void onClick(View v) { | 115 | 115 | bannerBtn.setOnClickListener(new View.OnClickListener() { | 116 | 116 | @Override | 117 | 117 | public void onClick(View v) { | 118 | | - startActivity(new Intent(MainActivity.this, BannerActivity.class)); | | 118 | + startActivity(new Intent(MainActivity.this, HoverItemActivity.class)); | 119 | 119 | } | 120 | 120 | }); | 121 | 121 |
| |
Collapse file Copy file name to clipboard +33Lines changed: 33 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +package com.allen.androidcustomview.adapter; | | 2 | + | | 3 | +import android.support.annotation.Nullable; | | 4 | + | | 5 | +import com.allen.androidcustomview.R; | | 6 | +import com.allen.androidcustomview.bean.UserBean; | | 7 | +import com.chad.library.adapter.base.BaseQuickAdapter; | | 8 | +import com.chad.library.adapter.base.BaseViewHolder; | | 9 | + | | 10 | +import java.util.List; | | 11 | + | | 12 | +/** | | 13 | + * <pre> | | 14 | + * @author : xiaoyao | | 15 | + | | 16 | + * date : 2018/04/16 | | 17 | + * desc : | | 18 | + * version : 1.0 | | 19 | + * </pre> | | 20 | + */ | | 21 | + | | 22 | +public class HoverAdapter extends BaseQuickAdapter<UserBean,BaseViewHolder> { | | 23 | + | | 24 | + public HoverAdapter( @Nullable List<UserBean> data) { | | 25 | + super(R.layout.adapter_item_hover_user, data); | | 26 | + } | | 27 | + | | 28 | + @Override | | 29 | + protected void convert(BaseViewHolder helper, UserBean item) { | | 30 | + helper.setText(R.id.user_name_tv,item.getUserName()); | | 31 | + } | | 32 | + | | 33 | +} |
Collapse file Copy file name to clipboard +33Lines changed: 33 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +package com.allen.androidcustomview.bean; | | 2 | + | | 3 | +/** | | 4 | + * <pre> | | 5 | + * @author : xiaoyao | | 6 | + | | 7 | + * date : 2018/04/16 | | 8 | + * desc : | | 9 | + * version : 1.0 | | 10 | + * </pre> | | 11 | + */ | | 12 | + | | 13 | +public class UserBean { | | 14 | + private String userName; | | 15 | + private String sortLetters=""; | | 16 | + | | 17 | + | | 18 | + public String getUserName() { | | 19 | + return userName; | | 20 | + } | | 21 | + | | 22 | + public void setUserName(String userName) { | | 23 | + this.userName = userName; | | 24 | + } | | 25 | + | | 26 | + public String getSortLetters() { | | 27 | + return sortLetters; | | 28 | + } | | 29 | + | | 30 | + public void setSortLetters(String sortLetters) { | | 31 | + this.sortLetters = sortLetters; | | 32 | + } | | 33 | +} |
Collapse file Copy file name to clipboard +131Lines changed: 131 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +package com.allen.androidcustomview.utils; | | 2 | + | | 3 | +/** | | 4 | + * Java汉字转换为拼音 | | 5 | + * | | 6 | + */ | | 7 | +public class CharacterParser { | | 8 | + private static int[] pyvalue = new int[] {-20319, -20317, -20304, -20295, -20292, -20283, -20265, -20257, -20242, -20230, -20051, -20036, -20032, | | 9 | + -20026, -20002, -19990, -19986, -19982, -19976, -19805, -19784, -19775, -19774, -19763, -19756, -19751, -19746, -19741, -19739, -19728, | | 10 | + -19725, -19715, -19540, -19531, -19525, -19515, -19500, -19484, -19479, -19467, -19289, -19288, -19281, -19275, -19270, -19263, -19261, | | 11 | + -19249, -19243, -19242, -19238, -19235, -19227, -19224, -19218, -19212, -19038, -19023, -19018, -19006, -19003, -18996, -18977, -18961, | | 12 | + -18952, -18783, -18774, -18773, -18763, -18756, -18741, -18735, -18731, -18722, -18710, -18697, -18696, -18526, -18518, -18501, -18490, | | 13 | + -18478, -18463, -18448, -18447, -18446, -18239, -18237, -18231, -18220, -18211, -18201, -18184, -18183, -18181, -18012, -17997, -17988, | | 14 | + -17970, -17964, -17961, -17950, -17947, -17931, -17928, -17922, -17759, -17752, -17733, -17730, -17721, -17703, -17701, -17697, -17692, | | 15 | + -17683, -17676, -17496, -17487, -17482, -17468, -17454, -17433, -17427, -17417, -17202, -17185, -16983, -16970, -16942, -16915, -16733, | | 16 | + -16708, -16706, -16689, -16664, -16657, -16647, -16474, -16470, -16465, -16459, -16452, -16448, -16433, -16429, -16427, -16423, -16419, | | 17 | + -16412, -16407, -16403, -16401, -16393, -16220, -16216, -16212, -16205, -16202, -16187, -16180, -16171, -16169, -16158, -16155, -15959, | | 18 | + -15958, -15944, -15933, -15920, -15915, -15903, -15889, -15878, -15707, -15701, -15681, -15667, -15661, -15659, -15652, -15640, -15631, | | 19 | + -15625, -15454, -15448, -15436, -15435, -15419, -15416, -15408, -15394, -15385, -15377, -15375, -15369, -15363, -15362, -15183, -15180, | | 20 | + -15165, -15158, -15153, -15150, -15149, -15144, -15143, -15141, -15140, -15139, -15128, -15121, -15119, -15117, -15110, -15109, -14941, | | 21 | + -14937, -14933, -14930, -14929, -14928, -14926, -14922, -14921, -14914, -14908, -14902, -14894, -14889, -14882, -14873, -14871, -14857, | | 22 | + -14678, -14674, -14670, -14668, -14663, -14654, -14645, -14630, -14594, -14429, -14407, -14399, -14384, -14379, -14368, -14355, -14353, | | 23 | + -14345, -14170, -14159, -14151, -14149, -14145, -14140, -14137, -14135, -14125, -14123, -14122, -14112, -14109, -14099, -14097, -14094, | | 24 | + -14092, -14090, -14087, -14083, -13917, -13914, -13910, -13907, -13906, -13905, -13896, -13894, -13878, -13870, -13859, -13847, -13831, | | 25 | + -13658, -13611, -13601, -13406, -13404, -13400, -13398, -13395, -13391, -13387, -13383, -13367, -13359, -13356, -13343, -13340, -13329, | | 26 | + -13326, -13318, -13147, -13138, -13120, -13107, -13096, -13095, -13091, -13076, -13068, -13063, -13060, -12888, -12875, -12871, -12860, | | 27 | + -12858, -12852, -12849, -12838, -12831, -12829, -12812, -12802, -12607, -12597, -12594, -12585, -12556, -12359, -12346, -12320, -12300, | | 28 | + -12120, -12099, -12089, -12074, -12067, -12058, -12039, -11867, -11861, -11847, -11831, -11798, -11781, -11604, -11589, -11536, -11358, | | 29 | + -11340, -11339, -11324, -11303, -11097, -11077, -11067, -11055, -11052, -11045, -11041, -11038, -11024, -11020, -11019, -11018, -11014, | | 30 | + -10838, -10832, -10815, -10800, -10790, -10780, -10764, -10587, -10544, -10533, -10519, -10331, -10329, -10328, -10322, -10315, -10309, | | 31 | + -10307, -10296, -10281, -10274, -10270, -10262, -10260, -10256, -10254}; | | 32 | + public static String[] pystr = new String[] {"a", "ai", "an", "ang", "ao", "ba", "bai", "ban", "bang", "bao", "bei", "ben", "beng", "bi", "bian", | | 33 | + "biao", "bie", "bin", "bing", "bo", "bu", "ca", "cai", "can", "cang", "cao", "ce", "ceng", "cha", "chai", "chan", "chang", "chao", "che", | | 34 | + "chen", "cheng", "chi", "chong", "chou", "chu", "chuai", "chuan", "chuang", "chui", "chun", "chuo", "ci", "cong", "cou", "cu", "cuan", | | 35 | + "cui", "cun", "cuo", "da", "dai", "dan", "dang", "dao", "de", "deng", "di", "dian", "diao", "die", "ding", "diu", "dong", "dou", "du", | | 36 | + "duan", "dui", "dun", "duo", "e", "en", "er", "fa", "fan", "fang", "fei", "fen", "feng", "fo", "fou", "fu", "ga", "gai", "gan", "gang", | | 37 | + "gao", "ge", "gei", "gen", "geng", "gong", "gou", "gu", "gua", "guai", "guan", "guang", "gui", "gun", "guo", "ha", "hai", "han", "hang", | | 38 | + "hao", "he", "hei", "hen", "heng", "hong", "hou", "hu", "hua", "huai", "huan", "huang", "hui", "hun", "huo", "ji", "jia", "jian", | | 39 | + "jiang", "jiao", "jie", "jin", "jing", "jiong", "jiu", "ju", "juan", "jue", "jun", "ka", "kai", "kan", "kang", "kao", "ke", "ken", | | 40 | + "keng", "kong", "kou", "ku", "kua", "kuai", "kuan", "kuang", "kui", "kun", "kuo", "la", "lai", "lan", "lang", "lao", "le", "lei", "leng", | | 41 | + "li", "lia", "lian", "liang", "liao", "lie", "lin", "ling", "liu", "long", "lou", "lu", "lv", "luan", "lue", "lun", "luo", "ma", "mai", | | 42 | + "man", "mang", "mao", "me", "mei", "men", "meng", "mi", "mian", "miao", "mie", "min", "ming", "miu", "mo", "mou", "mu", "na", "nai", | | 43 | + "nan", "nang", "nao", "ne", "nei", "nen", "neng", "ni", "nian", "niang", "niao", "nie", "nin", "ning", "niu", "nong", "nu", "nv", "nuan", | | 44 | + "nue", "nuo", "o", "ou", "pa", "pai", "pan", "pang", "pao", "pei", "pen", "peng", "pi", "pian", "piao", "pie", "pin", "ping", "po", "pu", | | 45 | + "qi", "qia", "qian", "qiang", "qiao", "qie", "qin", "qing", "qiong", "qiu", "qu", "quan", "que", "qun", "ran", "rang", "rao", "re", | | 46 | + "ren", "reng", "ri", "rong", "rou", "ru", "ruan", "rui", "run", "ruo", "sa", "sai", "san", "sang", "sao", "se", "sen", "seng", "sha", | | 47 | + "shai", "shan", "shang", "shao", "she", "shen", "sheng", "shi", "shou", "shu", "shua", "shuai", "shuan", "shuang", "shui", "shun", | | 48 | + "shuo", "si", "song", "sou", "su", "suan", "sui", "sun", "suo", "ta", "tai", "tan", "tang", "tao", "te", "teng", "ti", "tian", "tiao", | | 49 | + "tie", "ting", "tong", "tou", "tu", "tuan", "tui", "tun", "tuo", "wa", "wai", "wan", "wang", "wei", "wen", "weng", "wo", "wu", "xi", | | 50 | + "xia", "xian", "xiang", "xiao", "xie", "xin", "xing", "xiong", "xiu", "xu", "xuan", "xue", "xun", "ya", "yan", "yang", "yao", "ye", "yi", | | 51 | + "yin", "ying", "yo", "yong", "you", "yu", "yuan", "yue", "yun", "za", "zai", "zan", "zang", "zao", "ze", "zei", "zen", "zeng", "zha", | | 52 | + "zhai", "zhan", "zhang", "zhao", "zhe", "zhen", "zheng", "zhi", "zhong", "zhou", "zhu", "zhua", "zhuai", "zhuan", "zhuang", "zhui", | | 53 | + "zhun", "zhuo", "zi", "zong", "zou", "zu", "zuan", "zui", "zun", "zuo"}; | | 54 | + private StringBuilder buffer; | | 55 | + private String resource; | | 56 | + private static CharacterParser characterParser = new CharacterParser(); | | 57 | + | | 58 | + public static CharacterParser getInstance() { | | 59 | + return characterParser; | | 60 | + } | | 61 | + | | 62 | + public String getResource() { | | 63 | + return resource; | | 64 | + } | | 65 | + | | 66 | + public void setResource(String resource) { | | 67 | + this.resource = resource; | | 68 | + } | | 69 | + | | 70 | + /** * 汉字转成ASCII码 * * @param chs * @return */ | | 71 | + private int getChsAscii(String chs) { | | 72 | + int asc = 0; | | 73 | + try { | | 74 | + byte[] bytes = chs.getBytes("gb2312"); | | 75 | + if (bytes == null || bytes.length > 2 || bytes.length <= 0) { | | 76 | + throw new RuntimeException("illegal resource string"); | | 77 | + } | | 78 | + if (bytes.length == 1) { | | 79 | + asc = bytes[0]; | | 80 | + } | | 81 | + if (bytes.length == 2) { | | 82 | + int hightByte = 256 + bytes[0]; | | 83 | + int lowByte = 256 + bytes[1]; | | 84 | + asc = (256 * hightByte + lowByte) - 256 * 256; | | 85 | + } | | 86 | + } catch (Exception e) { | | 87 | + System.out.println("ERROR:ChineseSpelling.class-getChsAscii(String chs)" + e); | | 88 | + } | | 89 | + return asc; | | 90 | + } | | 91 | + | | 92 | + /** * 单字解析 * * @param str * @return */ | | 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 | + /** * 词组解析 * * @param chs * @return */ | | 110 | + public String getSelling(String chs) { | | 111 | + String key, value; | | 112 | + buffer = new StringBuilder(); | | 113 | + for (int i = 0; i < chs.length(); i++) { | | 114 | + key = chs.substring(i, i + 1); | | 115 | + if (key.getBytes().length >= 2) { | | 116 | + value = (String) convert(key); | | 117 | + if (value == null) { | | 118 | + value = "unknown"; | | 119 | + } | | 120 | + } else { | | 121 | + value = key; | | 122 | + } | | 123 | + buffer.append(value); | | 124 | + } | | 125 | + return buffer.toString(); | | 126 | + } | | 127 | + | | 128 | + public String getSpelling() { | | 129 | + return this.getSelling(this.getResource()); | | 130 | + } | | 131 | +} |
Collapse file Copy file name to clipboard +22Lines changed: 22 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +package com.allen.androidcustomview.utils; | | 2 | + | | 3 | + | | 4 | +import com.allen.androidcustomview.bean.UserBean; | | 5 | + | | 6 | +import java.util.Comparator; | | 7 | + | | 8 | +public class PinyinComparator implements Comparator<UserBean> { | | 9 | + | | 10 | + @Override | | 11 | + public int compare(UserBean o1, UserBean o2) { | | 12 | + if (o1.getSortLetters().equals("@") | | 13 | + || o2.getSortLetters().equals("#")) { | | 14 | + return -1; | | 15 | + } else if (o1.getSortLetters().equals("#") | | 16 | + || o2.getSortLetters().equals("@")) { | | 17 | + return 1; | | 18 | + } else { | | 19 | + return o1.getSortLetters().compareTo(o2.getSortLetters()); | | 20 | + } | | 21 | + } | | 22 | +} |
|