Skip to content

Instantly share code, notes, and snippets.

@root-ansh
Created July 1, 2018 19:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save root-ansh/6cf31c034f40fe495569cba0bc09e810 to your computer and use it in GitHub Desktop.
Save root-ansh/6cf31c034f40fe495569cba0bc09e810 to your computer and use it in GitHub Desktop.
CustomNavDrawer1.3
<!--
Basically, we have to add whole of our content view along with the toolbars inside a temporary _holder_ .
And as our drawer slides,
we will be scaling down the height of this holder in a relative factor, by overriding 'on drawer slide(){…}'
-->
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
...>
<LinearLayout <--------holder layout
android:id="@+id/holder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<include
layout="@layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<...>
</...>
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
...
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
final LinearLayout holder=findViewById(R.id.holder);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close)
{
@Override
public void onDrawerSlide(View drawerView, float slideOffset)
{
//this code-block is the real player behind this beautiful ui
// basically, it's a mathemetical calculation which handles the shrinking of
// our content view.
float scaleFactor = 7f;
float slideX = drawerView.getWidth() * slideOffset;
holder.setTranslationX(slideX);
holder.setScaleX(1 - (slideOffset / scaleFactor));
holder.setScaleY(1 - (slideOffset / scaleFactor));
super.onDrawerSlide(drawerView, slideOffset);
}
};
...
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment