在Fragment中添加SearchView

时间:2015-12-15 14:14:20

标签: android xamarin searchview

尝试在操作栏中包含搜索结果。为此,我做了以下工作:

在菜单文件夹中创建了MenuSearch.xml,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
   <item
    android:id="@+id/action_search"
    android:actionViewClass="Android.Support.V7.Widget.SearchView"
    android:icon="@android:drawable/ic_menu_search"
    android:showAsAction="always"
    android:title="Search"/>

</menu>

片段中包含OnCreateOptionsMenu方法:

public override void OnCreateOptionsMenu(IMenu menu, MenuInflater inflater)
    {
        inflater.Inflate(Resource.Menu.MenuSearch, menu);
        base.OnCreateOptionsMenu(menu, inflater);
        var searchManager = (SearchManager) this.Activity.GetSystemService(Context.SearchService);
        searchView = (SearchView) (menu.FindItem(Resource.Id.action_search).ActionView);
        searchView.SetSearchableInfo(searchManager.GetSearchableInfo(Activity.ComponentName));

    }

我在片段的oncreate中设置了HasOptionsMenu = true;

使用上面的代码,我在行searchView.SetSearchableInfo...中得到空指针。不知道为什么会发生这种情况,尽管一切似乎都是正确的。

根据解决类似问题的一些建议,我也尝试了以下搜索视图选项。

        var item = menu.FindItem (Resource.Id.action_search);
        var searchItem = MenuItemCompat.GetActionView(item);
        searchView = searchItem.JavaCast<Android.Support.V7.Widget.SearchView>();
        searchView.SetSearchableInfo(searchManager.GetSearchableInfo(Activity.ComponentName));

但是,我再次将searchView视为空。

现在我已经尝试了可能的选项,我对于为什么searchview变为null无能为力。

非常感谢任何帮助。

7 个答案:

答案 0 :(得分:29)

将MenuSearch.xml更改为

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item android:id="@+id/action_search"
        android:title="@string/app_name"
        android:icon="@drawable/ic_action_search"
        app:showAsAction="collapseActionView|ifRoom"
         />
</menu>

在您的片段中添加searchView.setOnQueryTextListener

@Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        menu.clear();
        inflater.inflate(R.menu.search_option_menu, menu);
        MenuItem item = menu.findItem(R.id.action_search);
        SearchView searchView = new SearchView(((MainActivity) mContext).getSupportActionBar().getThemedContext());
        MenuItemCompat.setShowAsAction(item, MenuItemCompat.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW | MenuItemCompat.SHOW_AS_ACTION_IF_ROOM);
        MenuItemCompat.setActionView(item, searchView);
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
            }
            @Override
            public boolean onQueryTextChange(String newText) {
                return false;
            }
        });
        searchView.setOnClickListener(new View.OnClickListener() {
                                          @Override
                                          public void onClick(View v) {

                                          }
                                      }
        );
    }

答案 1 :(得分:5)

答案已更新至2018年。如果您使用的是Java,对Kotlin表示抱歉。

您的xml菜单资源(例如search_menu.xml):

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item android:id="@+id/action_search"
        android:title="@string/app_name"
        android:icon="@drawable/ic_search"
        app:showAsAction="collapseActionView|ifRoom"
        />
</menu>

构建代码:

override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater?) {
    super.onCreateOptionsMenu(menu, inflater)
    menu.clear()
    inflater?.inflate(R.menu.search_menu, menu)
    val searchView = SearchView((context as MainActivity).supportActionBar?.themedContext ?: context)
    menu.findItem(R.id.action_search).apply {
        setShowAsAction(MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW or MenuItem.SHOW_AS_ACTION_IF_ROOM)
        actionView = searchView
    }

    searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
        override fun onQueryTextSubmit(query: String): Boolean {
            return false
        }

        override fun onQueryTextChange(newText: String): Boolean {
            return false
        }
    })
    searchView.setOnClickListener {view ->  }
}

您可能还需要将此行放在onCreate()中(适用于我的片段),以使所有内容都显示出来:

setHasOptionsMenu(true)

答案 2 :(得分:1)

我解决了这个问题,我想分享我对searchView NullPointerException的回答

