OnItemClickListener Navigation Drawer

前端 未结 6 1748
粉色の甜心
粉色の甜心 2020-12-14 12:57

I am making an app from the example of Android Developers with the Navigation Drawer. I made the items but I don\'t know how I can open new Activity from each of items enlis

6条回答
  •  渐次进展
    2020-12-14 13:40

    Instead of your code:

    private void selectItem(int position) {
    
        Fragment fragment = new GalaxyFragment();
        Bundle args = new Bundle();
        args.putInt(GalaxyFragment.ARG_Galaxy_NUMBER, position);
        fragment.setArguments(args);
    
        FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
    
    
        mDrawerList.setItemChecked(position, true);
        setTitle(mGalaxyTitles[position]);
        mDrawerLayout.closeDrawer(mDrawerList);
    }
    

    Use this code:

    private void selectItem(int position) {
        switch(position) {
        case 1:
                Intent a = new Intent(MainActivity.this, Activity1.class);
                startActivity(a);
        break;
        case 2:
                Intent b = new Intent(MainActivity.this, Activity2.class);
               startActivity(b);
               break;
        default:
        }
    }
    

    And remove this part: (It is not necessary)

    public void onItemClick(AdapterView parent, View view, int position, long id) {
    
    switch(position) {
    case 1:
            Intent a = new Intent(MainActivity.this, Page1.class);
            startActivity(a);
    break;
    case 2:
            Intent b = new Intent(MainActivity.this, Page2.class);
           startActivity(b);
           break;
    default:
    }
    }
    

提交回复
热议问题