在使用ActionBarSherlock定义app的头部操作时,会经常看见搜索的动作,本文主要介绍一下搜索是如何实现的。
1. SearchView
是搜索的核心组件,具体介绍请参考Android官方说明文档:http://developer.android.com/reference/android/widget/SearchView.html
XML 属性
属性名称 | 描述 |
android:iconifiedByDefault | 设置SearchView的默认状态。 如果为true,在没有被使用和点击展开时它将被图形化。 |
android:maxWidth | SearchView的可选择的最大宽度 |
android:queryHint | 当查询条件为空时显示的一个提示字符串。 |
重要的接口
名称 | 作用 |
interface SearchView.OnCloseListener | 当用户关闭SearchView时触发的回调函数 |
interface SearchView.OnQueryTextListener | 当查询文本改变时的回调函数。 |
interface SearchView.OnSuggestionListener | 在建议上选择事件时的回调接口。 |
demo的核心代码
package com.hsx.wangcuo;
import android.app.SearchManager;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.os.Bundle;
import android.provider.BaseColumns;
import android.view.LayoutInflater;
import android.view.View;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.MenuItem;
import com.actionbarsherlock.view.SubMenu;
import com.actionbarsherlock.widget.SearchView;
import com.hsx.wangcuo.adapter.QuerySuggestionsAdapter;
import com.hsx.wangcuo.util.ToastUtil;
public class MainActivity extends SherlockActivity implements SearchView.OnSuggestionListener, SearchView.OnQueryTextListener {
private static final String[] COLUMNS = { BaseColumns._ID, SearchManager.SUGGEST_COLUMN_TEXT_1, };
private QuerySuggestionsAdapter mSuggestionsAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initActionBarTitle();
}
private void initActionBarTitle() {
// 可以自定义actionbar
getSupportActionBar().setDisplayShowCustomEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(false);
// 不在actionbar显示logo
getSupportActionBar().setDisplayShowHomeEnabled(false);
View mainActionBarView = LayoutInflater.from(this).inflate(R.layout.main_action_bar, null);
getSupportActionBar().setCustomView(mainActionBarView);
}
@Override
public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
createSearchItem(menu);
/*************************************************/
MenuItem share = menu.add(0, 1, 2, "share");
share.setIcon(R.drawable.abs__ic_menu_share_holo_dark);
share.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
/***************************************************/
SubMenu addMenu = menu.addSubMenu(0, 2, 3, "overflow");
addMenu.add(0, 3, 0, "更换主题");
addMenu.add(0, 4, 0, "系统设置");
addMenu.add(0, 5, 0, "修改密码");
addMenu.add(0, 6, 0, "更换头像");
MenuItem overFlowItem = addMenu.getItem();
overFlowItem.setIcon(R.drawable.abs__ic_menu_moreoverflow_holo_dark);
overFlowItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
return super.onCreateOptionsMenu(menu);
}
/**
* Create the search view(搜索的核心代码)
*
* @param menu
* @return void
* @author hsx
* @time 2014-3-16下午03:48:01
*/
private void createSearchItem(com.actionbarsherlock.view.Menu menu) {
SearchView searchView = new SearchView(getSupportActionBar().getThemedContext());
searchView.setQueryHint("Search for countries…");
searchView.setOnQueryTextListener(this);
searchView.setOnSuggestionListener(this);
if (mSuggestionsAdapter == null) {
MatrixCursor cursor = new MatrixCursor(COLUMNS);
cursor.addRow(new String[] { "1", "'Murica" });
cursor.addRow(new String[] { "2", "Canada" });
cursor.addRow(new String[] { "3", "Denmark" });
mSuggestionsAdapter = new QuerySuggestionsAdapter(getSupportActionBar().getThemedContext(), cursor);
}
searchView.setSuggestionsAdapter(mSuggestionsAdapter);
MenuItem searchItem = menu.add(0, 0, 0, "search");
searchItem.setIcon(R.drawable.abs__ic_search);
searchItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
searchItem.setActionView(searchView);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 1:
ToastUtil.showShortToast(MainActivity.this, "分享");
break;
case 3:
ToastUtil.showShortToast(MainActivity.this, "更换主题");
break;
case 4:
ToastUtil.showShortToast(MainActivity.this, "系统设置");
break;
case 5:
ToastUtil.showShortToast(MainActivity.this, "修改密码");
break;
case 6:
ToastUtil.showShortToast(MainActivity.this, "更换头像");
break;
}
return super.onOptionsItemSelected(item);
}
/***
* implements SearchView.OnQueryTextListener
*/
@Override
public boolean onQueryTextSubmit(String query) {
ToastUtil.showShortToast(this, "You searched for: " + query);
return true;
}
/***
* implements SearchView.OnQueryTextListener
*/
@Override
public boolean onQueryTextChange(String newText) {
return false;
}
/**
* implements SearchView.OnSuggestionListener
*/
@Override
public boolean onSuggestionSelect(int position) {
return false;
}
/**
* implements SearchView.OnSuggestionListener
*/
@Override
public boolean onSuggestionClick(int position) {
Cursor c = (Cursor) mSuggestionsAdapter.getItem(position);
String query = c.getString(c.getColumnIndex(SearchManager.SUGGEST_COLUMN_TEXT_1));
ToastUtil.showShortToast(this, "Suggestion clicked: " + query);
return true;
}
}
/*******************************************************************************************************************************/
QuerySuggestionsAdapter
package com.hsx.wangcuo.adapter;
import android.app.SearchManager;
import android.content.Context;
import android.database.Cursor;
import android.support.v4.widget.CursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class QuerySuggestionsAdapter extends CursorAdapter {
public QuerySuggestionsAdapter(Context context, Cursor c) {
super(context, c, 0);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(android.R.layout.simple_list_item_1, parent, false);
return v;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView tv = (TextView) view;
final int textIndex = cursor.getColumnIndex(SearchManager.SUGGEST_COLUMN_TEXT_1);
tv.setText(cursor.getString(textIndex));
}
}
效果图如下:
还有一个小的bug,没有找到解决方法:输入一个字母的时候,不弹出提示
demo的地址 : http://download.csdn.net/detail/abc13939746593/7061035