片段置换触发onQueryTextChange上搜索查看 [英] Fragment replacement triggers onQueryTextChange on searchview

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

问题描述

这是我通过我的应用程序是如何导航:

This is how I navigate through my app:

  • 与清单打开片段
  • 在搜索查看输入的文本过滤器列表
  • 轻触列表项(名单片段被换成细节片段)
  • 在导航回(详细片段会被替换成列表片段)

当我从列表 - 细节片段导航,我想保持搜索查看目前的过滤器在一个字符串变量。我存储在执行onQueryTextChange的搜索查看的价值。

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}

我怎样才能prevent从航行时被清除,系统会将该搜索查看?

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.

我刚刚剪切和粘贴 code从 onCreateOptionMenu()在prepareOptionMenu()

I have just cut and pasted code from onCreateOptionMenu() to onPrepareOptionMenu()

我不知道为什么会发生,我觉得搜索查看是不会被监听器 NULL 的这样你可以改变你的code这个样子。

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);
}

感谢:)

这篇关于片段置换触发onQueryTextChange上搜索查看的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