Created
July 1, 2018 19:21
-
-
Save root-ansh/6cf31c034f40fe495569cba0bc09e810 to your computer and use it in GitHub Desktop.
CustomNavDrawer1.3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- | |
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> | |
<...> | |
</...> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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