@TargetApi(Build.VERSION_CODES.LOLLIPOP)
 private void toolbarSetElevation(float elevation) {
   // setElevation() only works on Lollipop
   if (AndroidUtils.isLollipop()) {
     tToolbar.setElevation(elevation);
   }
 }
 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
 @Override
 protected void onSaveInstanceState(Bundle outState) {
   if (AndroidUtils.isLollipop()) {
     outState.putFloat(STATE_TOOLBAR_ELEVATION, tToolbar.getElevation());
   }
   outState.putFloat(STATE_TOOLBAR_TRANSLATION_Y, tToolbar.getTranslationY());
   outState.putInt(STATE_VERTICAL_OFFSET, verticalOffset);
   outState.putInt(STATE_SCROLLING_OFFSET, scrollingOffset);
   outState.putParcelable(STATE_RECYCLER_VIEW, rvCities.getLayoutManager().onSaveInstanceState());
   super.onSaveInstanceState(outState);
 }
  @TargetApi(Build.VERSION_CODES.LOLLIPOP)
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);

    // Assign Toolbar to the activity
    tToolbar = (Toolbar) findViewById(R.id.tToolbar);
    setSupportActionBar(tToolbar);
    getSupportActionBar().setTitle(R.string.app_name);

    // RecyclerView with sample data
    rvCities = (RecyclerView) findViewById(R.id.rvCities);
    rvCities.setLayoutManager(new LinearLayoutManager(this));
    rvCities.setAdapter(new CitiesAdapter(CityUtils.CITIES));

    if (savedInstanceState != null) {
      if (AndroidUtils.isLollipop()) {
        tToolbar.setElevation(savedInstanceState.getFloat(STATE_TOOLBAR_ELEVATION));
      }
      tToolbar.setTranslationY(savedInstanceState.getFloat(STATE_TOOLBAR_TRANSLATION_Y));
      verticalOffset = savedInstanceState.getInt(STATE_VERTICAL_OFFSET);
      scrollingOffset = savedInstanceState.getInt(STATE_SCROLLING_OFFSET);
      rvCities
          .getLayoutManager()
          .onRestoreInstanceState(savedInstanceState.getParcelable(STATE_RECYCLER_VIEW));
    }

    // We need to detect scrolling changes in the RecyclerView
    rvCities.addOnScrollListener(
        new RecyclerView.OnScrollListener() {
          @Override
          public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            if (newState == RecyclerView.SCROLL_STATE_IDLE) {
              if (scrollingOffset > 0) {
                if (verticalOffset > tToolbar.getHeight()) {
                  toolbarAnimateHide();
                } else {
                  toolbarAnimateShow(verticalOffset);
                }
              } else if (scrollingOffset < 0) {
                if (tToolbar.getTranslationY() < tToolbar.getHeight() * -0.6
                    && verticalOffset > tToolbar.getHeight()) {
                  toolbarAnimateHide();
                } else {
                  toolbarAnimateShow(verticalOffset);
                }
              }
            }
          }

          @Override
          public final void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            verticalOffset = rvCities.computeVerticalScrollOffset();
            scrollingOffset = dy;
            int toolbarYOffset = (int) (dy - tToolbar.getTranslationY());
            tToolbar.animate().cancel();
            if (scrollingOffset > 0) {
              if (toolbarYOffset < tToolbar.getHeight()) {
                if (verticalOffset > tToolbar.getHeight()) {
                  toolbarSetElevation(TOOLBAR_ELEVATION);
                }
                tToolbar.setTranslationY(-toolbarYOffset);
              } else {
                toolbarSetElevation(0);
                tToolbar.setTranslationY(-tToolbar.getHeight());
              }
            } else if (scrollingOffset < 0) {
              if (toolbarYOffset < 0) {
                if (verticalOffset <= 0) {
                  toolbarSetElevation(0);
                }
                tToolbar.setTranslationY(0);
              } else {
                if (verticalOffset > tToolbar.getHeight()) {
                  toolbarSetElevation(TOOLBAR_ELEVATION);
                }
                tToolbar.setTranslationY(-toolbarYOffset);
              }
            }
          }
        });
  }