android - Synchronizing Toolbar Height with ViewPager -
i have activity toolbar , viewpager, in viewpager there 3 fragments, every fragment should set different height toolbar means when 1st fragment selected fragment toolbar's height should x , when 2nd fragment selected toolbar height should y , so.
now want changing of toolbar height synchronized viewpager scrolling, advice that?
here output ..
for sample code https://github.com/msquare097/mflex-toolbar ,....
first of nice question , interesting... , implement it.
have used toolbar minimum height changing on viewpager scroll.
first of declare toolbar
height respect fragments in activity viewpager
implemented.
i have take directly integer
. load dimen dp , convert px. in viewpager have taken 4 fragment. declaring 4 toolbar's height.
int heightforpage0 = 150; int heightforpage1 = 400; int heightforpage2 = 300; int heightforpage3 = 600;
after setting adapter add listener
mviewpager.addonpagechangelistener(this);
and override 3 method , write below code..
@override public void onpagescrolled(int position, float positionoffset, int positionoffsetpixels) { log.d(tag,positionoffset+""); int currentheight; int nextheight; switch (position) { case 0: currentheight = heightforpage0; nextheight = heightforpage1; calculateheightandapply(currentheight, nextheight, positionoffset); break; case 1: currentheight = heightforpage1; nextheight = heightforpage2; calculateheightandapply(currentheight, nextheight, positionoffset); break; case 2: currentheight = heightforpage2; nextheight = heightforpage3; calculateheightandapply(currentheight, nextheight, positionoffset); break; case 3: // last page don't have worry it. // bcoz there no next page available; break; } } @override public void onpageselected(int position) {} @override public void onpagescrollstatechanged(int state) {}
and here magical method have called in onpagescrolled
..
private void calculateheightandapply(int currentheight, int nextheight, float positionoffset) { if (positionoffset==0) { return; } int diff = nextheight - currentheight; int newheight = (int) ((positionoffset*diff)); mtoolbar.setminimumheight(currentheight+newheight); }
Comments
Post a Comment