导航抽屉:如何在启动时设置所选项目? [英] Navigation drawer: How do I set the selected item at startup?

查看:97
本文介绍了导航抽屉:如何在启动时设置所选项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码运行完美:每次在导航抽屉中单击一个项目时,都会选中该项目.

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.

这篇关于导航抽屉:如何在启动时设置所选项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