2019独角兽企业重金招聘Python工程师标准>>>

1. SearchRecentSuggestionsProvider

  首先要创建自己的SearchRecentSuggestionsProvider,用于保存和查询搜索记录。

1.1 创建SearchRecentSuggestionsProvider

  SearchRecentSuggestionsProvider基本实现了进行读写搜索历史所需的所有功能,只需继承它并定义ContentProvider的AUTHORITY和Mode。Mode必须包含 DATABASE_MODE_QUERIES ,可选包含 DATABASE_MODE_2LINES 来让历史记录具有两行内容。具体内容如下:

public class SimpleSearchSuggestionsProvider extends SearchRecentSuggestionsProvider {private static final String LOG_TAG = SimpleSearchSuggestionsProvider.class.getSimpleName();public final static String AUTHORITY = "com.nex3z.examples.customsearchsuggestionitem.SimpleSearchSuggestionsProvider";public final static int MODE = DATABASE_MODE_QUERIES;public SimpleSearchSuggestionsProvider() {setupSuggestions(AUTHORITY, MODE);}
}

  然后要在AndroidManifest.xml中声明这个ContentProvider:

<application
...<providerandroid:name=".SimpleSearchSuggestionsProvider"android:authorities="com.nex3z.examples.customsearchsuggestionitem.SimpleSearchSuggestionsProvider" />
...
</application>

1.2. 保存搜索记录

  有了SimpleSearchSuggestionsProvider,就可以使用SearchRecentSuggestions可以很方便地保存搜索记录:

SearchRecentSuggestions suggestions = new SearchRecentSuggestions(this,SimpleSearchSuggestionsProvider.AUTHORITY, SimpleSearchSuggestionsProvider.MODE);
suggestions.saveRecentQuery(query, null);

这里 saveRecentQuery() 的第一个参数代表要保存的搜索记录,第二个参数为可选添加的内容(需要在SearchRecentSuggestionsProvider设置 DATABASE_MODE_2LINES )。

1.3. 查询搜索记录

  可以按照使用一般ContentProvider的方法来查询搜索记录。这里使用了SearchManager来辅助建立uri:

public Cursor getRecentSuggestions(String query, int limit) {Uri.Builder uriBuilder = new Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT).authority(SimpleSearchSuggestionsProvider.AUTHORITY);uriBuilder.appendPath(SearchManager.SUGGEST_URI_PATH_QUERY);String selection = " ?";String[] selArgs = new String[] { query };if (limit > 0) {uriBuilder.appendQueryParameter(SearchManager.SUGGEST_PARAMETER_LIMIT, String.valueOf(limit));}Uri uri = uriBuilder.build();return getContentResolver().query(uri, null, selection, selArgs, null);
}

2. 创建Suggestion Item

2.1. Item布局

  接下来创建自定义的Suggestion Item,在res/layout下建立search_suggestion_item.xml,其中包含一个用于显示图标的ImageView和一个用于显示提示的TextView:

<LinearLayoutandroid:orientation="horizontal"...><ImageViewandroid:id="@+id/iv_suggestion_item_icon".../><TextViewandroid:id="@+id/tv_suggestion_item_title".../></LinearLayout>

2.2. 创建Adapter

  Adapter把从ContentProvider中获取的提示数据填充到上面创建的Item里,这里的Adapter就是一个简单的CursorAdapter。前面使用SearchRecentSuggestions的 saveRecentQuery() 方法来保存历史记录,对应地,这里要从 SearchManager.SUGGEST_COLUMN_TEXT_1 获取所保存的搜索历史记录。

[@Override](https://my.oschina.net/u/1162528)
public void bindView(View view, Context context, Cursor cursor) {ViewHolder viewHolder = (ViewHolder) view.getTag();viewHolder.mTitle.setText(cursor.getString(cursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_TEXT_1)));
}

3. 创建Searchable Configuration

  Searchable Configuration用于配置搜索对话框的UI。在res/xml下建立searchable.xml,其中必须包含 label ,通常为app的名称,一般不可见。这里还添加了 hint ,用于在用户未输入搜索内容时,在搜索框显示提示。由于这里使用了自定义的adapter来显示搜索提示,这里并不需要添加 searchSuggestAuthority 。

<searchable xmlns:android="http://schemas.android.com/apk/res/android"android:label="@string/app_name"android:hint="@string/search_hint" />

4. 配置Searchable Activity

  Searchable Activity用来处理搜索,当用户提交搜索后,系统会启动对应的Activity,搜索内容通过带有 ACTION_SEARCH 的intent来传递。

