重置搜索查看与标签的ActionBar在ViewPager片段变化 [英] Reset the SearchView on fragment change in ViewPager with ActionBar tabs

查看:214
本文介绍了重置搜索查看与标签的ActionBar在ViewPager片段变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现了两个片段与动作条的标签。我使用的福尔​​摩斯片段。当我开始一个片段,我创建了操作栏上搜索查看。

问题是:当我搜索的第一个片段,但不关闭搜索查看或删除文字的东西,我移动到下一个片段,我得到一个折叠的搜索查看,这是罚款。但是,当我再次回到我的第一个片段,我看到一个列表排序的前一样,但搜索查看已展开。找回原来的列表中的唯一方法是点击一次和关闭它的搜索查看。

我如何重置搜索查看的第一个片段,当我移动到第二个片段或如何可以保持搜索查看开的时候我来到了第一个片段?

解决方案中的任何一种都行。

 <菜单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
的xmlns:程序=htt​​p://schemas.android.com/apk/res-auto
的xmlns:工具=htt​​p://schemas.android.com/tool​​s
 ><项目机器人:ID =@ + ID / ACTION_SEARCH
    机器人:标题=搜索
    机器人:图标=@绘制/搜索
    机器人:orderInCategory =100
    机器人:showAsAction =总是| collapseActionView
    机器人:actionViewClass =com.actionbarsherlock.widget.SearchView/>&所述; /菜单>

这是我怎么在片段充气

  @覆盖
        公共无效onCreateOptionsMenu(菜单菜单,MenuInflater气筒)
        {
        super.onCreateOptionsMenu(菜单,充气器);        inflater.inflate(R.menu.main,菜单);
        搜索查看SV =(搜索查看)getActivity()findViewById(R.id.action_search)。
          sv.setOnQueryTextListener(新OnQueryTextListener(){
              @覆盖
              公共布尔onQueryTextSubmit(查询字符串){
                  // ...
                  返回false;
              }
              @覆盖
              公共布尔onQueryTextChange(字符串newText){
                  bindingData.resetData();
                  。bindingData.getFilter()过滤(newText.toString());
                  返回false;
              }
          });
    }


解决方案

对于那些谁仍然摸不着头脑,并且不能够找到一个解决方案,在这里你怎么做。这是pretty简单:

在创建它(通过创建ids.xml和保留的ID存在和运行时分配给它)一个ID只是分配给searchviews。
现在onTabUnselected()主选项卡活动的方法,确定searchviews和使用功能setIconified(真)两次(一次删除文本和一个折叠搜索查看)。

不要忘记添加为您在您的搜索查看的onQueryTextChange方法无效的查询字符串,它开创了我的问题。

I have implemented two fragments with ActionBar tabs. I am using Sherlock fragments. When I start a fragment I create a searchview on the action bar.

The problem is: When I search something on the first fragment and without closing the searchview or deleting the text, I move to the next fragment, I get a collapsed searchview, which is fine. But when I again get back to my first fragment, I see a list sorted like before but the searchview is collapsed. The only way to get back the original list is to click the searchview once and closing it.

How do I reset the searchview on the first fragment when I move to second fragment or how can keep the searchview open when I come to the first fragment?

Any one of the solutions will do.

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
 >

<item android:id="@+id/action_search"
    android:title="search"
    android:icon="@drawable/search"
    android:orderInCategory="100"
    android:showAsAction="always|collapseActionView"
    android:actionViewClass="com.actionbarsherlock.widget.SearchView" />

</menu>

This is how I am inflating in the fragment

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

        inflater.inflate(R.menu.main, menu);
        SearchView sv = (SearchView)getActivity().findViewById(R.id.action_search);
          sv.setOnQueryTextListener(new OnQueryTextListener() {
              @Override
              public boolean onQueryTextSubmit(String query) {
                  //...
                  return false;
              }
              @Override
              public boolean onQueryTextChange(String newText) {
                  bindingData.resetData();


                  bindingData.getFilter().filter(newText.toString());
                  return false;
              }
          });
    }

解决方案

For those who are still scratching their heads and are not able to find a solution, here it how you do it. It is pretty simple:

Just assign an ID to your searchviews when you are creating it (by creating the ids.xml and reserving the id there and assigning it on runtime). Now in onTabUnselected() method of the main tab activity, identify the searchviews and use function setIconified(true) twice (one to remove the text and one to collapse the searchview).

Do not forget to add a check for null query string in onQueryTextChange method of your searchview, it created a problem for me.

这篇关于重置搜索查看与标签的ActionBar在ViewPager片段变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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