打开搜索建议以快速搜索框

时间:2014-03-13 16:11:11

标签: android search drop-down-menu search-box

当我打开搜索字段时,不会打开搜索建议框。只要搜索字段为空,它就会保持关闭状态。当我开始输入时,才会打开搜索建议框。 当我公开搜索字段时,如何以不正当的方式打开搜索建议框?

YouTube示例: YouTube example

1 个答案:

答案 0 :(得分:0)

searchView.setSuggestionsAdapter() is the answer

例如对于List<串GT;项目:

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>