android - CoordinatorLayout with multiple snapping points -


here i've got quite complex animation may resolved (i believe) in simple way using coordinatorlayout. has 3 states:

  1. initial (left screen) - header view shown (orange background): toolbar, grey roundrect (it's photo there) plus other views below (textviews, ratingbar etc)
  2. scrolling content (middle screen) - roundrect zooming changing green foreground alpha level on it, becomes green while scrolling (well, not obvious these screens. green background zoomed roundrect green foreground on it, , cause header background becomes green , not orange)
  3. scrolling once more (right screen) - rest of header should scrolled up

scrolling down content should lead appearing of views in reverse way accordingly.

coordinatorlayout states

i had experience working coordinatorlayout, i'm not sure understand how handle 2 anchor points. understand how scroll flags work , zooming (p. 2) , changing foreground alpha need custom behavior implementation, cannot understand how shall handle of in complex.

all i've found far saúl molinero's tutorial , this tutorial examples.

so please sorry poor description here, i'll update question of course , add source code when have success issue, i'd glad hints maybe or tutorials i've missed. hope had similar in projects.

here's test repo code , here link layout.xml file.

you can 2 snapping points setting scroll flags follows:

<android.support.design.widget.collapsingtoolbarlayout     ...stuff...     app:layout_scrollflags="scroll|enteralways|snap"> 

so, expanded 1 stopping point , toolbar visible second stopping point. when view scrolled further, toolbar disappears. how want things work when scrolling up.

now when app bar collapsed, app bar start showing when scrolling down. not surprise, since enteralways does. if top of content has been scrolled out of view, won't see again until after app bar expanded. so, if behavior want, we'll stop there.

however, think want exiting behavior outlined above different entry behavior. late entry behavior if set scroll flags follows:

<android.support.design.widget.collapsingtoolbarlayout     ...stuff...     app:layout_scrollflags="scroll|snap"> 

this deleted enteralways flag. these scroll flags, app bar not reappear (once collapsed) until top of content visible , "pulls" app bar view.

so, 1 solution (of many) write a new behavior attached appbarlayout code change scroll flags once app bar collapsed , change them opens again. way can change behavior want , still use android machinery figure out specific operations @ view level. can done in custom view or in activity - wherever have access scroll state of app bar , scrolling flags. can done in behavior not best place it.

oh, , have discovered, snapping janky on api 26.


here implementation of concept. simplicity, implementation in activity:

enter image description here

scrollingactivity.java

public class scrollingactivity extends appcompatactivity {      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_scrolling);          final appbarlayout appbar = (appbarlayout) findviewbyid(r.id.app_bar);          appbar.post(new runnable() {             @override             public void run() {                 collapsingtoolbarlayout toolbarlayout =                     (collapsingtoolbarlayout) findviewbyid(r.id.toolbar_layout);                 setupappbar(appbar, toolbarlayout);             }         });     }      private void setupappbar(appbarlayout appbar, final collapsingtoolbarlayout toolbarlayout) {         // scroll range positive offsets negative. make signs agree camparisons.         final int mscrollrange = -appbar.gettotalscrollrange();          appbar.addonoffsetchangedlistener(new appbarlayout.onoffsetchangedlistener() {             private boolean mappbarcollapsed = false;              @override             public void onoffsetchanged(appbarlayout appbarlayout, int verticaloffset) {                 if (verticaloffset == mscrollrange) { // app bar collapsed                     mappbarcollapsed = true;                     appbarlayout.layoutparams lp =                         (appbarlayout.layoutparams) toolbarlayout.getlayoutparams();                     int flags = lp.getscrollflags()                         & ~appbarlayout.layoutparams.scroll_flag_enter_always;                     lp.setscrollflags(flags);                     toolbarlayout.setlayoutparams(lp);                 } else if (mappbarcollapsed) { // app bar opening                     mappbarcollapsed = false;                     appbarlayout.layoutparams lp =                         (appbarlayout.layoutparams) toolbarlayout.getlayoutparams();                     int flags = lp.getscrollflags()                         | appbarlayout.layoutparams.scroll_flag_enter_always;                     lp.setscrollflags(flags);                     toolbarlayout.setlayoutparams(lp);                 }             }         });     } } 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -