ActionBarSherlock学习笔记——SearchView

SearchView

1.添加搜索栏

 1 import com.actionbarsherlock.widget.SearchView;
 2 import com.actionbarsherlock.widget.SearchView.OnQueryTextListener;
 3 import com.actionbarsherlock.widget.SearchView.OnSuggestionListener;
 4         /**
 5          * 创建适配器对象
 6          */
 7         private SuggestionsAdapter suggestion;
 8         /**
 9          * 创建一个搜索view
10          */
11         SearchView search = new SearchView(getSupportActionBar()
12                 .getThemedContext());
13         /**
14          * 添加搜索提示
15          */
16         search.setQueryHint("Search for contries...");
17         /**
18          * 设置textview的监听
19          */
20         search.setOnQueryTextListener(this);
21         /**
22          * 设置选择搜索结果的监听
23          */
24         search.setOnSuggestionListener(this);
25         /**
26          * 设置搜索适配
27          */
28         suggestion = getSuggestions();
29         search.setSuggestionsAdapter(suggestion);
30         // 添加到menu中
31         menu.add("Search")
32                 .setIcon(R.drawable.ic_launcher)
33                 .setActionView(search)
34                 .setShowAsAction(
35                         MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW
36                                 | MenuItem.SHOW_AS_ACTION_IF_ROOM);

2.创建适配器

 1 /**
 2      * 生成一个Search适配器,其中的SearchManager.SUGGEST_COLUMN_TEXT_1 和下面的bindView是对应的
 3      * 
 4      * @return
 5      */
 6     private SuggestionsAdapter getSuggestions() {
 7         String[] columns = { BaseColumns._ID,
 8                 SearchManager.SUGGEST_COLUMN_TEXT_1 };
 9         MatrixCursor c = new MatrixCursor(columns);
10         c.addRow(new String[] { "1", "New_0" });
11         c.addRow(new String[] { "2", "New_1" });
12         c.addRow(new String[] { "3", "New_2" });
13         c.addRow(new String[] { "4", "New_3" });
14         return new SuggestionsAdapter(this, c);
15     }
16 
17     /**
18      * 为Search添加搜索结果的类,创建布局和添加搜索结果
19      * 
20      * @author Administrator
21      * 
22      */
23     private class SuggestionsAdapter extends CursorAdapter {
24 
25         public SuggestionsAdapter(Context context, Cursor c) {
26             super(context, c, 0);
27             // TODO Auto-generated constructor stub
28         }
29 
30         /**
31          * 为每一个item,添加搜索结果
32          */
33         @Override
34         public void bindView(View arg0, Context arg1, Cursor arg2) {
35             // TODO Auto-generated method stub
36             TextView view = (TextView) arg0;
37             int index = arg2
38                     .getColumnIndex(SearchManager.SUGGEST_COLUMN_TEXT_1);
39             view.setText(arg2.getString(index));
40         }
41 
42         /**
43          * 返回搜索结果的布局,这个是list布局
44          */
45         @Override
46         public View newView(Context arg0, Cursor arg1, ViewGroup arg2) {
47             // TODO Auto-generated method stub
48             LayoutInflater flater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
49             View v = flater.inflate(android.R.layout.simple_list_item_1, arg2,
50                     false);
51             return v;
52         }
53 
54     }

3.监听处理

 1 implements
 2         OnQueryTextListener, OnSuggestionListener
 3 /**
 4      * 搜索提交关键字,处理后返回true
 5      */
 6     @Override
 7     public boolean onQueryTextSubmit(String query) {
 8         // TODO Auto-generated method stub
 9         return false;
10     }
11 
12     /**
13      * textview变化监听
14      */
15     @Override
16     public boolean onQueryTextChange(String newText) {
17         // TODO Auto-generated method stub
18         return false;
19     }
20 
21     @Override
22     public boolean onSuggestionSelect(int position) {
23         // TODO Auto-generated method stub
24         return false;
25     }
26 
27     /**
28      * 选择搜索结果,处理了要返回true
29      */
30     @Override
31     public boolean onSuggestionClick(int position) {
32         // TODO Auto-generated method stub
33         Cursor c = (Cursor) suggestion.getItem(position);
34         String query = c.getString(c
35                 .getColumnIndex(SearchManager.SUGGEST_COLUMN_TEXT_1));
36         Toast.makeText(this, "Suggestion clicked: " + query, Toast.LENGTH_LONG)
37                 .show();
38         return true;
39     }
原文地址:https://www.cnblogs.com/qinghuaideren/p/3066351.html