I like what Marzieh Heidari suggested , but I would like to add my thoughts about this since I ran into a similar problem and implemented some additional features.
First of all, you might think that there are many ways to scroll, even if the current Fragment does not have a ScrollView . This can happen on some devices, i.e. On creation (it can happen in recreation) in your AppBarLayout you can call requestChildRectangleOnScreen , and if you look at the source code that you find out that under certain circumstances it will automatically shorten your Toolbar (that's what happened to me).
During this, I also noticed that you can drag it to the Toolbar and with scrollFlags="scroll|enterAlways|snap" you can compress (wring) your Toolbar . You can easily test it in your application by simply checking on the Toolbar (even on the FINANCIAL tab).
For these two problems, there is a very simple fix that programmatically changes scrollFlags . After that, you can no longer squeeze your Toolbar (also not by chance), and the system also does not push it.
Programmatically, you can clear scrollFlags by setting them to 0 , and on the other hand, the code will restore them when you switch to another tab.
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { if (position == 1) { view.findViewById(R.id.app_bar_layout).setExpanded(true, true); ((AppBarLayout.LayoutParams) view.findViewById(R.id.toolbar).getLayoutParams()).setScrollFlags(0); } else if (position == 0) ((AppBarLayout.LayoutParams) view.findViewById(R.id.toolbar).getLayoutParams()).setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL | AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS | AppBarLayout.LayoutParams.SCROLL_FLAG_SNAP); } @Override public void onPageScrollStateChanged(int state) { } });
creativecreatorormaybenot
source share