Android - How to change fragments in the Navigation Drawer

前端 未结 4 1585
甜味超标
甜味超标 2020-11-30 05:22

I\'m fairly new to Android programming, but have been doing pretty well until now. I\'ve read a lot of answers to this question but can\'t seem to make mine work. Basically

4条回答
  •  借酒劲吻你
    2020-11-30 05:40

    Follow This link!! for more details and use

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

    and

    private void selectItem(int position) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction ft = fragmentManager.beginTransaction();
    
    
        switch (position) {
    
        case 0:
    
            ft.replace(R.id.content_frame, new Fragment1, Constants.TAG_FRAGMENT).commit();
    
            break;
    
    
        case 1:
            ft.replace(R.id.content_frame, new Fragment2, Constants.TAG_FRAGMENT);
    
            ft.commit();
            break;
    
        }
    
        mDrawerList.setItemChecked(position, true);
        setTitle(title[position]);
    
        // Close drawer
        mDrawerLayout.closeDrawer(mDrawerList);
    }
    

提交回复
热议问题