单击选项时导航抽屉关闭

时间:2017-10-23 11:05:05

标签: java android android-layout android-studio

目前,汉堡菜单按钮工作正常。但是,当我单击导航菜单中的任何选项时,它立即关闭,并且该操作未在我的Android应用程序中实现。而另一件事情很好。请帮我解决这个问题。非常感谢你。

这是我的activity.java

public class HomeActivity extends AppCompatActivity implements View.OnClickListener{
Button pro;
FirebaseAuth firebaseAuth;
ProgressDialog pd;
FirebaseAuth.AuthStateListener firebaseAuthState;
DrawerLayout drawerLayout;
ActionBarDrawerToggle mToggle;
NavigationView navigationView;

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

    drawerLayout=(DrawerLayout) findViewById(R.id.drawer);
    mToggle=new ActionBarDrawerToggle(this,drawerLayout,R.string.open,R.string.close);
    drawerLayout.addDrawerListener(mToggle);
    mToggle.syncState();
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    navigationView=(NavigationView)findViewById(R.id.navigation_view);

    pro=(Button)findViewById(R.id.button1);

    firebaseAuthState= new FirebaseAuth.AuthStateListener() {
        @Override
        public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
            if (firebaseAuth.getCurrentUser()==null){
                Intent i=new Intent(HomeActivity.this,LoginActivity.class);
                startActivity(i);
                finish();
            }
        }
    };

    pd=new ProgressDialog(this);

    firebaseAuth=FirebaseAuth.getInstance();

    pro.setOnClickListener(this);

    navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()){
                case R.id.profile:
                    startActivity(new Intent(HomeActivity.this,ProfileActivity.class));
                    drawerLayout.closeDrawers();
                    break;
            }
            return false;
        }
    });
}



@Override
public void onClick(View v) {
    if(v==pro){
        Intent i=new Intent(HomeActivity.this,ProfileActivity.class);
        startActivity(i);
    }
        }

@Override
protected void onStart() {
    super.onStart();
    firebaseAuth.addAuthStateListener(firebaseAuthState);
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {

    if (mToggle.onOptionsItemSelected(item)){
        return true;
    }

    return super.onOptionsItemSelected(item);
}
}

请帮帮我......

2 个答案:

答案 0 :(得分:0)

删除这行代码:

drawerLayout.closeDrawers();

答案 1 :(得分:0)

只需从

中删除drawerLayout.closeDrawers();即可
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()){
                case R.id.profile:
                    startActivity(new Intent(HomeActivity.this,ProfileActivity.class));
                    drawerLayout.closeDrawers();// Remove this line
                    break;
            }
            return false;
        }
    });