有没有更好的方式来恢复搜索查看状态?状态、方式

2023-09-05 06:38:44 作者:故事太长太心酸

我在动作条可折叠搜索查看。搜索后已经完成,相关的的ListView 过滤,只显示匹配的项目。在此状态下,搜索查看仍在扩大,显示搜索字符串。如果用户关闭了搜索查看,我删除的过滤器。

I have a collapsible SearchView in my ActionBar. After a search has been performed, the associated ListView is filtered to show only matching items. During that state, the SearchView is still expanded and shows the search string. If the user closes the SearchView, I remove the filter.

我要恢复搜索状态,例如,在配置更改或者当我回到它从另一个活动的活性被破坏了。

I want to restore the search state, e.g. on configuration change or if the activity was destroyed when I return to it from another activity.

我恢复在查询字符串 onRestoreInstanceState(),如果我找到 onCreateOptionsMenu查询字符串()我称之为

I restore the query string in onRestoreInstanceState(), and if I find a query string in onCreateOptionsMenu() I call

searchView.setQuery(query, true); 

以便再执行查询。原来,这是不是马上 onRestoreInstanceState()应用查询过滤器更好。与后者的列表不久示出未过滤,才把该查询再次应用。随着 setQuery()不会发生这种情况。

so as to execute the query again. It turned out that this is better than applying the query filter immediately onRestoreInstanceState(). With the latter the list is shortly shown unfiltered, only then the query is applied again. With setQuery() this does not happen.

问题:执行查询,列表被过滤,但搜索视图保持倒塌。因此,用户不能使用搜索视图去除过滤器或应用其他查询。

Problem: The query is executed and the list is filtered, but the search view remains collapsed. Therefore the user cannot use the search view to remove the filter or to apply another query.

onCreateOptionsMenu()我可以肯定的是,搜索项和搜索视图存在,因此,我可以叫 searchItem.expandActionView()。奇怪的是,只有这真的扩大了而ActionView - 调用 setIconified(假)不扩张的观点,即使是在它在一排被称为两次。

In onCreateOptionsMenu() I can be sure that the search item and the search view exists, therefore I can call searchItem.expandActionView(). Strangely, only this really expands the ActionView - calling setIconified(false) does not expand the view, not even when it is called twice in a row.

如果我用 expandActionView()之前,我称之为 setQuery()搜索查看打开,显示的文字(否则 expandActionView()清空搜索查看)。

If I use expandActionView() before I call setQuery(), the SearchView is opened and shows the text (otherwise expandActionView() empties the SearchView).

不幸的是, expandActionView()有一个副作用:建议列表也显示和键盘将打开。

Unfortunately, expandActionView() has a side effect: the suggestion list is also shown and the keyboard opens.

我可以隐藏使用键盘 searchView.clearFocus()。所以,剩下的问题是建议列表。如何关闭一个开放的建议列表中的搜索查看有文字?

I can hide the keyboard using searchView.clearFocus(). So the remaining problem is the suggestion list. How can I close an open suggestion list on a SearchView that has text?

我不知道是否有更好的方法来恢复操作栏中搜索视图,它不会有这么多的副作用。

I wonder if there is a better way to restore a search view in the action bar that does not have so many side effects.

推荐答案

我已经找到了解决办法。这是非常丑陋的,但它的作品。

I have found a workaround. It is extremely ugly, but it works.

因此​​,这里是我如何能够配置更改后恢复搜索框实例状态。

So here is how I was able to restore the search box instance state after a configuration change.

首先,恢复在onRestoreInstanceState查询字符串

First of all, restore the query string in onRestoreInstanceState

currentQuery = state.getString(KEY_SAVED_FILTER_CONSTRAINT);

在onCreateOptionsMenu设置搜索视图,如果有一个currentQuery,扩展搜索项并再次提交查询。清除焦点隐藏键盘。

In onCreateOptionsMenu set up the search view and if there is a currentQuery, expand the search item and submit the query again. Clear the focus to hide the keyboard.

    MenuItem searchItem = menu.findItem(R.id.action_search);
    searchView = (SearchView) searchItem.getActionView();
    searchView.setSearchableInfo(searchManager
            .getSearchableInfo(getComponentName()));

    if (!TextUtils.isEmpty(currentQuery)) {
        searchItem.expandActionView();
        searchView.setQuery(currentQuery, true);
        searchView.clearFocus();
    }

最后,我们需要关闭建议列表。下面是如何从搜索视图的查询文本视图:

Finally we need to close the suggestion list. Here is how to get the query text view from the search view:

private AutoCompleteTextView findQueryTextView(ViewGroup viewGroup) {
    AutoCompleteTextView queryTextView = null;
    if (viewGroup != null) {
        int childCount = viewGroup.getChildCount();
        for (int i = 0; i < childCount; i++) {
            View child = viewGroup.getChildAt(i);
            if (child instanceof AutoCompleteTextView) {
                queryTextView = (AutoCompleteTextView) child;
                break;
            } else if (child instanceof ViewGroup) {
                queryTextView = findQueryTextView((ViewGroup) child);
                if (queryTextView != null) {
                    break;
                }
            }
        }
    }
    return queryTextView;
} 

然后就可以关闭该建议列表:

Then you can dismiss the suggestion list:

    AutoCompleteTextView queryTextView = findQueryTextView(searchView);
    if (queryTextView != null) {
        queryTextView.dismissDropDown();
    }

这不但是没有从内部onCreateOptionsMenu工作。我不得不dismissDropDown的调用移动到我的活动onNewIntent方法,使其工作。

This does however not work from within onCreateOptionsMenu. I had to move the invocation of dismissDropDown into the onNewIntent method of my activity to make it work.

使这种所以难看的事实是,在恢复步骤分散在生命周期的各个阶段,该递归观看搜索是必要去建议列表。

What makes this so ugly is the fact that the restoration steps are dispersed over various phases of the lifecycle and that a recursive view search is necessary to get to the suggestion list.