这里我做了一个DrawerBase活动让我的所有Activity都有相同的DrawerLayout,但是当我这样做时,发生了一些无聊的事情,当我开始一个新的Intent开始新的Activity时没有关闭抽屉。
这是我的代码,希望有人可以帮助我。
package cn.rezero.utool;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import cn.rezero.utool.Note.NotelistActivity;
/**
* Created by ReZero on 2017/4/5.
*/
public class DrawerBase extends AppCompatActivity {
protected DrawerLayout mDrawerLayout;
protected NavigationView navigationView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setCheckedItem(R.id.nav_moment);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.nav_lbs:
startActivity(new Intent(DrawerBase.this, BmapActivity.class));
mDrawerLayout.closeDrawers();
break;
case R.id.nav_note:
startActivity(new Intent(DrawerBase.this, NotelistActivity.class));
mDrawerLayout.closeDrawers();
break;
case R.id.nav_web:
startActivity(new Intent(DrawerBase.this, BrowserActivity.class));
mDrawerLayout.closeDrawers();
default:
}
return true;
}
});
}
}
this a test demo :
public class MainActivity extends DrawerBase {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
答案 0 :(得分:0)
使用此:
mDrawerLayout.closeDrawer(GravityCompat.START);
答案 1 :(得分:0)
从抽屉布局中删除此行...
startActivity(new Intent(this, ActivityToOpen.class));
并覆盖setContentView,如下所示
setContentView(R.layout.activity_main);
替换
@Override
public void setContentView(int layoutResID) {
DrawerLayout fullView = (DrawerLayout) getLayoutInflater().inflate(R.layout.activity_base, null);
FrameLayout activityContainer = (FrameLayout) fullView.findViewById(R.id.activity_content);
getLayoutInflater().inflate(layoutResID, activityContainer, true);
super.setContentView(fullView);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
setTitle("Activity Title");
}
到
startActivity(new Intent(DrawerBase.this, BmapActivity.class));
mDrawerLayout.closeDrawers();
您可以在此处Adding a Toolbar and Navigation Drawer Across all Activities of an Android App
查看完整实施答案 2 :(得分:0)
使用此方法
/**
* Close Navigation Drawer
*/
private void closeNavMenu() {
if (mDrawerLayout != null && mDrawerLayout.isDrawerOpen(GravityCompat.START)) {
mDrawerLayout.closeDrawer(GravityCompat.START);
}
}
答案 3 :(得分:0)
你只是试试这个
mDrawerLayout.closeDrawer(GravityCompat.START);
答案 4 :(得分:0)
您需要使用
if (mDrawerLayout.isDrawerOpen(GravityCompat.START)) {
mDrawerLayout.closeDrawer(GravityCompat.START);
}
而不是
mDrawerLayout.closeDrawers();