android 输入模糊匹配_Android 模糊搜索

本文详细介绍了在Android开发中实现模糊搜索的功能,包括汉字转拼音、模糊搜索Adapter的封装以及自定义模糊搜索规则。通过FuzzySearchBaseAdapter和IFuzzySearchRule接口,实现了拼音首字母模糊搜索,并允许根据需求自定义匹配规则,提高用户体验。
摘要由CSDN通过智能技术生成

在Android移动端开发过程中,列表展示是咱们经常使用的一种展现方式。这个时候就可能有如下情况出现了,比如说现在咱们列表里面的项还是相当可观的,而且咱们只想快速的找到咱们需要的哪些项。例如手机联系人里面。咱们想快速的找到张三。这个时候咱们就需要一个搜索功能了。同时为了用户体验更加好,咱们还支持拼音搜索的功能。想要找张三,输入”张“能找到,输入”z“也能找到。

一,效果图

在讲解实现过程之前咱们先献上效果图。

62be70e527f0

fuzzy_search_action_2.gif

这里因为我是用模拟器跑的,所以在输入的时候键盘没有弹出来。gif的时候只输入了拼音,其实中文也是可以匹配到的。

62be70e527f0

device-2018-04-28-173955.png

二,功能

列表里面的项按照首字母分类分组,并且滑动的时候每个分组固定在列表的顶部。

列表支持字母索引(列表右侧有字母索引bar)。

不仅支持原始字符的模糊搜索,还支持拼音模糊搜索。

模糊搜索的规则自定义,当然咱们实例里面也会默认实现一种(拼音首字母模糊搜索)。

1,2两点属于列表字母索引的功能了,3,4

两点模糊搜索功能才是咱们本文的重点。

三,实现

这篇文章咱们主要关注模糊搜索的实现过程和封装,关于列表字母索引的功能请参考之前文章Android RecyclerView字母索引

模糊搜索实现过程主要分为以下几个步骤:汉字转拼音的实现(当然了如果本来就是英文字符则不用转)、模糊搜索adapter的封装(FuzzySearchBaseAdapter),模糊搜索规则的自定义。

3.1 汉字转拼音

Android关于汉字转拼音的实现,网上也一大堆。在DEMO中咱们也封装了一个汉字转拼音的帮助类PinyinUtil。有兴趣的可以到DEMO里面看看大概的实现过程。PinyinUtil帮助列里面两个重要的函数。

/**

* 中文转换成拼音,返回结果是list

*

* @param source 原始字符

* @return 中国->["zhong", "guo"]

*/

public static List getPinYinList(String source) {

if (source == null || source.isEmpty()) {

return null;

}

List pinyinList = new ArrayList<>();

for (int i = 0; i < source.length(); i++) {

String item = source.substring(i, i + 1);

if (item.getBytes().length >= 2) {

String pinyin = getSinglePinYin(item);

if (pinyin == null) {

pinyin = item;

}

pinyinList.add(pinyin);

} else {

pinyinList.add(item);

}

}

return pinyinList;

}

/**

* 中文转换成拼音

*

* @param source 原始字符

* @return 中国->"zhongguo"

*/

public static String getPinYin(String source) {

if (source == null || source.isEmpty()) {

return null;

}

StringBuilder pinyinList = new StringBuilder();

for (int i = 0; i < source.length(); i++) {

String item = source.substring(i, i + 1);

if (item.getBytes().length >= 2) {

String pinyin = getSinglePinYin(item);

if (pinyin == null) {

pinyin = item;

}

pinyinList.append(pinyin);

} else {

pinyinList.append(item);

}

}

return pinyinList.toString();

}

这里特别说下,为了方便拼音的模糊匹配我特意会做这样的处理:比如输入的是”中文“,我会把他转换成拼音字符串列表["zhong", "wen"]的形式。

3.2 模糊搜索adapter的封装

为了让使用起来比较方便,咱们封装一个模糊搜索的adapter FuzzySearchBaseAdapter并且实现Filterable接口。

public abstract class FuzzySearchBaseAdapter

