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/archive/2013/05/08/3066351.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值