|
@@ -0,0 +1,128 @@
|
|
|
+package com.common.im.helper;
|
|
|
+
|
|
|
+import com.github.promeg.pinyinhelper.Pinyin;
|
|
|
+import com.mcxtzhang.indexlib.IndexBar.bean.BaseIndexPinyinBean;
|
|
|
+import com.mcxtzhang.indexlib.IndexBar.helper.IIndexBarDataHelper;
|
|
|
+
|
|
|
+import java.util.Collections;
|
|
|
+import java.util.Comparator;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+/**
|
|
|
+ * Author by pq, Date on 2022/9/29.
|
|
|
+ */
|
|
|
+public class CustomIndexBarDataHelperImpl implements IIndexBarDataHelper {
|
|
|
+ /**
|
|
|
+ * 如果需要,
|
|
|
+ * 字符->拼音,
|
|
|
+ *
|
|
|
+ * @param datas
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public IIndexBarDataHelper convert(List<? extends BaseIndexPinyinBean> datas) {
|
|
|
+ if (null == datas || datas.isEmpty()) {
|
|
|
+ return this;
|
|
|
+ }
|
|
|
+ int size = datas.size();
|
|
|
+ for (int i = 0; i < size; i++) {
|
|
|
+ BaseIndexPinyinBean indexPinyinBean = datas.get(i);
|
|
|
+ StringBuilder pySb = new StringBuilder();
|
|
|
+ //add by zhangxutong 2016 11 10 如果不是top 才转拼音,否则不用转了
|
|
|
+ if (indexPinyinBean.isNeedToPinyin()) {
|
|
|
+ String target = indexPinyinBean.getTarget();//取出需要被拼音化的字段
|
|
|
+ //遍历target的每个char得到它的全拼音
|
|
|
+ for (int i1 = 0; i1 < target.length(); i1++) {
|
|
|
+ //利用TinyPinyin将char转成拼音
|
|
|
+ //查看源码,方法内 如果char为汉字,则返回大写拼音
|
|
|
+ //如果c不是汉字,则返回String.valueOf(c)
|
|
|
+ pySb.append(Pinyin.toPinyin(target.charAt(i1)).toUpperCase());
|
|
|
+ }
|
|
|
+ indexPinyinBean.setBaseIndexPinyin(pySb.toString());//设置城市名全拼音
|
|
|
+ } else {
|
|
|
+ //pySb.append(indexPinyinBean.getBaseIndexPinyin());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return this;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 如果需要取出,则
|
|
|
+ * 取出首字母->tag,或者特殊字母 "#".
|
|
|
+ * 否则,用户已经实现设置好
|
|
|
+ *
|
|
|
+ * @param datas
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public IIndexBarDataHelper fillInexTag(List<? extends BaseIndexPinyinBean> datas) {
|
|
|
+ if (null == datas || datas.isEmpty()) {
|
|
|
+ return this;
|
|
|
+ }
|
|
|
+ int size = datas.size();
|
|
|
+ for (int i = 0; i < size; i++) {
|
|
|
+ BaseIndexPinyinBean indexPinyinBean = datas.get(i);
|
|
|
+ if (indexPinyinBean.isNeedToPinyin()) {
|
|
|
+ //以下代码设置城市拼音首字母
|
|
|
+ String tagString = indexPinyinBean.getBaseIndexPinyin().toString().substring(0, 1);
|
|
|
+ if (tagString.matches("[A-Z]")) {//如果是A-Z字母开头
|
|
|
+ indexPinyinBean.setBaseIndexTag(tagString);
|
|
|
+ } else {//特殊字母这里统一用#处理
|
|
|
+ indexPinyinBean.setBaseIndexTag("#");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return this;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public IIndexBarDataHelper sortSourceDatas(List<? extends BaseIndexPinyinBean> datas) {
|
|
|
+ if (null == datas || datas.isEmpty()) {
|
|
|
+ return this;
|
|
|
+ }
|
|
|
+ convert(datas);
|
|
|
+ fillInexTag(datas);
|
|
|
+ //对数据源进行排序
|
|
|
+ Collections.sort(datas, new Comparator<BaseIndexPinyinBean>() {
|
|
|
+ @Override
|
|
|
+ public int compare(BaseIndexPinyinBean lhs, BaseIndexPinyinBean rhs) {
|
|
|
+ boolean flag;
|
|
|
+ if (!lhs.isNeedToPinyin()) {
|
|
|
+ return 0;
|
|
|
+ } else if (!rhs.isNeedToPinyin()) {
|
|
|
+ return 0;
|
|
|
+ } else if ((flag = lhs.getBaseIndexTag().startsWith("#")) ^ rhs.getBaseIndexTag().startsWith("#")) {
|
|
|
+ return flag ? 1 : -1;
|
|
|
+ }
|
|
|
+ return lhs.getBaseIndexPinyin().compareTo(rhs.getBaseIndexPinyin());
|
|
|
+ //if (!lhs.isNeedToPinyin()) {
|
|
|
+// return 0;
|
|
|
+// } else if (!rhs.isNeedToPinyin()) {
|
|
|
+// return 0;
|
|
|
+// } else if (lhs.getBaseIndexTag().equals("#")) {
|
|
|
+// return 1;
|
|
|
+// } else if (rhs.getBaseIndexTag().equals("#")) {
|
|
|
+// return -1;
|
|
|
+// } else {
|
|
|
+// return lhs.getBaseIndexPinyin().compareTo(rhs.getBaseIndexPinyin());
|
|
|
+// }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ return this;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public IIndexBarDataHelper getSortedIndexDatas(List<? extends BaseIndexPinyinBean> sourceDatas, List<String> indexDatas) {
|
|
|
+ if (null == sourceDatas || sourceDatas.isEmpty()) {
|
|
|
+ return this;
|
|
|
+ }
|
|
|
+ //按数据源来 此时sourceDatas 已经有序
|
|
|
+ int size = sourceDatas.size();
|
|
|
+ String baseIndexTag;
|
|
|
+ for (int i = 0; i < size; i++) {
|
|
|
+ baseIndexTag = sourceDatas.get(i).getBaseIndexTag();
|
|
|
+ if (!indexDatas.contains(baseIndexTag)) {//则判断是否已经将这个索引添加进去,若没有则添加
|
|
|
+ indexDatas.add(baseIndexTag);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return this;
|
|
|
+ }
|
|
|
+}
|