@Override protected void onPostExecute(Void v) { mPager.setAdapter(mPagerAdapter); mPagerAdapter.notifyDataSetChanged(); mPager.setCurrentItem(sFavoriteIndex); if (PrefHelper.subtitleEnabled() && MainActivity.sCurrentFragment == R.id.nav_favorites) { mActionBar.setSubtitle(String.valueOf(sFavorites[sFavoriteIndex])); } Toolbar toolbar = ((MainActivity) getActivity()).toolbar; if (toolbar.getAlpha() == 0) { toolbar.setTranslationY(-300); toolbar.animate().setDuration(300).translationY(0).alpha(1); View view; for (int i = 0; i < toolbar.getChildCount(); i++) { view = toolbar.getChildAt(i); view.setTranslationY(-300); view.animate().setStartDelay(50 * (i + 1)).setDuration(70 * (i + 1)).translationY(0); } } }
@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); } } } }); }