【问题标题】:Search View Suggestions doesn't work with 2 toolbars搜索视图建议不适用于 2 个工具栏
【发布时间】:2017-11-06 06:23:52
【问题描述】:

早安先生

我能够重现 WhatsApp 搜索栏,如下图所示:

为此,我创建了两个工具栏和两个菜单,如下所示:

menu_item.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:icon="@drawable/ic_search"
        android:title="@string/action_search"
        app:showAsAction="always"  />
</menu> 

menu_search.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_filter_search"
        android:icon="@drawable/ic_search"
        android:title="Search"
        app:actionViewClass="android.support.v7.widget.SearchView"
        app:showAsAction="collapseActionView|always" />
</menu>

activity_main.xml 中的工具栏

                <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:layout_alignParentTop="true"
                android:background="?attr/colorPrimary"
                app:layout_scrollFlags="scroll|enterAlways"
                android:fitsSystemWindows="true"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

search_toolbar.xml 中的工具栏

    <?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/searchtoolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:layout_alignParentTop="true"
    app:layout_scrollFlags="scroll|enterAlways"
    android:background="@color/colorTextPrimary"
    android:fitsSystemWindows="true"
    app:collapseIcon="@drawable/ic_arrow_back"
    app:titleTextColor="@color/colorPrimary"
    /> 

我实现了 SearchRecentSuggestions 但它不起作用,做一些测试我发现它只有在我直接在 onCreateMenuOptions 中使用 menu_search 中的膨胀时才起作用,但是,我最终失去了图像中显示的动画下面:

Case 1

Case 2

使用的代码

    @Override
public boolean onCreateOptionsMenu(final Menu menu) {

    getMenuInflater().inflate(R.menu.menu_search, menu);

    SearchManager searchManager=(SearchManager)getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView = (SearchView) menu.findItem(R.id.action_filter_search).getActionView();
    sView = searchView;
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    searchView.setMaxWidth(Integer.MAX_VALUE);
    return super.onCreateOptionsMenu(menu);
} 

我尝试在 menu_items 之后为 menu_search 充气,但也没有得到预期的结果。

    @Override
public boolean onCreateOptionsMenu(final Menu menu) {

    getMenuInflater().inflate(R.menu.menu_items, menu);

    Toolbar searchTool = findViewById(R.id.searchtoolbar);
    searchTool.inflateMenu(R.menu.menu_search);
    Menu searchM = searchTool.getMenu();
    SearchView itemS = (SearchView)searchM.findItem(R.id.action_filter_search).getActionView();
    SearchManager searchManager=(SearchManager)getSystemService(Context.SEARCH_SERVICE);
    sView = itemS;
    itemS.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    itemS.setMaxWidth(Integer.MAX_VALUE);
    return true;
}

Case 5

分析一下,我发现它并没有注册我在 SearchView 中输入的内容,因为在我从正常测试返回后,注册了几个单词后,它们与动画一起出现在设计中。

Case 3

有没有可能我可以在第二个工具栏中扩充两个菜单或启用此注册表并实现此目的?

Case 4

感谢您的帮助

【问题讨论】:

    标签: android xml searchview search-suggestion


    【解决方案1】:

    我找到了解决这个问题的方法。

    首先在onCreateMenuOptions中是不可能膨胀两个菜单的,所以要在这种情况下实现SearchSuggestion的行为,只需在我的例子中创建一个方法handleSearch并放入SearchListener,搜索建议的问题就会得到解决。

    【讨论】:

      猜你喜欢
      • 2011-07-29
      • 1970-01-01
      • 2017-08-16
      • 2021-11-02
      • 2018-10-02
      • 2016-10-30
      • 2016-10-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多