本文介绍了如果选项卡位置发生更改或通过调用 onPageChangeListener 关闭搜索视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一项活动,从中加载了 6 个片段.三个片段中有六个具有搜索视图,其余三个片段尚未实现搜索视图.

I have one activity from where i am loading 6 fragments.Six of three fragments have the searchview and rest of the three have not implemented the searchview.

我做了两个menu.xml,其中

I have made two menu.xml in which

  • 一个具有用于操作栏和其他的搜索视图和通用项
  • 一个没有搜索视图并包含常见项目

现在,如果我在第一个选项卡中展开搜索视图,然后移动到第二个选项卡,搜索视图仍然会打开(我知道这是第二个选项卡的搜索视图,因为我已经为每个片段实现了 onCreateOptionMenu()),我不想要的.

Now if i expand the searchview in the first tab and then move to the second tab,searchview still get opened(i know it is the searchview of the second tab as i have implemented the onCreateOptionMenu() for the each fragments),which i don't want.

我想要的是当我移动到第一个片段到第二个或第三个时,搜索视图应该折叠并且只单击搜索视图(在当前片段中它应该被展开).

What i want is when i move to the first fragment to second or third at that time searchview should get collapsed and only click of the searchview(in current fragment it should get expanded).

此外,如果我将第一个片段移到第 2、第 3 和第 4 个(我的帐户信息片段,操作栏中不包含搜索视图),那么在第 4 个片段中我仍然在打开模式下获得搜索视图...

Moreover, if i move to the first fragment to 2nd,3rd and 4th(my account info fragment,not contain searchview in action bar),then in 4th fragment i still get the searchview in open mode...

我想知道的另一件事是,通过单击搜索视图,它会展开并弹出软键盘,这是搜索视图的默认行为(扩展和打开键盘)吗?

Another thing that i want to know,by clicking on the searchview it gets expanded and also popup the soft keyboard,so is this is the default behavior of the searchview (expanding and open keyboard)?

我想在单击搜索视图的文本(编辑文本)区域时打开键盘.

I want to open keyboard when i click on the text(edit text) area of the searchview.

希望我清楚....任何建议/链接将不胜感激...

Hope m clear....Any Suggestion/Links will be appreciated...

如果您需要我方提供更多详细信息,请告诉我.....

Let me know if any more detail you required from my side.....

:

应用 S.D. 的解决方案

Applying S.D.'s solution

在 2.3.6 上测试

1)当我在第一个片段(第一个标签)

1)When i am on the first fragment(First Tab)

2) 在同一个片段中展开后 ::

2)After Expanding it in same fragment ::

3) 移动到下一个 Fragment(Tab,保持搜索视图打开) ::

3)Move to the next Fragment(Tab,by keeping the searchview opened) ::

在 4.2.1 上测试 ::

1)当我在第一个片段(第一个标签)

1)When i am on the first fragment(First Tab)

2) 在同一个片段中展开后 ::

2)After Expanding it in same fragment ::

3) 移动到下一个 Fragment(Tab,保持搜索视图打开) ::

3)Move to the next Fragment(Tab,by keeping the searchview opened) ::

我在我的容器活动中设置了setIconified(true)"(从我的所有片段所在的位置)在下面的方法中::

i have set 'setIconified(true)' in my Container Activity(from where my all fragment loded) in below methods ::

@Override
public void onPageSelected(int position)
{
    if (searchView!=null && !searchView.isIconified()) {  //true == searchView closed
        searchView.setIconified(true);
        searchView.setIconified(true);
    }
    actionBar.setSelectedNavigationItem(position);
}

@Override
public void onTabSelected(Tab tabposition, FragmentTransaction fragmentposition) {
    if (searchView!=null && !searchView.isIconified()) {  //true == searchView closed
        searchView.setIconified(true);
        //searchView.setIconified(true);
    }
    awesomePager.setCurrentItem(tabposition.getPosition());
}

推荐答案

搜索视图的实现有点奇怪,我不确定这是一个错误还是预期的功能.要正确折叠 SearchView,您必须调用 setIconified(true) 两次.第一个调用有时会清除文本,第二个调用会实际折叠它.

Search view implementation is a bit strange I'm not sure it is a bug or intended feature. To collapse SearchView properly, you have to call setIconified(true) twice. First call will sometime clear the text, the second will then actually collapse it.

这篇关于如果选项卡位置发生更改或通过调用 onPageChangeListener 关闭搜索视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 23:14