所以我在我的应用中实现了NavigationDrawer的问题。我所做的是将NavigationDrawer活动扩展到我的其他活动,它工作正常。
我打算做的是点击抽屉里的链接打开一个新的活动,但是我不希望发生两件事:
navigationDrawerActivity.java
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.widget.FrameLayout;
public class navigationDrawerActivity extends AppCompatActivity implements
NavigationView.OnNavigationItemSelectedListener {
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mToggle;
protected DrawerLayout fullLayout;
protected FrameLayout actContent;
private Toolbar mToolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void setContentView(final int layoutResID) {
fullLayout = (DrawerLayout) getLayoutInflater().inflate(R.layout.activity_navigation_drawer, null);
actContent = (FrameLayout) fullLayout.findViewById(R.id.act_content);
// This code combines this activity along with others for the NavigationDrawer to be global
getLayoutInflater().inflate(layoutResID, actContent, true);
super.setContentView(fullLayout);
//NavigationDrawer settings
mToolbar = (Toolbar) findViewById(R.id.nav_action);
setSupportActionBar(mToolbar);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
mToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
mDrawerLayout.addDrawerListener(mToggle);
mToggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
setNavigationViewListener();
}
//Enables the NavDrawer icon to work
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
//Handles navigation view item clicks
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.nav_newsFeed: {
Intent intent = new Intent (this, newsfeedActivity.class);
startActivity(intent);
mDrawerLayout.closeDrawer(GravityCompat.START);
break;
}
case R.id.nav_hiScores: {
Intent intent = new Intent (this, hiscoresActivity.class);
startActivity(intent);
mDrawerLayout.closeDrawer(GravityCompat.START);
break;
}
}
//close navigation drawer
mDrawerLayout.closeDrawer(GravityCompat.START);
return true;
}
private void setNavigationViewListener() {
NavigationView navigationView = (NavigationView) findViewById(R.id.navigationView);
navigationView.setNavigationItemSelectedListener(this);
}
}
编辑:
所以我在调整了一下代码之后设法解决了问题,基本上我在每个活动中添加了一个onResume。在onResume中,它设置在导航视图中选择的项目,以及使用overridePendingTransition来停止我遇到的过渡问题。
答案 0 :(得分:0)
在活动中,显示高分在onCreate()中执行:
navigationView.setCheckedItem(R.id.nav_hiScores);
P.S。
导航抽屉应该用于在单个活动中更改片段。不要用它来切换多个活动。
答案 1 :(得分:0)
可以使用NavigationView或NavigationDrawer更改Activites,但要在当前活动中设置导航的选定位置,您可以使用您开始活动的目的发送int数据,并使用该索引设置选中的项目。