我们需要从主要活动中删除以下方法

      public boolean onOptionsItemSelected(MenuItem item) {
          int id = item.getItemId();  
          return true;

并在OnActivityCreated setHasOptionsMenu(true)中使用内部片段 和覆盖方法

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
         inflater.inflate(R.menu.menu, menu);
    }

    @Override
     public boolean onOptionsItemSelected(MenuItem item) {
       // handle item selection
        switch (item.getItemId()) {
            case R.id.action_search:

     //       onCall();   //your logic

            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

答案 3 :(得分:1)

您的 search_options_menu.xml 应如下所示:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item android:id="@+id/action_search"
        android:title="@string/app_name"
        android:icon="@drawable/ic_action_search"
        app:showAsAction="collapseActionView|ifRoom"
         />
</menu>

覆盖片段中 onCreate()内的片段setHasOptionsMenu(true);中的 onCreateOptionMenu 方法。

@Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
       menu.clear();
        inflater.inflate(R.menu.search_option_menu, menu);
        MenuItem item = menu.findItem(R.id.action_search);
                SearchView searchView = (SearchView) MenuItemCompat.getActionView(menuItem);
        MenuItemCompat.setShowAsAction(item, MenuItemCompat.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW | MenuItemCompat.SHOW_AS_ACTION_IF_ROOM);
        MenuItemCompat.setActionView(item, searchView);
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
            }
            @Override
            public boolean onQueryTextChange(String newText) {
                    //Do search code here
                return true;
            }
        });

    }

答案 4 :(得分:1)

对于2018年末和兼容> = {26的api(弃用警告)和格式正确的答案:

menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item android:id="@+id/action_search"
        android:title="Search_change_me_with_proper_id"
        android:icon="@drawable/ic_action_search"
        app:showAsAction="collapseActionView|ifRoom"/>
</menu>

ActivityFragment中的Java代码:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    setHasOptionsMenu(true); // Add this!
    return inflater.inflate(R.layout.fragment_add_app, container, false);
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    menu.clear();

    inflater.inflate(R.menu.menu_add_app, menu);
    MenuItem item = menu.findItem(R.id.action_search_menu_add_app);
    item.setShowAsAction(MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW | MenuItem.SHOW_AS_ACTION_IF_ROOM);

    SearchView searchView = (SearchView) item.getActionView();
    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

        @Override
        public boolean onQueryTextSubmit(String query) {
            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            // Here is where we are going to implement the filter logic
            return true;
        }

    });
}

或作为第二个选项,您让ActivityFragment实现SearchView.OnQueryTextListener,然后使用菜单搜索:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    setHasOptionsMenu(true); // Add this! (as above)
    return inflater.inflate(R.layout.fragment_add_app, container, false);
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    menu.clear();
    inflater.inflate(R.menu.menu_add_app, menu);
    MenuItem item = menu.findItem(R.id.action_search_menu_add_app);
    item.setShowAsAction(MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW | MenuItem.SHOW_AS_ACTION_IF_ROOM);

    SearchView searchView = (SearchView) item.getActionView();
    searchView.setOnQueryTextListener(this); // Fragment implements SearchView.OnQueryTextListener
}

@Override
public boolean onQueryTextChange(String query) {
    // Here is where we are going to implement the filter logic
    return false;
}

@Override
public boolean onQueryTextSubmit(String query) {
    return false;
}

答案 5 :(得分:0)

search_view.setOnQueryTextListener(object: SearchView.OnQueryTextListener {
            override fun onQueryTextSubmit(query: String?): Boolean {
                return false
            }

            override fun onQueryTextChange(query: String?): Boolean {
                adapter.filter.filter(query)
                return true
            }
        })

不要在清单文件中添加活动的意图过滤器。

答案 6 :(得分:-2)

在我的片段中的onCreateOptionsMenu代码

finishActivity(1);

产生以下错误消息: java.lang.NullPointerException:尝试在空对象引用上调用虚方法'void android.support.v7.widget.SearchView.setSearchableInfo(android.app.SearchableInfo)'

我在menu.xml中更改了android:actionViewClass到app:actionViewClass问题没有再次发生。 menu.xml:

SearchManager searchManager =
        (SearchManager)getActivity().getSystemService(Context.SEARCH_SERVICE);
SearchableInfo searchableInfo =
        searchManager.getSearchableInfo(getActivity().getComponentName());
searchItem = menu.findItem(R.id.menu_search);
SearchView searchView = (SearchView) searchItem.getActionView();
searchView.setSearchableInfo( searchableInfo);