导航抽屉:如何在启动时设置所选项目?
我的代码运行完美:每次在导航抽屉中单击一个项目时,都会选中该项目.
My code works perfectly: every time an item in Navigation Drawer is clicked the item is selected.
我当然想使用默认片段(主页)启动应用程序,但是导航抽屉中没有选择该项.如何以编程方式选择该项目?
Of course I want to start the app with a default fragment (home), but Navigation Drawer doesn't have the item selected. How can I select that item programmatically?
public class BaseApp extends AppCompatActivity {
//Defining Variables
protected String LOGTAG = "LOGDEBUG";
protected Toolbar toolbar;
protected NavigationView navigationView;
protected DrawerLayout drawerLayout;
private DateManager db = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.base_layout);
navigationView = (NavigationView) findViewById(R.id.navigation_view);
// set the home/dashboard at startup
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.frame, new DashboardFragment());
fragmentTransaction.commit();
setNavDrawer();
}
private void setNavDrawer(){
// Initializing Toolbar and setting it as the actionbar
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//Initializing NavigationView
//Setting Navigation View Item Selected Listener to handle the item click of the navigation menu
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
// This method will trigger on item Click of navigation menu
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
//Checking if the item is in checked state or not, if not make it in checked state
// I THINK THAT I NEED EDIT HERE...
if (menuItem.isChecked()) menuItem.setChecked(false);
else menuItem.setChecked(true);
//Closing drawer on item click
drawerLayout.closeDrawers();
//Check to see which item was being clicked and perform appropriate action
switch (menuItem.getItemId()) {
//Replacing the main content with ContentFragment
case R.id.home:
DashboardFragment dashboardFragment = new DashboardFragment();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.frame, dashboardFragment,"DASHBOARD_FRAGMENT");
fragmentTransaction.commit();
return true;
[...]
我认为我需要在此处进行
I think that I need to edit here:
if (menuItem.isChecked()) menuItem.setChecked(false);
else menuItem.setChecked(true);
或者在onCreate
中使用FragmentTransaction在应用程序启动时显示.
Or in onCreate
at App startup with FragmentTransaction.
感谢您的支持.
使用以下代码:
navigationView.getMenu().getItem(0).setChecked(true);
调用setNavDrawer();
getItem(int index)
方法获取MenuItem
,然后您可以在该MenuItem
上调用setChecked(true);
,剩下要做的就是找出默认值具有哪个元素索引,并将0替换为该索引.
The getItem(int index)
method gets the MenuItem
then you can call the setChecked(true);
on that MenuItem
, all you are left to do is to find out which element index does the default have, and replace the 0 with that index.
您可以通过调用选择(突出显示)该项目
You can select(highlight) the item by calling
onNavigationItemSelected(navigationView.getMenu().getItem(0));
以下是参考链接: http: //thegeekyland.blogspot.com/2015/11/navigation-drawer-how-set-selected-item.html
编辑 在nexus 4上不起作用,支持库版本24.0.0.我建议使用
EDIT Did not work on nexus 4, support library revision 24.0.0. I recommend use
navigationView.setCheckedItem(R.id.nav_item);
由下面的@kingston回答.
answered by @kingston below.