처음 새 프로젝트를 만들게 되면 아래에 같은 모양이 됩니다.
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ... 중략 ...
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
        this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
}

ActionBarDrawerToggle 에서는 이미 DrawerLayout.DrawerListener 를 가지고 있으므로 아래와 같이 추가를 해주시면 되겠습니다.

(Android Studio) 메뉴 > Code > Ovrride Methods .... 선택후

onDrawerSlide

onDrawerOpened

onDrawerClosed

onDrawerStateChanged 원하는 것을 추가하면 됩니다.


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ... 중략 ...
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
        this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);

DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
        this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close) {

    @Override
    public void onDrawerSlide(View drawerView, float slideOffset) {
        super.onDrawerSlide(drawerView, slideOffset);
    }

    @Override
    public void onDrawerOpened(View drawerView) {
        super.onDrawerOpened(drawerView);
    }

    @Override
    public void onDrawerClosed(View drawerView) {
        super.onDrawerClosed(drawerView);
    }

    @Override
    public void onDrawerStateChanged(int newState) {
        super.onDrawerStateChanged(newState);
    }
};

    //drawer.setDrawerListener(toggle); // 이건 Deprecated 되었다니깐 아래와 같이 바꿔줍니다.
    drawer.addDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
}

추가적으로
Fragment 교체 등 에서 Actionbar 의 홈 버튼을 화살표 이미지로 바꿨을 경우에 

손가락으로 밀었을때 좌측메뉴(DrawerLayout) 가 나오면 안되니깐


막기

drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);


풀기

drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);


+ Recent posts