How to implement a DrawerLayout with a visible handle

后端 未结 2 1336
囚心锁ツ
囚心锁ツ 2020-12-04 18:17

I have successfully implemented a NavigationDrawer for my application.

My app displays a drawer that opens on the left of the screen.

My problem is I need t

2条回答
  •  感动是毒
    2020-12-04 18:26

    It's quite tricky.

    I think this is similar to what is done to the drawer handle in new Google Maps app. Don't take my word, not sure. :)

    I have written toggle that stays on the edge of the Activity content view. When the DrawerLayout is dragged, I translated the view on the x-axis by the amount of the minimal child (which is DrawerLayout content view) minus the shadow (if any). Since the shadow casted + content view of the DrawerLayout gives the full measured width of the entire drawer.

    I quickly multiply the slided offset and the minimal child and find the x translation.

    [Edit: Code has been removed for readability and it has been moved to the link provided below]

    In your activity:

    mDrawerToggle = new DrawerLayoutEdgeToggle(
        this, 
        mDrawerLayout, 
        R.drawable.ic_launcher, 
        R.drawable.ic_launcher,
        Gravity.LEFT, 
        true) {
    
            @Override
            public void onDrawerClosed(View view) {
                super.onDrawerClosed(view); //must call super
            }
    
            @Override
            public void onDrawerOpened(View view) {
                super.onDrawerOpened(view); //must call super
            }
    
            @Override
            public void onDrawerSlide(View view, float slideOffset) {
                super.onDrawerSlide(view, slideOffset); //must call super
    
            }
        };
    mDrawerLayout.setDrawerListener(mDrawerToggle);
    

    Creativity boost: You can add more cosmetics like distance from ActionBar which you can set as margin to the handle.

    Also you can mimic "single-zipper effect" by moving the handle up/down along left/right just by translating on the Y axis. :)

    Edit: Its available on my GitHub here

    Edit 2: For those that can't get the handle appear at the beginning just use mDrawerToggle.setVerticalPostionOffset(0)

提交回复
热议问题