我已经实施了搜索小部件以及建议。以下是我的明确条目:
<activity android:name=".HomeActivity"
android:theme="@style/AppTheme.NoActionBar" android:screenOrientation="portrait">
<meta-data android:name="android.app.default_searchable"
android:value=".SearchActivity" />
</activity>
<activity android:name=".SearchActivity" android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="@xml/searchable"/>
</activity>
HomeActivity中的oncreateOptions菜单方法:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.options_menu, menu);
searchItem= menu.findItem(R.id.search);
// Associate searchable configuration with the SearchView
SearchManager searchManager =
(SearchManager) getSystemService(getApplicationContext().SEARCH_SERVICE);
searchView =
(SearchView) menu.findItem(R.id.search).getActionView();
searchView.setSearchableInfo(
searchManager.getSearchableInfo(getComponentName()));
searchAdapter = new SuggestionAdapter(this, R.layout.suggestion_layout, null, columns,null, -1000);
searchView.setSuggestionsAdapter(searchAdapter);
SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextChange(final String newText) {
if(newText.length()>3){
handler.removeCallbacksAndMessages(null);
handler.postDelayed(new Runnable() {
@Override
public void run() {
EventBus.getDefault().post(new TriggerTrickSuggestion(newText));
}
},500);
}
return true;
}
@Override
public boolean onQueryTextSubmit(String query) {
if(!query.trim().equals(""))
{
Intent intent = new Intent(getApplicationContext(), SearchActivity.class);
intent.setAction(Intent.ACTION_SEARCH);
intent.putExtra("query", query);
startActivity(intent);
}
return true;
}
};
SearchView.OnSuggestionListener suggestionListener= new SearchView.OnSuggestionListener() {
@Override
public boolean onSuggestionSelect(int position) {
return false;
}
@Override
public boolean onSuggestionClick(int position) {
Cursor cursor= searchView.getSuggestionsAdapter().getCursor();
cursor.moveToPosition(position);
int id= cursor.getInt(0);
showTrickDetails(id);
return false;
}
};
searchView.setOnQueryTextListener(queryTextListener);
searchView.setOnSuggestionListener(suggestionListener);
return true;
}
搜索工作正常。列表视图中的建议也很好。但是它与其他应用程序的搜索视图看起来并不相同。在检查其他应用程序时,我注意到在点击搜索视图时它会展开,另一个屏幕覆盖主要活动。例如:Youtube搜索小部件。
我不知道如何才能实现这样的外观。找不到任何代码。