1
當我打開搜索欄時,搜索建議框並未打開。只要搜索字段爲空,它就會保持關閉狀態。當我開始輸入時,只有這樣,纔會打開搜索建議框。 當我公開搜索字段時,如何不正當地打開搜索建議框?打開搜索建議到快速搜索框
YouTube的例子:
當我打開搜索欄時,搜索建議框並未打開。只要搜索字段爲空,它就會保持關閉狀態。當我開始輸入時,只有這樣,纔會打開搜索建議框。 當我公開搜索字段時,如何不正當地打開搜索建議框?打開搜索建議到快速搜索框
YouTube的例子:
searchView.setSuggestionsAdapter() is the answer
例如用於名單< String>的項目:
searchView.setSuggestionsAdapter(new SearchableCursorAdapter(this, getCursorByQuery(items, ""), items));
/**
* CursorAdapter with the search able list
* compiler bug in javac 1.5.0_07-164, we need to implement Filter able to make compilation work
*/
public class SearchableCursorAdapter extends CursorAdapter implements Filterable {
List<String> mItems;
public SearchableCursorAdapter(Context context, Cursor cursor, List<String> items) {
super(context, cursor);
mItems = items;
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.searchable_item, parent, false);
TextView textView = (TextView) view.findViewById(R.id.searchable_optianl_text_view);
textView.setText(cursor.getString(COLUMN_DISPLAY_NAME));
return view;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView textView = (TextView) view.findViewById(R.id.searchable_optianl_text_view);
textView.setText(cursor.getString(COLUMN_DISPLAY_NAME));
}
@Override
public String convertToString(Cursor cursor) {
return cursor.getString(COLUMN_DISPLAY_NAME);
}
@Override
public Cursor runQueryOnBackgroundThread(CharSequence constraint) {
FilterQueryProvider filter = getFilterQueryProvider();
if (filter != null) {
return filter.runQuery(constraint);
}
return getCursorByQuery(mItems, (String)constraint);
}
private static final int COLUMN_DISPLAY_NAME = 1;
}
/**
* create a cursor with the list to display
*/
Cursor getCursorByQuery(List<String> fullItems, String constraint) {
List<String> items = new ArrayList<String>();
for (String item : fullItems) {
if (item.toLowerCase(Locale.US).startsWith(((String) constraint).toLowerCase(Locale.US))) {
items.add(item);
}
}
// Load data from list to cursor
String[] columns = new String[] { "_id", "text" };
Object[] temp = new Object[] { 0, "default" };
MatrixCursor cursor = new MatrixCursor(columns);
for(int i = 0; i < items.size(); i++) {
temp[0] = i;
temp[1] = items.get(i);
cursor.addRow(temp);
}
return cursor;
}
在AndroidMenifest不要忘記:
<!-- enable the search dialog to send searches to SearchableActivity -->
<meta-data android:name="android.app.searchable" android:resource="@xml/searchable"/>
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
searchable.xml
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="@string/search_hint"
android:hint="@string/search_label">
</searchable>
menu.xml文件
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/menu_search"
android:title="@string/Search"
android:icon="@drawable/ic_action_search"
android:showAsAction="always"
android:actionViewClass="android.widget.SearchView" />
</menu>
您要打開前開始打字
? – Ranjit
我不會顯示一個彈出窗口,像YouTube這樣的一些可選結果 –