Navigation Drawer to switch between activities

心不动则不痛 提交于 2019-11-28 14:18:53

Suppose you have 5 items (from 0 index to 4), each index identifying an Activity of your project. You can create a method selectItem(int position)to know what drawer item has been chosen by user.

public void selectItem(int position) {
    Intent intent = null;
    switch(position) {
        case 0:
            intent = new Intent(this, Activity_0.class);
            break;
        case 1:
            intent = new Intent(this, Activity_1.class);
            break;

        ...


        case 4: 
            intent = new Intent(this, Activity_4.class);
            break;

        default : 
            intent = new Intent(this, Activity_0.class); // Activity_0 as default
            break;
    }

    startActivity(intent);
}

Finally, add this method to your DrawerItemClickListener :

private class DrawerItemClickListener implements ListView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView parent, View view, int position, long id) {
        selectItem(position);
        drawerLayout.closeDrawer(drawerListView);

    }
}

It's easier than using Fragments, I think !!!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!