LuckyMe LuckyMe - 1 year ago 74
Android Question

Why does DrawerLayout sometimes glitch upon opening?

I have followed the tutorial Navigation Drawer and everything works like a charm except for a small glitch. I'll try to explain it as much as possible, and if it still isn't clear, I'll try to upload a video of the problem.

Problem happens when trying to open the drawer and only when opening, and only happens sometimes, not always. That is when I start to open it it glitches and freezes with about 4 millimeters open, and always the same distance. It then would not continue opening nor close back if I move my finger back, when I let go, it closes.

Please note:

  • I have tried it on multiple devices (Nexus 7, Nexus) and virtual devices as well, problem persists.

  • Issue replicated using the example provided by Google. (Youtube Link)

  • Issue presented with my app. (Youtube Link)

  • I managed to replicated the issue once but only once in Gmail app (which I believe uses the same implementation), however much more frequent with my app and the sample app.

  • I noticed that the glitch happens at the same distance of which the drawer opens if you just click the edge of the screen, as in it freezes after initial open and never drags.

Any pointers would be appreciated.

Answer Source

I researched the code of DrawerLayout and found the next issue: When we touch the edge of screen there appears small (20*density px) part of the drawer (it makes moving drawer easier). It doesn't appears immediatly, but after a certain interval (160 ms) of time. It is realized by postDelayed.

The drawer can be in several states: IDLE, DRAGGING and SETTLING. If it was in the DRAGGING state, it cannot return to this state anymore with the same pointer and edge (because there is a condition: mEdgeDragsInProgress[pointerId] & edge) == edge which doesn't allow to drag the edge which have been dragging already).

So in some cases the drawer have moved to the state DRAGGING already when delayed Runnable is executing. This delayed action opens drawer for 20*density px and change state of drawer. So drawer cannot be moved anymore (because it cannot return to the state DRAGGING).

There is a code for cancelling delayed action (which opens drawer), but this code in the method onInterceptTouchEvent, which is called only once (because it returns false). I think this code should be in the method onTouchEvent.

Unfortunatly I didn't find any way to cancel delayed event (because it has private modifier and I can't get it). So only one way I found: copy the source code of DrawerLayout to my project and make this small change: copy

case MotionEvent.ACTION_MOVE: {
            // If we cross the touch slop, don't perform the delayed peek for an edge touch.
            if (mLeftDragger.checkTouchSlop(ViewDragHelper.DIRECTION_ALL)) {

from the method onInterceptTouchEvent to the method onTouchEvent.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download