shubham sharma shubham sharma - 5 months ago 22
Android Question

How to open next drawer on click of a specific fragment of previous drawer item

In my app i want to open new drawer on click of an item of navigation bar in previous drawer of navigation drawer in android . like now i had mainactivity.java and fragments are involved on that activity . Now what i want that when i click on any item of mainactivity.java drawer item then it must open another drawer with different values in item i am attaching my code of mainActivity.java

public class MainActivity extends AppCompatActivity{
DrawerLayout mDrawerLayout;
NavigationView mNavigationView;
FragmentManager mFragmentManager;
FragmentTransaction mFragmentTransaction;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_coordinator);

/**
*Setup the DrawerLayout and NavigationView
*/

mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
mNavigationView = (NavigationView) findViewById(R.id.shitstuff) ;

/**
* Lets inflate the very first fragment
* Here , we are inflating the TabFragment as the first Fragment
*/

mFragmentManager = getSupportFragmentManager();
mFragmentTransaction = mFragmentManager.beginTransaction();
mFragmentTransaction.replace(R.id.containerView,new TabFragment()).commit();
/**
* Setup click events on the Navigation View Items.
*/

mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
mDrawerLayout.closeDrawers();



if (menuItem.getItemId() == R.id.nav_item_home) {
FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.containerView,new coordinatorPanelFragment()).commit();

}


if (menuItem.getItemId() == R.id.nav_item_fllclass) {
FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
xfragmentTransaction.replace(R.id.containerView,new fillClassFragment()).commit();
}
///

if (menuItem.getItemId() == R.id.nav_item_fillstudent) {
FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.containerView,new fillStudentFragment()).commit();

}


if (menuItem.getItemId() == R.id.nav_item_fillschedule) {
FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
xfragmentTransaction.replace(R.id.containerView,new fillscheduleFragment()).commit();
}


if (menuItem.getItemId() == R.id.nav_item_seebunk) {
FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
xfragmentTransaction.replace(R.id.containerView,new seeBunkedStudentFragment()).commit();
}

if (menuItem.getItemId() == R.id.nav_item_cus) {
FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
xfragmentTransaction.replace(R.id.containerView,new ContactusFragment()).commit();
}

if (menuItem.getItemId() == R.id.nav_item_help) {
FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
xfragmentTransaction.replace(R.id.containerView,new helpCoorFragment()).commit();
}





return false;
}

});

/**
* Setup Drawer Toggle of the Toolbar
*/

android.support.v7.widget.Toolbar toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.toolbar);
ActionBarDrawerToggle mDrawerToggle = new ActionBarDrawerToggle(this,mDrawerLayout, toolbar,R.string.app_name,
R.string.app_name);

mDrawerLayout.setDrawerListener(mDrawerToggle);

mDrawerToggle.syncState();

}


These was my code for making one drawer with 6 items now i want that when i click on any item then it must open an specific drawer of that perticular fragment , i had searched but not found answer on google .It is same as app of google drive .

Answer

Just see these example i hope they will help for your problem . http://www.androidhive.info/2013/11/android-sliding-menu-using-navigation-drawer/

It is tutorial of Android Sliding Menu using Navigation Drawer . The required code is i am pasting here :-

private class SlideMenuClickListener implements
            ListView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            // display view for selected nav drawer item
            displayView(position);
        }
    }

     /**
     * Diplaying fragment view for selected nav drawer list item
     * */
    private void displayView(int position) {
        // update the main content by replacing fragments
        Fragment fragment = null;
        switch (position) {
        case 0:
            fragment = new HomeFragment();
            break;
        case 1:
            fragment = new FindPeopleFragment();
            break;
        case 2:
            fragment = new PhotosFragment();
            break;
        case 3:
            fragment = new CommunityFragment();
            break;
        case 4:
            fragment = new PagesFragment();
            break;
        case 5:
            fragment = new WhatsHotFragment();
            break;

        default:
            break;
        }

        if (fragment != null) {
            FragmentManager fragmentManager = getFragmentManager();
            fragmentManager.beginTransaction()
                    .replace(R.id.frame_container, fragment).commit();

            // update selected item and title, then close the drawer
            mDrawerList.setItemChecked(position, true);
            mDrawerList.setSelection(position);
            setTitle(navMenuTitles[position]);
            mDrawerLayout.closeDrawer(mDrawerList);
        } else {
            // error in creating fragment
            Log.e("MainActivity", "Error in creating fragment");
        }
    }