Reputation: 16730
I have implemented a navigation drawer and everything functions properly. The navigation drawer contains a listview and when an item is selected, the main fragment is replaced properly.
This is the onClickListener code:
mDrawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
HeaderViewListAdapter headerView = (HeaderViewListAdapter) parent.getAdapter();
AccountAdapter adapter = (AccountAdapter) headerView.getWrappedAdapter();
Cursor cursor = adapter.getCursor();
if(cursor != null && cursor.moveToPosition(position)){
((NavigationDrawerCallbacks) getActivity()).onNavigationDrawerItemSelected(cursor.getLong(cursor.getColumnIndex(ID_COLUMN)));
}
}
});
And in my activity, the onNavigationDrawerItemSelected method is like this:
@Override
public void onNavigationDrawerItemSelected(long accountId) {
// update the main content by replacing fragments
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.mainFragment, AccountDetailFragment.newInstance(accountId))
.commit();
}
As I said, this functions, but the navigation drawer remains open. It's a hassle for the user to have to select an account from the listview and then have to close the drawer to see the main fragment, so how can I have it close automatically, so that when an item is selected the only visible element is the main fragment (and everything inside of it)?
Upvotes: 1
Views: 7102
Reputation: 699
You need to call the closeDrawer inside the onItemClick, when the Drawer Layout is not null.
if (mDrawerLayout != null) {
mDrawerLayout.closeDrawer(mFragmentContainerView);
}
Upvotes: 2
Reputation: 39201
Call closeDrawer()
on the DrawerLayout
object with either the Drawer View
, or the Drawer's Gravity
as an argument.
Upvotes: 4