Reset the SearchView on fragment change in ViewPager with ActionBar tabs

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.


Android give you methods like onCreate and onDestroy to work with activity and also for the menus onCreateOptionsMenu and onDestoryOptionsMenu . You can use onDestroyOptionsMenu method to clear your SearchView or to realize your another ideas.

for example:

    @Override
    public void onDestroyOptionsMenu() {
       super.onDestroyOptionsMenu();

       if (searchView != null && 
          !searchView.getQuery().toString().isEmpty()) {

          searchView.setIconified(true);
          searchView.setIconified(true);
       }
    }

It works for me, good Luck.

链接地址: http://www.djcxy.com/p/92946.html

上一篇: 如何使用SearchView过滤RecyclerView

下一篇: 使用ActionBar选项卡重置ViewPager中的SearchView片段更改