Click here to Skip to main content
15,891,864 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a searchView in Action Bar and some viewPage use Fragment, Tab1 Tab2 Tab3. In Tab1 and Tab3 I use searchView, and tab2 hide it. searchView seems to be working OK, but when I change tab Fragment from Tab1 to Tab2 and return Tab1, searchView in tab1 not working. When change Tab1 to Tab2 to Tab3 and return Tab2 and Tab1, that's OK, searchView worked. So unintelligible. It's MainActivity:
<pre lang="java">
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_item, menu);
    this.menu = menu;
    MenuItem item = menu.findItem(R.id.action_search);
    searchView.setMenuItem(item);
    return true;
}
 viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}
        @Override
        public void onPageSelected(int position) {if(position==0){searchView.setVisibility(View.VISIBLE);
 MenuItem item2 = menu.findItem(R.id.action_search);
 item2.setIcon(R.drawable.ic_action_action_search);
 item2.setVisible(true);
 toolbar.setTitle("Danh Sách Bài Hát");
  } if(position==1){
 searchView.setVisibility(View.INVISIBLE);
 searchView.clearFocus();
 searchView.closeSearch();
 MenuItem item2 = menu.findItem(R.id.action_search);
 item2.setIcon(null);
 toolbar.setTitle("Bài Hát Yêu Thích");
 }if(position==2){//searchView.setVisibility(View.INVISIBLE);
searchView.setVisibility(View.VISIBLE);
MenuItem item2 = menu.findItem(R.id.action_search);
item2.setIcon(R.drawable.ic_action_action_search);
item2.setVisible(true);
toolbar.setTitle("Địa điểm âm nhạc");} if(position==3){searchView.setVisibility(View.INVISIBLE);MenuItem item2 = menu.findItem(R.id.action_search);item2.setVisible(false);                   toolbar.setTitle("Hướng dẫn sử dụng");
            }}
        @Override
        public void onPageScrollStateChanged(int state) {

        }

    });
    tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(viewPager);

    setupTabIcons();
}


What I have tried:

I try to build android app what show listview in fragment tab. I have searchview in actionbar but it not working correctly.
Posted
Comments
Richard MacCutchan 2-Mar-17 3:15am    
"but it not working correctly."
That could mean anything.

Why not get rid of all these fragments, and work on the SearchView in isolation? Once you have that working then you can start building that logic into your fragments.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900