android - How change ActionBar colour when swiping between fragments (Material Design)? -



android - How change ActionBar colour when swiping between fragments (Material Design)? -

i have app doing, has fragmentpageradapter setup 3 fragments swipe between on main page.

i have been trying setup swipe between fragments, action bar changes color (fades in , out).

however not sure how should this. method called when swipe between fragments? i.e should set code alter action bar colour?

and how fade in , out effect (so fades out 1 colour one)?

i appreciate anyones help.

thanks in advance

cheers corey :)

what method called when swipe between fragments?

you're looking viewpager.onpagechangelistener.onpagescrolled. give you:

position position index of first page beingness displayed. positionoffset value [0, 1) indicating offset page @ position. positionoffsetpixels value in pixels indicating offset position.

although, you'll need first 2 parameters. you'll want bind particular color each of fragments, retrieve both current page , next page colors, blend them using positionoffset ratio create new actionbar background.

a useful algorithm blending 2 colors based on ratio can found in google's new slidingtabstrip example. 0.0 homecoming sec color, 0.5 homecoming blend, , 1.0 homecoming first color

static int blendcolors(int from, int to, float ratio) { final float inverseration = 1f - ratio; final float r = color.red(from) * ratio + color.red(to) * inverseration; final float g = color.green(from) * ratio + color.green(to) * inverseration; final float b = color.blue(from) * ratio + color.blue(to) * inverseration; homecoming color.rgb((int) r, (int) g, (int) b); }

here's simple example:

colorfragment

public class colorfragment extends fragment { private static final string key_color = "colorfragment:color"; /** empty constructor per {@link fragment} docs */ public colorfragment() { } public static colorfragment newinstance(int color) { final bundle args = new bundle(); args.putint(key_color, color); final colorfragment fragment = new colorfragment(); fragment.setarguments(args); homecoming fragment; } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { final framelayout rootview = new framelayout(getactivity()); rootview.setbackgroundcolor(getarguments().getint(key_color)); homecoming rootview; } public int getcolor() { homecoming getarguments().getint(key_color); } }

pulling together

@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); // set actionbar background final colordrawable actionbarbackground = new colordrawable(); getsupportactionbar().setbackgrounddrawable(actionbarbackground); ... final pageradapter pageradapter = ...; ... // bind info pageradapter ... final viewpager pager = ...; pager.setonpagechangelistener(new viewpager.simpleonpagechangelistener() { @override public void onpagescrolled(int position, float positionoffset, int positionoffsetpixels) { super.onpagescrolled(position, positionoffset, positionoffsetpixels); if (position >= pageradapter.getcount() - 1) { // guard against arrayindexoutofboundsexception return; } // retrieve current , next colorfragment final colorfragment = (colorfragment) pageradapter.getitem(position); final colorfragment = (colorfragment) pageradapter.getitem(position + 1); // blend colors , adjust actionbar final int blended = blendcolors(to.getcolor(), from.getcolor(), positionoffset); actionbarbackground.setcolor(blended); } }); pager.setadapter(pageradapter); }

results

http://gfycat.com/cautiousbewitchedjabiru

i hope helps out some!

android android-actionbar fragmentpageradapter material-design

Comments

Popular posts from this blog

assembly - What is the addressing mode for ld, add, and rjmp instructions? -

vowpalwabbit - Interpreting Vowpal Wabbit results: Why are some lines appended by "h"? -

Is there a way to convert an HTML page styled with Bootstrap CSS into email-compatible html? -