在我的应用程序中,我的应用程序中有一个NavigationDrawer,它有两个我想在MainActivity中显示的片段(Fragment1和Fragment2)。我点击片段1中的工具栏中有一个菜单项我希望我的MainActivity切换到Fragment2并让NavigationView反映UI的变化。
public class MainActivity extends AppCompatActivity {
private DrawerLayout mDrawer;
private Toolbar toolbar;
private NavigationView nvDrawer;
private ActionBarDrawerToggle drawerToggle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().replace(R.id.flContent, new BookListFragment()).commit();
}
// Set a Toolbar to replace the ActionBar.
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Find our drawer view
mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerToggle = setupDrawerToggle();
// Tie DrawerLayout events to the ActionBarToggle
mDrawer.addDrawerListener(drawerToggle);
// Find our drawer view
nvDrawer = (NavigationView) findViewById(R.id.nvView);
// Setup drawer view
setupDrawerContent(nvDrawer);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
drawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggles
drawerToggle.onConfigurationChanged(newConfig);
}
private ActionBarDrawerToggle setupDrawerToggle() {
// NOTE: Make sure you pass in a valid toolbar reference. ActionBarDrawToggle() does not require it
// and will not render the hamburger icon without it.
return new ActionBarDrawerToggle(this, mDrawer, toolbar, R.string.drawer_open, R.string.drawer_close);
}
private void setupDrawerContent(NavigationView navigationView) {
navigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
selectDrawerItem(menuItem);
return true;
}
});
}
public void selectDrawerItem(MenuItem menuItem) {
// Create a new fragment and specify the fragment to show based on nav item clicked
Fragment fragment = null;
Class fragmentClass;
switch (menuItem.getItemId()) {
case R.id.book_list_fragment:
fragmentClass = BookListFragment.class;
break;
case R.id.discover_fragment:
fragmentClass = DiscoverFragment.class;
break;
default:
fragmentClass = BookListFragment.class;
}
try {
fragment = (Fragment) fragmentClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
// Insert the fragment by replacing any existing fragment
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.flContent, fragment).commit();
// Highlight the selected item has been done by NavigationView
menuItem.setChecked(true);
// Set action bar title
setTitle(menuItem.getTitle());
// Close the navigation drawer
mDrawer.closeDrawers();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// The action bar home/up action should open or close the drawer.
switch (item.getItemId()) {
case android.R.id.home:
mDrawer.openDrawer(GravityCompat.START);
return true;
}
if (drawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
片段代码
public class BookListFragment extends Fragment {
...
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_insert_dummy_data:
insertBook();
return true;
case R.id.action_delete_all_entries:
int rowsDeleted = getContext().getContentResolver().delete(ShelfHelpContract.BookEntry.CONTENT_URI, null, null);
return true;
case R.id.action_discover:
/*TODO Here I want to open another Fragment and have it reflect
in the UI on my NavigationView and MainActivity
*/
return true;
}
return super.onOptionsItemSelected(item);
....
}
答案 0 :(得分:0)
以下代码负责在用户点击其中一个导航项时替换Fragment
// Insert the fragment by replacing any existing fragment
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.flContent, fragment).commit();
除了替换片段之外,您还需要使用以下内容来update the title进行活动:
((AppCompatActivity)getActivity()).getSupportActionBar().setTitle("Home");
最后,您必须告诉NavigationView
使用setChecked(boolean)
功能突出显示正确的项目。看起来应该是这样的:
nvDrawer.getMenu().findItem(/*your item id*/).setChecked(true);
如果您想在用户点击菜单项时执行相同的操作,请确保将新案例添加到onOptionsItemSelected(MenuItem item)
中找到的切换块,并且只需调用右侧传递的相同行{ {1}}