Open Navigation Drawer with a Handle

This type of functionality is not supported on DrawerLayout from support libraries. There are some third party libraries with all kind of additional functionality. They are huge and usually we don’t need all components included.

The implementation is very easy. After looking at source code of DrawerLayout class, we can see that there is onTouchEvent method predefined. So we have to delegate touch events from our “handle” widget to DrawerLayout.

 drag.setOnTouchListener(new View.OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {

       if(event.getAction() == MotionEvent.ACTION_DOWN){
          MotionEvent copy = MotionEvent.obtainNoHistory(event);
          copy.setEdgeFlags(ViewDragHelper.EDGE_ALL);
          copy.setLocation(0, event.getY());

          drawerLayout.onTouchEvent(copy);
       }
       return false;
   }
});

Object drag could be any type of View as long as we can listen to touch events. If touch action equals ACTION_DOWN than we delegate event handling to DrawerLayout object with motion event position X set to 0. All next updates will bi automatically propagated to DrawerLayout instance.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s