Reset the SearchView on fragment change in ViewPager with ActionBar tabs

醉酒当歌 提交于 2019-12-01 06:30:29

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!