extends RecyclerView.Adapter implements Filterable {

private FuzzySearchFilter mFilter;

private List mBackDataList;

protected List mDataList;

private IFuzzySearchRule mIFuzzySearchRule;

public FuzzySearchBaseAdapter(IFuzzySearchRule rule) {

this(rule, null);

}

public FuzzySearchBaseAdapter(IFuzzySearchRule rule, List dataList) {

if (rule == null) {

mIFuzzySearchRule = new DefaultFuzzySearchRule();

}

mBackDataList = dataList;

mDataList = dataList;

}

public void setDataList(List dataList) {

mBackDataList = dataList;

mDataList = dataList;

}

@Override

public int getItemCount() {

return mDataList == null ? 0 : mDataList.size();

}

@Override

public Filter getFilter() {

if (mFilter == null) {

mFilter = new FuzzySearchFilter();

}

return mFilter;

}

private class FuzzySearchFilter extends Filter {

/**

* 执行过滤操作,如果搜索的关键字为空,默认所有结果

*/

@Override

protected FilterResults performFiltering(CharSequence constraint) {

FilterResults result = new FilterResults();

List filterList;

if (TextUtils.isEmpty(constraint)) {

filterList = mBackDataList;

} else {

filterList = new ArrayList<>();

for (ITEM item : mBackDataList) {

if (mIFuzzySearchRule.accept(constraint, item.getSourceKey(), item.getFuzzyKey())) {

filterList.add(item);

}

}

}

result.values = filterList;

result.count = filterList.size();

return result;

}

/**

* 得到过滤结果

*/

@SuppressWarnings("unchecked")

@Override

protected void publishResults(CharSequence constraint, FilterResults results) {

mDataList = (List) results.values;

notifyDataSetChanged();

}

}

}

为了方便adapter的使用,我们规定data ite 必须实现IFuzzySearchItem。

/**

* 先匹配原始数据,再匹配模糊数据

*/

public interface IFuzzySearchItem {

/**

* 获取item原始字符串

*

* @return 原始item字符串

*/

String getSourceKey();

/**

* 获取item模糊字符串,item对应的拼音 江西省->["jiang", "xi", "sheng"]

*

* @return 模糊item字符串

*/

List getFuzzyKey();

}

还得注意下FuzzySearchBaseAdapter内部类FuzzySearchFilter里面的performFiltering()函数是用来处理模糊匹配过程的,模糊匹配的规则我们是通过IFuzzySearchRule接口来实现的。这样整个就很灵活了,如果默认的匹配规则不符合您的要求,您完全可以自己去实现一个高大上的匹配规则。

3.3 模糊搜索规则的自定义

为了让模糊匹配更加的灵活,FuzzySearchBaseAdapter的匹配规则是通过IFuzzySearchRule接口来实现的。所以如果您有特别好的匹配规则可以自定义实现IFuzzySearchRule接口。DEMO里面咱们也默认实现一种匹配规则DefaultFuzzySearchRule,默认匹配规则如下:先匹配原始字符,然后在匹配原始字符拼音的首字母。而且不区分大小写。

public class DefaultFuzzySearchRule implements IFuzzySearchRule {

@Override

public boolean accept(CharSequence constraint, String itemSource, List itemPinYinList) {

/**

* 1. 先匹配原始的字符,比如 原始字符是 "中国" 输入 "中" 也能保证匹配到

*/

if ((itemSource != null && itemSource.toLowerCase().contains(constraint.toString().toLowerCase()))) {

return true;

}

/**

* 2. 拼音匹配 这里咱们匹配每个拼音的首字母

*/

if (itemPinYinList != null && !itemPinYinList.isEmpty()) {

StringBuilder firstWord = null;

for (String wordPinYin : itemPinYinList) {

if (!TextUtils.isEmpty(wordPinYin)) {

if (firstWord == null) {

firstWord = new StringBuilder(wordPinYin.substring(0, 1));

} else {

firstWord.append(wordPinYin.substring(0, 1));

}

}

}

return firstWord != null && firstWord.toString().toLowerCase().contains(constraint.toString().toLowerCase());

}

return false;

}

}

讲解就说这么多了。整个的实现过程还是比较简单的,大伙有不明白的地方可以参考DEMOD的具体实现,DEMO下载地址。或者留言,我都会尽力帮大家解决的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值