Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
153 views
in Technique[技术] by (71.8m points)

android - How to implement a DrawerLayout with a visible handle

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 to add a button on the left. That button might be clicked or swiped to open the left drawer. That I can do.

But the button is supposed to look like it's a part of the drawer that would overflow into the screen.

That means the button should slide simultaneously as the drawer opens and closes.

CLOSED STATE :

enter image description here

OPENING STATE

enter image description here

I tried adding the button into the left drawer's layout, but it seems you can't make stuff appear outside of its boundaries, and the drawer will always get completely hidden when you close it.

Now I'm trying adding it to add a button to the main DrawerLayout and make it align to the right of the left drawer... But no luck... It looks like a DrawerLayout can't have more than two children...

Any help will be appreciated.

I'm using the Support library (v4)

[EDIT] And I am supporting API level 8... So can't use ImageView.setTranslationX or View.OnAttachStateChangeListener

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...