在每个活动内容的不同活动上使用导航栏活动

时间:2016-02-16 06:38:00

标签: android navigation drawerlayout setcontentview

我正在尝试将其他活动扩展到导航栏活动,我想使用活动与导航抽屉的内容,但是当我使用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语句,因为我知道在导航抽屉活动中调用了setcontentview,但我想要导航抽屉和内容。如何完成这项任务?

1 个答案:

答案 0 :(得分:0)

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

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

if (id == R.id.nav_dashboard) {
    // write code for fragment replacement
}