Skip to content

Commit

Permalink
更新TinPinyin到2.0.3;多音字处理借助TinyPinyin的方式; 添加自定义排序规则方法
Browse files Browse the repository at this point in the history
  • Loading branch information
YoKeyword committed Jul 26, 2017
1 parent 178bb3e commit a4696e3
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 23 deletions.
3 changes: 2 additions & 1 deletion indexablerecyclerview/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ android {
}

dependencies {
compile 'com.github.promeg:tinypinyin:1.0.0'
compile 'com.github.promeg:tinypinyin:2.0.3'
compile 'com.github.promeg:tinypinyin-lexicons-android-cncity:2.0.3'
provided 'com.android.support:appcompat-v7:25.3.1'
provided 'com.android.support:recyclerview-v7:25.3.1'
}
7 changes: 5 additions & 2 deletions indexablerecyclerview/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<manifest package="me.yokeyword.indexablerecyclerview"
xmlns:android="http://schemas.android.com/apk/res/android">
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="me.yokeyword.indexablerecyclerview">

<uses-sdk
tools:overrideLibrary="com.github.promeg.tinypinyin.lexicons.android.cncity,com.github.promeg.tinypinyin.android.asset.lexicons"/>
<application/>
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -29,31 +29,31 @@ public class EntityWrapper<T> {
this.itemType = itemType;
}

String getIndex() {
public String getIndex() {
return index;
}

void setIndex(String index) {
this.index = index;
}

String getIndexTitle() {
public String getIndexTitle() {
return indexTitle;
}

void setIndexTitle(String indexTitle) {
this.indexTitle = indexTitle;
}

String getPinyin() {
public String getPinyin() {
return pinyin;
}

void setPinyin(String pinyin) {
this.pinyin = pinyin;
}

String getIndexByField() {
public String getIndexByField() {
return indexByField;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ private void notifySetListener(int type) {
mDataSetObservable.notifySetListener(type);
}

List<T> getItems() {
public List<T> getItems() {
return mDatas;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ public class IndexableLayout extends FrameLayout {
private DataObserver mDataSetObserver;

private int mCompareMode = MODE_FAST;
private Comparator mComparator;
private Handler mHandler;

private HeaderFooterDataObserver<EntityWrapper> mHeaderFooterDataSetObserver = new HeaderFooterDataObserver<EntityWrapper>() {
Expand Down Expand Up @@ -251,6 +252,13 @@ public void setCompareMode(@CompareMode int mode) {
this.mCompareMode = mode;
}

/**
* set sort-mode
*/
public <T extends IndexableEntity> void setComparator(Comparator<EntityWrapper<T>> comparator) {
this.mComparator = comparator;
}

/**
* set Sticky Enable
*/
Expand Down Expand Up @@ -670,11 +678,11 @@ public int compare(String lhs, String rhs) {
T item = datas.get(i);
String indexName = item.getFieldIndexBy();
String pinyin = PinyinUtil.getPingYin(indexName);
entity.setPinyin(pinyin);

// init EntityWrapper
if (PinyinUtil.matchingLetter(pinyin)) {
entity.setIndex(pinyin.substring(0, 1).toUpperCase());
entity.setPinyin(pinyin);
entity.setIndexByField(item.getFieldIndexBy());
} else if (PinyinUtil.matchingPolyphone(pinyin)) {
entity.setIndex(PinyinUtil.gePolyphoneInitial(pinyin).toUpperCase());
Expand All @@ -685,7 +693,6 @@ public int compare(String lhs, String rhs) {
item.setFieldIndexBy(hanzi);
} else {
entity.setIndex(INDEX_SIGN);
entity.setPinyin(pinyin);
entity.setIndexByField(item.getFieldIndexBy());
}
entity.setIndexTitle(entity.getIndex());
Expand All @@ -709,14 +716,19 @@ public int compare(String lhs, String rhs) {

ArrayList<EntityWrapper<T>> list = new ArrayList<>();
for (List<EntityWrapper<T>> indexableEntities : map.values()) {
Comparator comparator;
if (mCompareMode == MODE_FAST) {
comparator = new InitialComparator<T>();
Collections.sort(indexableEntities, comparator);
} else if (mCompareMode == MODE_ALL_LETTERS) {
comparator = new PinyinComparator<T>();
Collections.sort(indexableEntities, comparator);
if (mComparator != null) {
Collections.sort(indexableEntities, mComparator);
} else {
Comparator comparator;
if (mCompareMode == MODE_FAST) {
comparator = new InitialComparator<T>();
Collections.sort(indexableEntities, comparator);
} else if (mCompareMode == MODE_ALL_LETTERS) {
comparator = new PinyinComparator<T>();
Collections.sort(indexableEntities, comparator);
}
}

list.addAll(indexableEntities);
}
return list;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,13 @@ public class PinyinUtil {
* Chinese character -> Pinyin
*/
public static String getPingYin(String inputString) {
char[] input = inputString.trim().toCharArray();
String output = "";
for (int i = 0; i < input.length; i++) {
output += Pinyin.toPinyin(input[i]);
}
return output.toLowerCase();
if (inputString == null) return "";
return Pinyin.toPinyin(inputString, "").toLowerCase();
}

/**
* Are start with a letter
*
* @return if return false, index should be #
*/
static boolean matchingLetter(String inputString) {
Expand Down

0 comments on commit a4696e3

Please sign in to comment.