导航抽屉:如何设定在启动时选定的项目?抽屉、启动时、项目

2023-09-14 23:03:00 作者:对着太阳喊声日

我的code完美的作品。我通过导航抽屉选择每一个项目,该列表项检查

My code works perfectly: every item I select via Navigation Drawer, the list item is checked.

当然,我想先从默认的片段(家)的应用程序,但抽屉式导航栏也没有选择的项目。如何选择编程该项目?

Of course I want start app with a default fragment (home), but Navigation Drawer has not the item selected. How can I select programmatically that item?

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.

感谢您的支持

推荐答案

使用code如下:

navigationView.getMenu().getItem(0).setChecked(true);

调用此方法之后您调用 setNavDrawer();

的getItem(INT指数)方法获取菜单项,那么你可以调用 setChecked(真); 菜单项,你剩下要做的就是找​​出哪些元素索引确实默认有,并更换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.