Android的ActionBarSherlock

Android的ActionBarSherlock

本文介绍了Android的ActionBarSherlock,搜索查看`setOnCloseListener`不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用搜索查看,这是工作的罚款,但只有 setOnCloseListener 不工作;这是我的code

 进口com.actionbarsherlock.widget.SearchView.OnCloseListener;
 

  searchView.setOnCloseListener(新OnCloseListener(){
            @覆盖
            公共布尔的OnClose(){

                Toast.makeText(背景下,关闭,Toast.LENGTH_SHORT).show();
                返回false;
            }
        });
 

的**编辑*的 * 的**

  @覆盖
    公共布尔onCreateOptionsMenu(功能菜单){
        //用来把暗图标光行动起来吧
        //创建搜索视图
        最后搜索查看搜索查看=新的搜索查看(getSupportActionBar()getThemedContext());
        sea​​rchView.setQueryHint(搜索);
        sea​​rchView.setIconifiedByDefault(真正的);

    //搜索按钮
    menu.add(Menu.NONE,Menu.NONE,1,搜索词)
        .setIcon(R.drawable.abs__ic_search_api_holo_light)
        .setActionView(搜索查看)
        .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);

        菜单项sView = menu.findItem(1);

        sView.setOnActionExpandListener(本);

返回true;

}

    @覆盖
    公共布尔onMenuItemActionCollapse(菜单项项){
        。adopter.getFilter()过滤器(空);
        Toast.makeText(getApplicationContext(),崩溃,Toast.LENGTH_LONG).show();
        返回true; //返回真正的崩溃动作视图
    }

    @覆盖
    公共布尔onMenuItemActionExpand(菜单项项){
        Toast.makeText(getApplicationContext(),扩展,Toast.LENGTH_LONG).show();
        返回true; //返回true以扩大行动视图
    }
 

解决方案

由我自己解决了这个问题。刚刚离开 setOnCloseListener 将无法正常工作,并提出以下code。在 onCreateOptionsMenu

  // searchView.setOnCloseListener(新OnCloseListener(){
// @覆盖
//公共布尔的OnClose(){
// adapter.getFilter()过滤();
(在接近getBaseContext(),Toast.LENGTH_SHORT)// Toast.makeText .show();
//返回false;
//}
//});

        菜单项菜单项= menu.findItem(ID_OF_SEARCHVIEW);
        menuItem.setOnActionExpandListener(新OnActionExpandListener(){
            @覆盖
            公共布尔onMenuItemActionExpand(菜单项项){
                。adapter.getFilter()过滤();
                返回true;
            }

            @覆盖
            公共布尔onMenuItemActionCollapse(菜单项项){
                。adapter.getFilter()过滤();
                返回true;
            }
        });
 

I am using SearchView and it is working fine but only setOnCloseListener is not working; Here is my code

import com.actionbarsherlock.widget.SearchView.OnCloseListener;

and

searchView.setOnCloseListener(new OnCloseListener() {
            @Override
            public boolean onClose() {

                Toast.makeText(context, "close", Toast.LENGTH_SHORT).show();
                return false;
            }
        });

**EDIT****

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //Used to put dark icons on light action bar
        //Create the search view
        final SearchView searchView = new SearchView(getSupportActionBar().getThemedContext());
        searchView.setQueryHint("Search");
        searchView.setIconifiedByDefault(true);

    //search button
    menu.add(Menu.NONE,Menu.NONE,1,"Search a word")
        .setIcon(R.drawable.abs__ic_search_api_holo_light)
        .setActionView(searchView)
        .setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);

        MenuItem sView =  menu.findItem(1);

        sView.setOnActionExpandListener(this);

return true;

}

    @Override
    public boolean onMenuItemActionCollapse(MenuItem item) {
        adopter.getFilter().filter(null);
        Toast.makeText(getApplicationContext(), "collapse", Toast.LENGTH_LONG).show();
        return true; // Return true to collapse action view
    }

    @Override
    public boolean onMenuItemActionExpand(MenuItem item) {
        Toast.makeText(getApplicationContext(), "Expand", Toast.LENGTH_LONG).show();
        return true; // Return true to expand action view
    }
解决方案

Solved it by myself. Just leave setOnCloseListener it will not work, and put following code in onCreateOptionsMenu

//        searchView.setOnCloseListener(new OnCloseListener() {
//          @Override
//          public boolean onClose() {
//              adapter.getFilter().filter("");
//              Toast.makeText(getBaseContext(), "on close", Toast.LENGTH_SHORT).show();
//              return false;
//          }
//      });

        MenuItem menuItem = menu.findItem(ID_OF_SEARCHVIEW);
        menuItem.setOnActionExpandListener(new OnActionExpandListener() {
            @Override
            public boolean onMenuItemActionExpand(MenuItem item) {
                adapter.getFilter().filter("");
                return true;
            }

            @Override
            public boolean onMenuItemActionCollapse(MenuItem item) {
                adapter.getFilter().filter("");
                return true;
            }
        });

这篇关于Android的ActionBarSherlock,搜索查看`setOnCloseListener`不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 09:05