如果哦我们需要侧滑屏幕来弹出一个侧边栏,我们可以通过drawerLayout来实现。它有监听事件,里面有四个方法。我们先看如下的代码,我们在代码中打一些log来看看这四个方法的执行情况 mDrawerLayout.setDrawerListener(new DrawerListener() { @Override public void onDrawerStateChanged(int arg0) { android.util.Log.e("zhangshuli", "statechange"); } @Override public void onDrawerSlide(View arg0, float arg1) { android.util.Log.e("zhangshuli", "slide"); } @Override public void onDrawerOpened(View arg0) { android.util.Log.e("zhangshuli", "open"); } @Override public void onDrawerClosed(View arg0) { android.util.Log.e("zhangshuli", "colse"); } }); 但我们把drawerLaout从隐藏到滑出时候,log打印结果如下 03-21 11:58:57.163 E/zhangshuli(28603): statechange 03-21 11:58:57.171 E/zhangshuli(28603): slide 03-21 11:58:57.204 E/zhangshuli(28603): slide 03-21 11:58:57.238 E/zhangshuli(28603): slide 03-21 11:58:57.262 E/zhangshuli(28603): slide 03-21 11:58:57.290 E/zhangshuli(28603): slide 03-21 11:58:57.317 E/zhangshuli(28603): slide ............... 03-21 11:58:57.590 E/zhangshuli(28603): open 03-21 11:58:57.591 E/zhangshuli(28603): statechange 当我们从滑出状态到隐藏的时候,log如下 03-21 12:05:27.180 E/zhangshuli(28603): statechange 03-21 12:05:27.190 E/zhangshuli(28603): slide 03-21 12:05:27.215 E/zhangshuli(28603): slide 03-21 12:05:27.246 E/zhangshuli(28603): slide 03-21 12:05:27.270 E/zhangshuli(28603): slide 03-21 12:05:27.295 E/zhangshuli(28603): slide ................. 03-21 12:05:27.485 E/zhangshuli(28603): slide 03-21 12:05:27.506 E/zhangshuli(28603): slide 03-21 12:05:27.527 E/zhangshuli(28603): slide 03-21 12:05:27.561 E/zhangshuli(28603): slide 03-21 12:05:27.601 E/zhangshuli(28603): colse 03-21 12:05:27.601 E/zhangshuli(28603): statechange 我们可以得出结论 1.onDrawerStateChanged:总是在drawerlayout状态发生切换时候执行两次,一次是状态刚刚发生改变的时候,一次是状态改变彻底完成的时候,它总是在监听事件最开始跟末尾个执行一次 2.onDrawerSlide:它是在状态发生改变的过程中一直执行 3.onDrawerOpened(打开)、onDrawerClosed(关闭):他们是在状态改变完成以后执行,是发生在第二次onDrawerStateChanged前面