简体   繁体   English

开始时已默认选择Android导航抽屉

[英]Android navigation drawer default selected on start

I have created a new android studio project using the navigation drawer activity. 我使用导航抽屉活动创建了一个新的android studio项目。 Currently, the main activity starts when the app is loaded (which is fine), but I want to associate this main activity with a particular menu item in the drawer such that 1) when this menu item is selected the main activity will load, and 2) I want that menu item to be selected/highlighted by default when the app first starts 目前,主要活动是在加载应用程序时启动的(很好),但是我想将此主要活动与抽屉中的特定菜单项相关联,这样1)选择此菜单项时,主要活动将被加载,并且2)我希望在应用程序首次启动时默认选择/突出显示该菜单项

I think I would handle issue 1 with intents in the onNavigationItemSelected method, but I'm not sure how to have a menu item selected by default. 我想我会用onNavigationItemSelected方法中的意图处理问题1,但是我不确定默认情况下如何选择菜单项。

I've searched around and the only solutions I've found refer to fragments, but I'm not using fragments in this project (and those solutions didn't work). 我四处搜寻,发现的唯一解决方案是片段,但是我在这个项目中没有使用片段(这些解决方案不起作用)。 For example I have tried selectItem(0); 例如我尝试过selectItem(0); in onCreate() , and navigationView.getMenu().getItem(0).setChecked(true); onCreate()navigationView.getMenu().getItem(0).setChecked(true); but neither seem to work. 但似乎都不起作用。

Here is my base code: 这是我的基本代码:

MainActivity.java : MainActivity.java

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "FAB Pressed", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

    }

activity_main_drawer.xml : activity_main_drawer.xml

<item android:title="Menu">
        <menu>
            <group android:checkableBehavior="single">
                <item
                    android:id="@+id/nav_new"
                    android:icon="@drawable/ic_menu_share"
                    android:title="@string/nav_new" />
                <item
                    android:id="@+id/nav_start"
                    android:icon="@drawable/ic_menu_slideshow"
                    android:title="@string/nav_start" />
                <item
                    android:id="@+id/nav_delete"
                    android:icon="@drawable/ic_menu_manage"
                    android:title="@string/nav_delete" />
            </group>
        </menu>
    </item>

Not much has changed from the starting project apart from the menu items. 除了菜单项外,从开始项目进行的更改不多。 I want to have the New menu item selected by default as I want to associate that item with the MainActivity 我想默认选择“ New菜单项,因为我要将其与MainActivity关联

您只需要添加代码:

navigationView.setCheckedItem(R.id.nav_new);

I thing the best approach to this is to include android:checked="true" 我觉得最好的方法是包含android:checked="true"

<item android:title="Menu">
        <menu>
            <group android:checkableBehavior="single">
                <item
                    android:id="@+id/nav_new"
                    android:icon="@drawable/ic_menu_share"
                    android:checked="true"
                    android:title="@string/nav_new" />
                <item
                    android:id="@+id/nav_start"
                    android:icon="@drawable/ic_menu_slideshow"
                    android:title="@string/nav_start" />
                <item
                    android:id="@+id/nav_delete"
                    android:icon="@drawable/ic_menu_manage"
                    android:title="@string/nav_delete" />
            </group>
        </menu>
    </item>

Hope this helps you more 希望这对您有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM