3

我正在尝试将其他活动扩展到导航栏活动,我想将活动的内容与导航抽屉一起使用,但是当我使用 setcontentView 时,导航栏不起作用。

这是我的导航栏活动: :

public class navigationdrawer extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_navigationdrawer);



    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer,  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);
}

@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.navigationdrawer, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    if (id == R.id.nav_dashboard) {
        Intent i = new Intent(navigationdrawer.this, dashboard.class);
        startActivity(i);
        // Handle the camera action
    } else if (id == R.id.nav_community) {

    } else if (id == R.id.nav_recipes) {
        Intent i = new Intent(navigationdrawer.this, Recipe.class);
        startActivity(i);

    } else if (id == R.id.nav_leaderboard) {

    } else if (id == R.id.nav_upload) {

    } else if (id == R.id.nav_mypics) {

    } else if (id == R.id.nav_myvideos) {

    } else if (id == R.id.nav_settings) {

    } else if (id == R.id.nav_contactus) {

    }
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}
}

这是我的第二个活动:

public class Recipe extends navigationdrawer{

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
//        setContentView(new View(this));

//        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
//        setSupportActionBar(toolbar);


}



}

我已经评论了 setcontentview 语句,因为我知道在 Navigation Drawer Activity 中调用了 setcontentview,但我想要 Navigation Drawer 和内容。如何完成这个任务?

4

1 回答 1

0

使用片段代替活动片段是活动的一部分。因此,如果您想在整个应用程序中使用 singledrawer,那么最好在导航栏活动中使用片段

注意:在导航抽屉活动中使用片段视图,例如

if (id == R.id.nav_dashboard) {
    // write code for fragment replacement
}
于 2016-02-16T07:00:42.160 回答