如何将默认启动片段设置为主页?

时间:2018-02-19 11:24:08

标签: android android-layout navigation-drawer android-listfragment

我的导航抽屉出现问题,我想将FragHome设置为我的默认启动主页。但我不能在这里实现它是我的代码:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_nav);    
        activity = this;      
        auth = FirebaseAuth.getInstance();
        dl = findViewById(R.id.nav_drawer);
        dl.setVerticalScrollBarEnabled(false);
        toggle = new ActionBarDrawerToggle(this, dl, R.string.nav_drawer_open, R.string.nav_drawer_close);    
        NavigationView nv = findViewById(R.id.nav);
        nv.setCheckedItem(R.id.nav_home);
        nv.setVerticalScrollBarEnabled(false);
        dl.addDrawerListener(toggle);
        toggle.syncState();

        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }
        setContent(nv);
    }    
    public static void finishThis() {
        try {
            if (activity != null) {
                activity.finish();
            }

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void showHome() {
        fragment = new FragHome();    
        if (fragment != null) {
            FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
            ft.replace(R.id.nav_home, fragment,fragment.getTag());
            ft.commit();       
        }
    }

    public void onDisplay(View v) {
        startActivity(new Intent(NavActivity.this, FragInfo.class));
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        if (toggle.onOptionsItemSelected(item)) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }    
    Fragment fragment = null;
    public void selectItem(MenuItem i) {


        switch (i.getItemId()) {
            case R.id.nav_home:
                fragment = new FragHome();
                break;
            case R.id.nav_eng:
                fragment = new FragEng();
                break;
            case R.id.nav_math:
                fragment = new FragMath();
                break;
            case R.id.nav_sci:
                fragment = new FragSci();
                break;
            case R.id.nav_his:
                fragment = new FragHistory();
                break;
            case R.id.exam:
                fragment = new examMain();
                break;
            case R.id.nav_about:
                fragment = new FragAbout();
                break;
            case R.id.nav_logout:

                AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.navDialog);
                builder.setMessage("Are you sure you want to log out?")
                        .setCancelable(false)
                        .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                if (auth.getCurrentUser() != null)
                                    auth.signOut();
                                Intent intent = new Intent(NavActivity.this, LoginActivity.class);
                                startActivity(intent);
                                finish();
                            }
                        })
                        .setNegativeButton("No", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.cancel();
                            }
                        });
                AlertDialog alert = builder.create();
                alert.show();
            default:
                fragment = new FragHome();
        }
        if (fragment != null) {
            FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
            ft.replace(R.id.nav_home, fragment);
            ft.commit();
        }
        DrawerLayout drawer = findViewById(R.id.nav_drawer);
        drawer.closeDrawer(GravityCompat.START);
    }
    private void setContent(NavigationView v) {
        v.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                selectItem(item);
                return true;
            }
        });
    }
    @Override
    public void onBackPressed() {
        //You may also add condition if (doubleBackToExitPressedOnce || fragmentManager.getBackStackEntryCount() != 0) // in case of Fragment-based add
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.nav_drawer);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            if(fragment instanceof FragHome){
                super.onBackPressed();
            }else {
                showHome();
            }
        }
    }
}

2 个答案:

答案 0 :(得分:1)

你永远不会在onCreate()中调用showHome()。参见下面的例子。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_nav);    
    activity = this;      
    auth = FirebaseAuth.getInstance();
    dl = findViewById(R.id.nav_drawer);
    dl.setVerticalScrollBarEnabled(false);
    toggle = new ActionBarDrawerToggle(this, dl, R.string.nav_drawer_open, R.string.nav_drawer_close);    
    NavigationView nv = findViewById(R.id.nav);
    nv.setCheckedItem(R.id.nav_home);
    nv.setVerticalScrollBarEnabled(false);
    dl.addDrawerListener(toggle);
    toggle.syncState();

    if (getSupportActionBar() != null) {
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    setContent(nv);
    showHome();
}   

答案 1 :(得分:0)

你可以参加活动。

    @Override
    public void onCreate(Bundle savedInstanceState){

    super.onCreate(savedInstanceState);
        FragmentTransaction tx = getSupportFragmentManager().beginTransaction();
        tx.replace(R.id.content_frame, new FragmentA());
        tx.commit();


    }


private class DrawerItemClickListener implements ListView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        selectItem(position);
    }
}

/** Swaps fragments in the main content view */
private void selectItem(int position) {
    // Create a new fragment and specify the planet to show based on position
    Fragment fragment = new PlanetFragment();
    Bundle args = new Bundle();
    args.putInt(PlanetFragment.ARG_PLANET_NUMBER, position);
    fragment.setArguments(args);

    // Insert the fragment by replacing any existing fragment
    FragmentManager fragmentManager = getFragmentManager();
    fragmentManager.beginTransaction()
                   .replace(R.id.content_frame, fragment)
                   .commit();

    // Highlight the selected item, update the title, and close the drawer
    mDrawerList.setItemChecked(position, true);
    setTitle(mPlanetTitles[position]);
    mDrawerLayout.closeDrawer(mDrawerList);
}

@Override
public void setTitle(CharSequence title) {
    mTitle = title;
    getActionBar().setTitle(mTitle);
}

你必须看到绘制片段和导航的基本教程。 https://developer.android.com/training/implementing-navigation/nav-drawer.html