I have an application with an activity that contains a left-side navigation drawer. This activity also loads dynamically different fragments.
Now the point is: I want to attach to each fragment a different right-side drawer (to implement some filters), attached on fragment load.
I've tried to copy and re-use navigationDrawer.setUp(int fragmentId, DrawerLayout drawerLayout)
code in MyFragment.onAttach(...)
method, but I get a NullPointerException
with mFragmentContainerView = getActivity().findViewById(R.id.filter_drawer);
(I think getActivity().findViewById(...) can't find anything).
Here is the code copied from auto-generated NavigationDrawerFragment that I put in my CustomerFragment:
mFragmentContainerView = getActivity().findViewById(R.id.filter_drawer);
mDrawerLayout = (DrawerLayout) activity.findViewById(R.id.drawer_layout);
// set a custom shadow that overlays the main content when the drawer opens
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
mDrawerToggle = new ActionBarDrawerToggle(
getActivity(), /* host Activity */
mDrawerLayout, /* DrawerLayout object */
R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */
R.string.navigation_drawer_open, /* "open drawer" description for accessibility */
R.string.navigation_drawer_close /* "close drawer" description for accessibility */
) {
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
if (!isAdded()) {
return;
}
getActivity().invalidateOptionsMenu(); // calls onPrepareOptionsMenu()
}
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
if (!isAdded()) {
return;
}
if (!mUserLearnedDrawer) {
// The user manually opened the drawer; store this flag to prevent auto-showing
// the navigation drawer automatically in the future.
mUserLearnedDrawer = true;
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getActivity());
sp.edit().putBoolean(PREF_USER_LEARNED_DRAWER, true).apply();
}
getActivity().invalidateOptionsMenu(); // calls onPrepareOptionsMenu()
}
};
// If the user hasn't 'learned' about the drawer, open it to introduce them to the drawer,
// per the navigation drawer design guidelines.
if (!mUserLearnedDrawer && !mFromSavedInstanceState) {
mDrawerLayout.openDrawer(mFragmentContainerView);
}
// Defer code dependent on restoration of previous instance state.
mDrawerLayout.post(new Runnable() {
@Override
public void run() {
mDrawerToggle.syncState();
}
});
mDrawerLayout.setDrawerListener(mDrawerToggle);
I've searched the web for a solution, but everyone does the opposite of what i want: "manage fragment via navigation drawer" and I want "custom navigation drawer on fragment".
How can I achieve this?