4.1. 声明Searchable Activity

  这里以MainActivity做为Searchable Activity,首先要在AndroidManifest.xml中声明。

<application
...<activityandroid:name=".MainActivity"android:launchMode="singleTop"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter><intent-filter><action android:name="android.intent.action.SEARCH" /></intent-filter><meta-dataandroid:name="android.app.searchable"android:resource="@xml/searchable" /></activity>...
</application>

首先要添加meta-data,指向上一步中创建的Searchable Configuration(searchable.xml)。然后还要注册 android.intent.action.SEARCH的intent-filter,用于接收用户提交搜索后由系统发出的intent。注意这里还设置了 launchMode 为 singleTop ,避免搜索时重复启动Activity。

4.2. 添加搜索菜单

  在res/menu/下添加menu_search.xml如下:

<menu xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"><item android:id="@+id/action_search"android:title="@string/action_search"android:icon="@drawable/ic_search_24dp"app:showAsAction="collapseActionView|ifRoom"app:actionViewClass="android.support.v7.widget.SearchView" />
</menu>

搜索菜单以放大镜图标按钮的形式显示,注意这里指定 app:actionViewClass 为Support Library的SearchView。

4.3. 配置SearchView

  首先在onCreateOptionsMenu()找到搜索菜单对应的MenuItem:

@Override
public boolean onCreateOptionsMenu(Menu menu){getMenuInflater().inflate(R.menu.menu_search, menu);MenuItem searchItem = menu.findItem(R.id.action_search);...
}

  然后由searchItem获取SearchView,并通过SearchManager配置SearchableInfo,也就是之前的searchable.xml:

SearchManager searchManager =(SearchManager) getSystemService(Context.SEARCH_SERVICE);
final SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));

接下来把SearchSuggestionAdapter配置到searchView(注意SearchView的 setSuggestionsAdapter() 只支持CursorAdapter):

mSuggestionAdapter = new SearchSuggestionAdapter(this, null, 0);
searchView.setSuggestionsAdapter(mSuggestionAdapter);

当搜索框内容变化时,从ContentProvider查询对应的搜索记录,并传递给adapter:

searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {@Overridepublic boolean onQueryTextSubmit(String query) {return false;}@Overridepublic boolean onQueryTextChange(String newText) {Cursor cursor = getRecentSuggestions(newText, 10);mSuggestionAdapter.swapCursor(cursor);return false;}
});

  当suggestion被选中时,把对应suggestion内容填充到搜索栏,并触发搜索:

searchView.setOnSuggestionListener(new SearchView.OnSuggestionListener() {@Overridepublic boolean onSuggestionSelect(int position) {return false;}@Overridepublic boolean onSuggestionClick(int position) {searchView.setQuery(mSuggestionAdapter.getSuggestionText(position), true);return true;}
});

这里 setQuery() 的第二个参数指示是否触发搜索,这里设为true,当用户点击搜索历史提示时,会立即搜索指定关键词。

4.4. 处理搜索

  使用自定义搜索提示时,处理搜索并没有什么不同,这里顺带一提。当用户提交搜索后,系统会启动对应的Searchable Activity,发送ACTION_SEARCH intent,其中带有名为 QUERY 的String extra保存了搜索内容。 首先在 onCreate() 中调用 handleIntent() 来处理 ACTION_SEARCH ,另外Activity设置了 launchMode 为 "singleTop" ,还需要在 onNewIntent() 中调用 handleIntent() 。

