2016年2月28日 星期日

Scroll parallel

scroll parallel
that mean different layer view scroll in different speed

some basic knowledge:

use  onScrolled(RecyclerView recyclerView, int dx, int dy) in OnScrollListener can easily get the amount of  y and x that scrolled.

getHeight get the view height

setTranslationY can set the y location of the view

the different between setY and setTranslationY

getY and getTranslationY a all get the location of Y,but
getY:the location is rlative his father
getTranslationY:the location is is relative itself,so if the view dont move,getTranslationY must be 0

now,talk about the concept of moving the app bar

in onScrolled,we can’t get how much Y we moved
up is +,down is -

if we scroll up
the bat move app together
parallax_bar.getTranslationY() - dy / 2,dy/2 because we want the bar move more low the we scroll,adn we need to limit the bar move,the location of the bar conden’t<the -(bar height),because we want the bar come down fast when we just scroll down a little.

if we scroll down
same (parallax_bar.getTranslationY() - dy / 2,could not move do more tha its orginal location 0.

here is the answer code:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {

           mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
               @Override
               public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                   super.onScrolled(recyclerView, dx, dy);
                   Log.d("onScrolled", "dy: " + dy + "");
                   if (parallax_bar != null) {
                       int maxH = parallax_bar.getHeight();
                       Log.d("onScrolled", "maxh: " + maxH + "");
                       if (dy > 0) {
                           //scroll up
                           parallax_bar.setTranslationY(Math.max(parallax_bar.getTranslationY() - dy / 2, -maxH));
                       } else {
                           //scroll down
                           parallax_bar.setTranslationY(Math.min(parallax_bar.getTranslationY() - dy / 2, 0));
                       }

                   }

               }
           });
       }

only can do in when the device android version is > HONEYCOMB and using recycle view.
that mean different layer view scroll in different speed


some basic knowledge:


use  onScrolled(RecyclerView recyclerView, int dx, int dy) in OnScrollListener can easily get the amount of  y and x that scrolled.


getHeight get the view height


setTranslationY can set the y location of the view


the different between setY and setTranslationY


getY and getTranslationY a all get the location of Y,but
getY:the location is rlative his father
getTranslationY:the location is is relative itself,so if the view dont move,getTranslationY must be 0


now,talk about the concept of moving the app bar


in onScrolled,we can’t get how much Y we moved
up is +,down is -


if we scroll up
the bat move app together
parallax_bar.getTranslationY() - dy / 2,dy/2 because we want the bar move more low the we scroll,adn we need to limit the bar move,the location of the bar conden’t<the -(bar height),because we want the bar come down fast when we just scroll down a little.


if we scroll down
same (parallax_bar.getTranslationY() - dy / 2,could not move do more tha its orginal location 0.

here is the answer code:


if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {


           mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
               @Override
               public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                   super.onScrolled(recyclerView, dx, dy);
                   Log.d("onScrolled", "dy: " + dy + "");
                   if (parallax_bar != null) {
                       int maxH = parallax_bar.getHeight();
                       Log.d("onScrolled", "maxh: " + maxH + "");
                       if (dy > 0) {
                           //scroll up
                           parallax_bar.setTranslationY(Math.max(parallax_bar.getTranslationY() - dy / 2, -maxH));
                       } else {
                           //scroll down
                           parallax_bar.setTranslationY(Math.min(parallax_bar.getTranslationY() - dy / 2, 0));
                       }

                   }


               }
           });
       }


only can do in when the device android version is > HONEYCOMB and using recycle view.