An awesome and customizable search dialog with built-in search options.
通过DevEco studio 2.0+,并下载SDK Native 版本2.0+
"在中文路径下,Build Debug Hap(s)会失败,请将项目放置在全英文目录下"
方式一:
在entry的gradle中集成如下操作:
修改implementation fileTree(dir: 'libs', include: ['.jar','.har'])增加*.har
方式二:
allprojects{
repositories{
mavenCentral()
}
}
implementation 'io.openharmony.tpc.thirdlib:search-dialog:1.0.5'
if you just want to use the simple search dialog first you need to provide searchable items. to achieve this you should implement Searchable in your model.
you can see the SampleModel for example :
public class SampleSearchModel implements Searchable {
private String mTitle;
public SampleSearchModel(String title) {
mTitle = title;
}
@Override
public String getTitle() {
return mTitle;
}
public SampleSearchModel setTitle(String title) {
mTitle = title;
return this;
}
}
now generate some search options in your ability :
private ArrayList<SampleModel> createSampleData(){
ArrayList<SampleSearchModel> items = new ArrayList<>();
items.add(new SampleModel("First item"));
items.add(new SampleModel("Second item"));
items.add(new SampleModel("Third item"));
items.add(new SampleModel("The ultimate item"));
items.add(new SampleModel("Last item"));
items.add(new SampleModel("Lorem ipsum"));
items.add(new SampleModel("Dolor sit"));
items.add(new SampleModel("Some random word"));
items.add(new SampleModel("guess who's back"));
return items;
}
then you just need to add the below lines where you want to show the dialog :
new SimpleSearchDialogCompat(MainActivity.this, "Search...",
"What are you looking for...?", null, createSampleData(),
new SearchResultListener<SampleModel>() {
@Override
public void onSelected(BaseSearchDialogCompat dialog,
SampleModel item, int position) {
// If filtering is enabled, [position] is the index of the item in the filtered result, not in the unfiltered source
toast(item.getTitle());
dialog.hide();
}
}).show();
The constructor parameters are
SimpleSearchDialogCompat(Context context, String title, String searchHint,
@Nullable Filter filter, ArrayList<T> items,
SearchResultListener<T> searchResultListener)
Just use setLoading(true)
for showing and setLoading(false)
for hiding it on an instance of SimpleSearchDialogCompat
I used this layout for simple search dialog but you can use anything else. Of course your layout should have thse two views :
You can use your custom layouts, adapters and search options by creating a class inheriting the BaseSearchDialogCompat take a look at SimpleSearchDialogCompat to see an example of how it can be done You should implement the BaseSearchDialogCompat methods :
// handle your view with this one
protected abstract void getView(Component view);
// Id of your custom layout
protected abstract int getLayoutResId();
// Id of the search TextField you used in your custom layout
protected abstract int getSearchBoxId();
// Id of the ListContainer you used in your custom layout
protected abstract int getRecyclerViewId();
You can use your custom filters for text searching. The one used in SimpleSearchDialogCompat It checks the search key and if an item and the key had partially exact same letters it will add that item to results and also if the CheckLCS was set to true, it will check if the amount of matching letters was bigger than the given AccuracyPercentage the item will be added to results
the one used in SimpleSearchDialogCompat is so simple despite its too long. the main functionality is in initializeViews method. you can create your custom adapters and use it instead of this one
it has two methods which you can use for highlighting the results.
/**
* 给字体穿插上色
*
* @param str1 列表中某一项字符串
* @param str2 搜索的字符
* @param defaultColor 默认的颜色
* @param highlightColor 红色
* @param textSize 字体大小
* @return 封装好的颜色字体
*/
RichTextBuilder highlightLCS(String str1, String str2, int defaultColor, int highlightColor,int textSize);
// Returns the LCS(Longest Common Subsequence) of two strings
String lcs(String text1, String text2)
通过DevEco studio,并下载SDK 将项目中的build.gradle文件中dependencies→classpath版本改为对应的版本(即你的IDE新建项目中所用的版本)
版权所有2020-2025
根据Apache许可证2.0版(“许可证”)获得许可;
除非符合许可证,否则不得使用此文件。
您可以从以下网址获取许可证副本:
http://www.apache.org/licenses/LICENSE-2.0
除非适用法律要求或书面同意,否则软件
根据许可证发放,按“原样”发放,
没有任何明示或暗示的保证或条件。
请参阅许可证以了解特定语言控制权限和
许可证的限制。
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。
1. 开源生态
2. 协作、人、软件
3. 评估模型