drawerLayout-监听事件四个方法介绍
1.首先我们看一下它的监听事件的生命周期
代码如下
mDrawerLayout.setDrawerListener(new DrawerListener() { @Override public void onDrawerStateChanged(int arg0) { mHistoryHideButton.startAnimation(myAnimation); android.util.Log.e("zhangshuli", "statechange"); } @Override public void onDrawerSlide(View arg0, float arg1) { mHistoryHideButton.startAnimation(myAnimationRecover); android.util.Log.e("zhangshuli", "slide"); } @Override public void onDrawerOpened(View arg0) { myHistoryAnimationSetCount = 0; myHistoryItemCount= mHistory.mEntries.size()-1; if (mHistoryDisplayList.getCount() == 0) { mHistoryClearButton.setEnabled(false); } else { mHistoryClearButton.setEnabled(true); } android.util.Log.e("zhangshuli", "open"); } @Override public void onDrawerClosed(View arg0) { history_clear_choose.setVisibility(View.GONE); android.util.Log.e("zhangshuli", "colse"); } });
当我们直接点开,也就是执行
if (!mDrawerLayout.isDrawerVisible(GravityCompat.END)) { mDrawerLayout.openDrawer(GravityCompat.END); }
的时候,打印结果如下
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.341 E/zhangshuli(28603): slide 03-21 11:58:57.366 E/zhangshuli(28603): slide 03-21 11:58:57.393 E/zhangshuli(28603): slide 03-21 11:58:57.420 E/zhangshuli(28603): slide 03-21 11:58:57.445 E/zhangshuli(28603): slide 03-21 11:58:57.470 E/zhangshuli(28603): slide 03-21 11:58:57.496 E/zhangshuli(28603): slide 03-21 11:58:57.519 E/zhangshuli(28603): slide 03-21 11:58:57.545 E/zhangshuli(28603): slide 03-21 11:58:57.590 E/zhangshuli(28603): open 03-21 11:58:57.591 E/zhangshuli(28603): statechange
我们可以看到
onDrawerStateChanged 在状态开始改变跟状态改变完成以后各执行一次
onDrawerOpened 最终打开以后执行
onDrawerSlide 一直在移动的过程中执行
关闭的时候,点击关闭
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.319 E/zhangshuli(28603): slide 03-21 12:05:27.349 E/zhangshuli(28603): slide 03-21 12:05:27.378 E/zhangshuli(28603): slide 03-21 12:05:27.412 E/zhangshuli(28603): slide 03-21 12:05:27.440 E/zhangshuli(28603): slide 03-21 12:05:27.463 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
跟上面唯一的区别也就是
彻底关闭的时候,执行
onDrawerClosed
当我们是滑动打开的时候
03-21 12:09:20.367 E/zhangshuli(28603): statechange 03-21 12:09:20.375 E/zhangshuli(28603): slide 03-21 12:09:20.395 E/zhangshuli(28603): statechange 03-21 12:09:20.439 E/zhangshuli(28603): slide 03-21 12:09:20.470 E/zhangshuli(28603): slide 03-21 12:09:20.500 E/zhangshuli(28603): slide 03-21 12:09:20.524 E/zhangshuli(28603): slide 03-21 12:09:20.548 E/zhangshuli(28603): slide 03-21 12:09:20.572 E/zhangshuli(28603): slide 03-21 12:09:20.599 E/zhangshuli(28603): slide 03-21 12:09:20.621 E/zhangshuli(28603): slide 03-21 12:09:20.643 E/zhangshuli(28603): slide 03-21 12:09:20.666 E/zhangshuli(28603): slide 03-21 12:09:20.689 E/zhangshuli(28603): slide 03-21 12:09:20.713 E/zhangshuli(28603): slide 03-21 12:09:20.736 E/zhangshuli(28603): slide 03-21 12:09:20.760 E/zhangshuli(28603): slide 03-21 12:09:20.785 E/zhangshuli(28603): slide 03-21 12:09:20.810 E/zhangshuli(28603): slide 03-21 12:09:20.834 E/zhangshuli(28603): slide 03-21 12:09:20.859 E/zhangshuli(28603): slide 03-21 12:09:20.882 E/zhangshuli(28603): slide 03-21 12:09:20.906 E/zhangshuli(28603): slide 03-21 12:09:20.931 E/zhangshuli(28603): slide 03-21 12:09:20.962 E/zhangshuli(28603): slide 03-21 12:09:20.994 E/zhangshuli(28603): slide 03-21 12:09:21.019 E/zhangshuli(28603): slide 03-21 12:09:21.045 E/zhangshuli(28603): slide 03-21 12:09:21.070 E/zhangshuli(28603): slide 03-21 12:09:21.094 E/zhangshuli(28603): slide 03-21 12:09:21.118 E/zhangshuli(28603): slide 03-21 12:09:21.143 E/zhangshuli(28603): slide 03-21 12:09:21.168 E/zhangshuli(28603): slide 03-21 12:09:21.193 E/zhangshuli(28603): slide 03-21 12:09:21.218 E/zhangshuli(28603): slide 03-21 12:09:21.241 E/zhangshuli(28603): slide 03-21 12:09:21.266 E/zhangshuli(28603): slide 03-21 12:09:21.291 E/zhangshuli(28603): slide 03-21 12:09:21.318 E/zhangshuli(28603): slide 03-21 12:09:21.343 E/zhangshuli(28603): slide 03-21 12:09:21.367 E/zhangshuli(28603): slide 03-21 12:09:21.392 E/zhangshuli(28603): slide 03-21 12:09:21.417 E/zhangshuli(28603): slide 03-21 12:09:21.442 E/zhangshuli(28603): slide 03-21 12:09:21.469 E/zhangshuli(28603): slide 03-21 12:09:21.494 E/zhangshuli(28603): slide 03-21 12:09:21.519 E/zhangshuli(28603): slide 03-21 12:09:21.544 E/zhangshuli(28603): slide 03-21 12:09:21.569 E/zhangshuli(28603): slide 03-21 12:09:21.597 E/zhangshuli(28603): slide 03-21 12:09:21.623 E/zhangshuli(28603): slide 03-21 12:09:21.649 E/zhangshuli(28603): slide 03-21 12:09:21.674 E/zhangshuli(28603): slide 03-21 12:09:21.698 E/zhangshuli(28603): slide 03-21 12:09:21.724 E/zhangshuli(28603): slide 03-21 12:09:21.750 E/zhangshuli(28603): slide 03-21 12:09:21.776 E/zhangshuli(28603): slide 03-21 12:09:21.801 E/zhangshuli(28603): slide 03-21 12:09:21.832 E/zhangshuli(28603): slide 03-21 12:09:21.858 E/zhangshuli(28603): slide 03-21 12:09:21.882 E/zhangshuli(28603): slide 03-21 12:09:21.906 E/zhangshuli(28603): slide 03-21 12:09:21.930 E/zhangshuli(28603): slide 03-21 12:09:21.955 E/zhangshuli(28603): slide 03-21 12:09:21.980 E/zhangshuli(28603): slide 03-21 12:09:22.007 E/zhangshuli(28603): slide 03-21 12:09:22.035 E/zhangshuli(28603): slide 03-21 12:09:22.062 E/zhangshuli(28603): slide 03-21 12:09:22.262 E/zhangshuli(28603): open 03-21 12:09:22.262 E/zhangshuli(28603): statechange
其实跟我们直接点开没太大区别,就是
onDrawerSlide 一直在执行。所以,这个方法应该可以得到你的移动坐标
滑动关闭的时候
03-21 12:11:38.056 E/zhangshuli(28603): statechange 03-21 12:11:38.073 E/zhangshuli(28603): slide 03-21 12:11:38.100 E/zhangshuli(28603): slide 03-21 12:11:38.130 E/zhangshuli(28603): slide 03-21 12:11:38.161 E/zhangshuli(28603): slide 03-21 12:11:38.193 E/zhangshuli(28603): slide 03-21 12:11:38.222 E/zhangshuli(28603): slide 03-21 12:11:38.250 E/zhangshuli(28603): slide 03-21 12:11:38.275 E/zhangshuli(28603): slide 03-21 12:11:38.298 E/zhangshuli(28603): slide 03-21 12:11:38.322 E/zhangshuli(28603): slide 03-21 12:11:38.347 E/zhangshuli(28603): slide 03-21 12:11:38.371 E/zhangshuli(28603): slide 03-21 12:11:38.402 E/zhangshuli(28603): slide 03-21 12:11:38.429 E/zhangshuli(28603): slide 03-21 12:11:38.457 E/zhangshuli(28603): slide 03-21 12:11:38.483 E/zhangshuli(28603): slide 03-21 12:11:38.508 E/zhangshuli(28603): slide 03-21 12:11:38.534 E/zhangshuli(28603): slide 03-21 12:11:38.559 E/zhangshuli(28603): slide 03-21 12:11:38.582 E/zhangshuli(28603): slide 03-21 12:11:38.606 E/zhangshuli(28603): slide 03-21 12:11:38.631 E/zhangshuli(28603): slide 03-21 12:11:38.657 E/zhangshuli(28603): slide 03-21 12:11:38.688 E/zhangshuli(28603): slide 03-21 12:11:38.714 E/zhangshuli(28603): slide 03-21 12:11:38.741 E/zhangshuli(28603): slide 03-21 12:11:38.767 E/zhangshuli(28603): slide 03-21 12:11:38.796 E/zhangshuli(28603): slide 03-21 12:11:38.823 E/zhangshuli(28603): slide 03-21 12:11:38.849 E/zhangshuli(28603): slide 03-21 12:11:38.876 E/zhangshuli(28603): slide 03-21 12:11:38.903 E/zhangshuli(28603): slide 03-21 12:11:38.930 E/zhangshuli(28603): slide 03-21 12:11:38.959 E/zhangshuli(28603): slide 03-21 12:11:38.986 E/zhangshuli(28603): slide 03-21 12:11:39.009 E/zhangshuli(28603): slide 03-21 12:11:39.032 E/zhangshuli(28603): slide 03-21 12:11:39.066 E/zhangshuli(28603): slide 03-21 12:11:39.135 E/zhangshuli(28603): slide 03-21 12:11:39.160 E/zhangshuli(28603): slide 03-21 12:11:39.184 E/zhangshuli(28603): slide 03-21 12:11:39.212 E/zhangshuli(28603): slide 03-21 12:11:39.239 E/zhangshuli(28603): slide 03-21 12:11:39.349 E/zhangshuli(28603): statechange 03-21 12:11:39.360 E/zhangshuli(28603): slide 03-21 12:11:39.383 E/zhangshuli(28603): slide 03-21 12:11:39.404 E/zhangshuli(28603): slide 03-21 12:11:39.425 E/zhangshuli(28603): slide 03-21 12:11:39.446 E/zhangshuli(28603): slide 03-21 12:11:39.467 E/zhangshuli(28603): slide 03-21 12:11:39.488 E/zhangshuli(28603): slide 03-21 12:11:39.508 E/zhangshuli(28603): slide 03-21 12:11:39.529 E/zhangshuli(28603): slide 03-21 12:11:39.549 E/zhangshuli(28603): slide 03-21 12:11:39.585 E/zhangshuli(28603): slide 03-21 12:11:39.619 E/zhangshuli(28603): colse 03-21 12:11:39.619 E/zhangshuli(28603): statechange
也是一样的。所以,在它的方法中,我们可以做很多事情
我们可以得出结论
1.onDrawerStateChanged:总是在drawerlayout状态发生切换时候执行两次,一次是状态刚刚发生改变的时候,一次是状态改变彻底完成的时候,它总是在监听事件最开始跟末尾个执行一次
2.onDrawerSlide:它是在状态发生改变的过程中一直执行
3.onDrawerOpened(打开)、onDrawerClosed(关闭):他们是在状态改变完成以后执行,是发生在第二次onDrawerStateChanged前面