@Override
protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);handleIntent(getIntent());
}@Override
protected void onNewIntent(Intent intent) {handleIntent(intent);
}private void handleIntent(Intent intent) {if (Intent.ACTION_SEARCH.equals(intent.getAction())) {String query = intent.getStringExtra(SearchManager.QUERY);SearchRecentSuggestions suggestions = new SearchRecentSuggestions(this,SimpleSearchSuggestionsProvider.AUTHORITY, SimpleSearchSuggestionsProvider.MODE);suggestions.saveRecentQuery(query, null);// 保存最近的数据}
}

5. 完整代码

https://github.com/nex3z/android-examples/tree/master/CustomSearchSuggestionItem

转载于:https://my.oschina.net/u/2501904/blog/1517835

自定义SearchView的搜索提示相关推荐

  1. Android SearchView介绍及搜索提示实现

    本文主要介绍SearchView的使用.即时搜索提示功能的实现,以及一些设置. 1. layout文件 Java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 <?xm ...

  2. es基于completion suggest实现搜索提示

    在之前的某一篇中,我们使用了es的前缀搜索,获得了文档根据前缀进行匹配的效果,如下图所示, 下面说说在es中的另一种实现搜索提示的功能,基于completion suggest 进行实现,其在实际应用 ...

  3. android 多条件搜索界面,SearchView实现搜索功能

    SearchView是安卓自带的搜索控件,可以帮助我们省下很多功夫.SearchView提供的api很多,但是麻烦在于SearchView的默认样式很多情况下不满足我们的开发需求,需要我们进行去进行定 ...

  4. C# WinForm 技巧四:COMBOBOX搜索提示

    comboBox和textBox支持内置的搜索提示功能, 在form的InitializeComponent()中添加如下语句:    this.comboBox1.AutoCompleteCusto ...

  5. Android AutoCompleteTextView控件实现类似百度搜索提示,限制输入数字长度

    Android AutoCompleteTextView 控件实现类似被搜索提示,效果如下 1.首先贴出布局代码 activity_main.xml: <?xml version="1 ...

  6. 《iVX 高仿美团APP制作移动端完整项目》02 搜索、搜索提示及类别需求分析思路及制作流程

    点击整个专栏查看其它系列文章 (系列文章更新中-):<iVX 高仿美团APP制作移动端完整项目> 项目界面预览: 一.搜索制作 在上一节中我们完成了标题头的制作,接下来我们查看如何制作搜索 ...

  7. via浏览器简洁主页html源码 支持搜索提示

    简介: via浏览器简洁主页源码支持搜索提示,扒的一个简洁好看的via浏览器主页源码,纯html+js无后端源码分享给大家,放到服务器即可运行. 网盘下载地址: http://kekewl.cc/AL ...

  8. 搜索提示的实现(仿百度):附源码和在线demo

    这篇文章是我转载过来的,我现在做的搜索引擎项目也做了一个基于Jquery做的自动提示功能,这里就不贴了,下面我给出一个我转载的文章分享给大家,喜欢的拿去吧! 智能搜索提示的功能大家都用过,百度搜索的时 ...

  9. Domino下实现仿Google搜索提示效果

    随着Ajax的运用越来越广泛,越来越多的用户体验被提升,在Domino中应用Ajax实现Google搜索提示效果,在用户的使用过程中也是一种很实用的体验,下面将详细介绍下实现的思路和方法,仅供参考,以 ...

最新文章

  1. 解决request中文乱码问题
  2. NetCore服务虚拟化01(集群组件Sodao.Core.Grpc)
  3. html li 做瀑布流,js实现瀑布流效果(自动生成新的内容)
  4. Unity2018新功能抢鲜 | 粒子系统改进
  5. 信息学奥赛一本通(C++版)在线评测系统 基础(一) 第一章 参考答案(AC代码)
  6. 银行卡四要素API 方便好用
  7. 你说南京很好,但不是你最想去的城市,那么,上海呢,要不借这个机会去看看吧--写给自己
  8. 使用Java程序接口备份数据库的思路与实现
  9. Javascript 之 事件冒泡(Event Bubbling)
  10. flushia系统_IA 系统和应用 第七章 环境组态.pdf
  11. 看懂需要勇气,33张人性图!
  12. VB.Net实现身份证读卡器调用读取身份证信息和社保卡信息
  13. 华硕ROG|玩家国度冰刃6双屏GX650RX Windows11原厂预装系统 工厂模式恢复安装带ASUSRecevory一键还原
  14. 基于PHP+小程序(MINA框架)+Mysql数据库的评选投票小程序系统设计与实现
  15. 联想LJ2400激光打印机开机4灯闪烁维修分析
  16. 好书推荐:《Google.Android开发入门与实战》
  17. usb驱动开发16——设备生命线
  18. 看BT,VC的倒下!下一个是迅雷?
  19. android studio中取消关联git
  20. python中step什么意思_如何在Python中求解step函数?

热门文章

  1. python爬虫正则表达式实例-Python 正则表达式爬虫使用案例解析
  2. python项目实例代码-python开源项目及示例代码
  3. python语音播报库-基于python GUI开发的点名小程序(语音播报)
  4. python和c++哪个好用-python和C++语言哪个好?老男孩教育
  5. python读音有道词典-centos7安装有道词典(不能发音和取词)
  6. gitbook的使用
  7. php 防止刷新重复提交,php防止刷新与重复提交实例代码
  8. Revising Aggregations - The Count Function(集合函数-count)
  9. UVa1388 - Graveyard
  10. 计算几何中的线段相交判断问题