当我通过单击工具栏中的汉堡包图标打开导航抽屉时,它会打开它,但是当我单击左箭头关闭导航抽屉时,没有发生任何事情,它没有关闭它。请帮助
这是我的代码
public class MainActivity extends AppCompatActivity {
Toolbar toolbar;
NavigationDrawerFragment navigationDrawerFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_drawer);
toolbar=(Toolbar)findViewById(R.id.app_bar);
//findViewById(R.id.drawerlayout);
setSupportActionBar(toolbar);
navigationDrawerFragment= (NavigationDrawerFragment)
getSupportFragmentManager().findFragmentById(R.id.nav_frag);
navigationDrawerFragment.setup(R.id.nav_frag,(DrawerLayout) findViewById(R.id.drawerlayout),(Toolbar)findViewById(R.id.app_bar));
getSupportActionBar().setDisplayShowHomeEnabled(true);
final ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
/* if(actionBar!=null)
{
actionBar.setHomeAsUpIndicator(android.R.drawable.ic_dialog_dialer);
actionBar.setDisplayHomeAsUpEnabled(true);
}*/
}
@Override
public void onBackPressed() {
super.onBackPressed();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.cool_menu,menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id= item.getItemId();
if(id==android.R.id.home)
{ ((DrawerLayout)findViewById(R.id.drawerlayout)).openDrawer(GravityCompat.START); // OPEN DRAWER
return true;
}
if(id==R.id.action_settings){
Toast.makeText(this, "this is setting", Toast.LENGTH_SHORT).show();
}
if(id==R.id.action_mail){
startActivity(new Intent(this,SubActivity.class));
}
if(id==R.id.action_help){
Toast.makeText(this, "this is action_help", Toast.LENGTH_SHORT).show();
}
return true;
}
}
activity_main_drawer.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
android:id="@+id/app_bar"
layout="@layout/app_bar"></include>
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawerlayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/nav_frag"
android:name="com.example.adarsh.material.NavigationDrawerFragment"
android:layout_width="280dp"
android:layout_height="match_parent"
android:layout_gravity="start"
tools:layout="@layout/fragment_navigation_drawer" />
</android.support.v4.widget.DrawerLayout>
NavigationDrawerFragment.java
public class NavigationDrawerFragment extends Fragment {
private static final String FILE_NAME = "shared";
private static final String SHARED_KEY ="shared_key" ;
private DrawerLayout mDrawerLayout;
private View containerView;
private ActionBarDrawerToggle mDrawerToggle;
private boolean mUserLearnerDrawer;
private boolean mFromSaveInstances;
public NavigationDrawerFragment() {
// Required empty public constructor
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState); mUserLearnerDrawer=Boolean.valueOf(readFromSharedPreferences(getActivity(),SHARED_KEY,"false"));
if(savedInstanceState!=null){
mFromSaveInstances=true;
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_navigation_drawer, container, false);
}
public void setup(int id,DrawerLayout drawerLayout, Toolbar toolbar) {
containerView=getActivity().findViewById(id);
mDrawerLayout=drawerLayout;
mDrawerToggle=new ActionBarDrawerToggle(getActivity(),drawerLayout,R.string.open,R.string.close){
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
//we are going to store the mUserLearnerDrawer in shared pref
//if it is saved then we will not show the drawer then we will show the drawer
//if the user has never seeen the drawer before
if(!mUserLearnerDrawer){
mUserLearnerDrawer=true;
saveToSharedPreferences(getActivity(),SHARED_KEY,mUserLearnerDrawer+"");
}
getActivity().invalidateOptionsMenu();// it going to make draw the action bar again
}
@Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
getActivity().invalidateOptionsMenu();
}
};
//never seen the drawer + never used it before
if(!mUserLearnerDrawer&&!mFromSaveInstances){
mDrawerLayout.openDrawer(containerView);
}
mDrawerLayout.addDrawerListener(mDrawerToggle);
mDrawerLayout.post(new Runnable() {
@Override
public void run() {
mDrawerToggle.syncState();
}
});
}
public void saveToSharedPreferences(Context context,String name,String value){
SharedPreferences sPrefs=context.getSharedPreferences(FILE_NAME,Context.MODE_PRIVATE);
SharedPreferences.Editor editor=sPrefs.edit();
editor.putString(name,value);
editor.apply();//apply is faster then the commit method .coz unlike commit it does not return that yes it is commited or not
}
public String readFromSharedPreferences(Context context,String name,String defaultValue){
SharedPreferences sPrefs=context.getSharedPreferences(FILE_NAME,Context.MODE_PRIVATE);
return sPrefs.getString(name,defaultValue);
}
}
答案 0 :(得分:0)
创建一个全局变量
private boolean isDrawerOpen = false;
然后在你的onOptionItemSelected做这样的事情
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id= item.getItemId();
if(id==android.R.id.home)
{
DrawerLayout drawerLayout = (DrawerLayout)findViewById(R.id.drawerlayout);
if(isDrawerOpen){
isDrawerOpen = false;
drawaerLayout.closeDrawer(GravityCompat.START);
} else{
isDrawerOpen = true;
drawaerLayout.openDrawer(GravityCompat.START);
}
return true;
}
if(id==R.id.action_settings){
Toast.makeText(this, "this is setting", Toast.LENGTH_SHORT).show();
}
if(id==R.id.action_mail){
startActivity(new Intent(this,SubActivity.class));
}
if(id==R.id.action_help){
Toast.makeText(this, "this is action_help", Toast.LENGTH_SHORT).show();
}
return true;
}