Created
October 11, 2017 07:29
-
-
Save jie-meng/eadaf518121f68f45cb19e3e262ad469 to your computer and use it in GitHub Desktop.
ViewPager invalidate ActionBar
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { | |
@Override | |
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { | |
} | |
@Override | |
public void onPageSelected(int position) { | |
invalidateActionbar(position); | |
} | |
@Override | |
public void onPageScrollStateChanged(int state) { | |
} | |
}); | |
private void invalidateActionbar(int position) { | |
Fragment currentPage = getChildFragmentManager().findFragmentByTag("android:switcher:" + R.id.viewpager + ":" + viewPager.getCurrentItem()); | |
if (currentPage == null || currentPage.getView() == null) { | |
return; | |
} | |
Toolbar toolbar = (Toolbar) currentPage.getView().findViewById(R.id.toolbar); | |
if (toolbar == null) { | |
return; | |
} | |
((AppCompatActivity) getActivity()).setSupportActionBar(toolbar); | |
for (int i=0; i<viewPagerAdapter.getCount(); i++) { | |
Fragment fragment = getChildFragmentManager().findFragmentByTag("android:switcher:" + R.id.viewpager + ":" + i); | |
fragment.setHasOptionsMenu(i == position); | |
} | |
getActivity().invalidateOptionsMenu(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment