drawerLayout 監聽事件

若是哦咱們須要側滑屏幕來彈出一個側邊欄,咱們能夠經過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前面
相關文章
相關標籤/搜索