片段替换在 searchview 上触发 onQueryTextChange

Fragment replacement triggers onQueryTextChange on searchview(片段替换在 searchview 上触发 onQueryTextChange)

本文介绍了片段替换在 searchview 上触发 onQueryTextChange的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这就是我浏览我的应用程序的方式:

This is how I navigate through my app:

  • 用列表打开片段
  • 按在搜索视图中输入的文本过滤列表
  • 点击列表项(列表片段被详细片段替换)
  • 向后导航(详细片段被列表片段替换)

当我从列表导航到细节片段时,我想将搜索视图的当前过滤器保留在字符串变量中.我在执行 onQueryTextChange 时存储了 searchview 的值.

When I navigate from list- to detail-fragment, I want to keep the current filter of the searchview in a string variable. I store the value of the searchview when onQueryTextChange is executed.

问题:我无法存储实际的过滤器值,因为当我从列表导航到详细信息时会调用 onQueryTextChange,因为某些内容清除了搜索视图的文本.

The problem: I can't store the actual filter-value because onQueryTextChange gets called when I navigate from list to detail because something cleared the text of the searchview.

// ...
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String s) {
            return false;
        }

        @Override
        public boolean onQueryTextChange(String s) {
            searchReceived(s);
            return true;
        }
    });
// ...

public void searchReceived(String searchQuery)
{
    this.stateHolder.searchQuery = searchQuery;
    // more code...
}

当我想在返回时恢复过滤器时,它只使用空字符串进行过滤,因为错误的值存储在 this.stateHolder.searchQuery 中.

When I want to restore the filter when navigating back, it just filters with an empty string because the wrong value got stored in this.stateHolder.searchQuery.

堆栈:

onQueryTextChange():139, EmployeeListFragment$1 {com.example.exampleapp.fragment}
onTextChanged():1153, SearchView {android.widget}
access$2000():92, SearchView {android.widget}
onTextChanged():1638, SearchView$11 {android.widget}
sendOnTextChanged():7408, TextView {android.widget}
setText():3816, TextView {android.widget}
setText():3671, TextView {android.widget}
setText():80, EditText {android.widget}
setText():3646, TextView {android.widget}
setQuery():511, SearchView {android.widget}
onActionViewCollapsed():1250, SearchView {android.widget}
collapseItemActionView():1662, ActionBarView$ExpandedActionViewMenuPresenter {com.android.internal.widget}
collapseItemActionView():1258, MenuBuilder {com.android.internal.view.menu}
clear():521, MenuBuilder {com.android.internal.view.menu}
doInvalidatePanelMenu():789, PhoneWindow {com.android.internal.policy.impl}
run():221, PhoneWindow$1 {com.android.internal.policy.impl}

导航时如何防止搜索视图被系统清除?

How can I prevent the searchview from being cleared by the system when navigating?

谢谢.

推荐答案

经过 2 天的谷歌搜索,我得到了绝对能帮助你的解决方案.

After 2 days of Googling i got the solution that will definitely helps you.

我刚刚从 onCreateOptionMenu() 剪切和粘贴代码到 onPrepareOptionMenu()

我不知道为什么会这样,我认为 searchView 的侦听器不会是 NULL 以您可以通过这种方式更改代码.

I do not know why it happens, I think listener of searchView is not going to be NULL by the way you can change your code this way.

@Override
public void onPrepareOptionsMenu(Menu menu) {
    super.onPrepareOptionsMenu(menu);
    getSherlockActivity().getSupportMenuInflater().inflate(R.menu.menu_all_order, menu);

    searchView = (SearchView) menu.findItem(R.id.menu_all_order_search).getActionView();
    searchView.setInputType(InputType.TYPE_CLASS_NUMBER);
    searchView.setQueryHint("Enter Order No");
    searchView.setOnQueryTextListener(this);
}

和删除:

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

谢谢:)

这篇关于片段替换在 searchview 上触发 onQueryTextChange的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:片段替换在 searchview 上触发 onQueryTextChange

基础教程推荐