Navigation Drawer to switch between activities

前端 未结 1 1969
南旧
南旧 2020-12-11 22:09

I have browsed the website for a bit and I can\'t find an answer to my problem, I am trying to get my Navigation Drawer to switch between activities instead of fragments. I

1条回答
  •  难免孤独
    2020-12-11 22:50

    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 !!!

    0 讨论(0)
提交回复
热议问题