这是我实现抽屉和工具栏的主要活动。我在堆栈上尝试了所有可能的解决方案。我还没有用过汉堡图片 我创建了导航视图并将其添加到我的xml中:
public class NavigationMenu extends AppCompatActivity {
DrawerLayout drawerLayout;
Toolbar toolbar;
ActionBarDrawerToggle actionBarDrawerToggle;
FragmentTransaction fragmentTransaction;
NavigationView navigationView;
RoundImage roundedImage;
ImageView profileImage;
TextView uname,mail;
private SQLiteHandler db;
private SessionManager session;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navigation_menu);
toolbar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
/* Setting drawer in menu*/
drawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
actionBarDrawerToggle = new ActionBarDrawerToggle(this,drawerLayout,R.string.drawer_open,R.string.drawer_close);
drawerLayout.setDrawerListener(actionBarDrawerToggle);
actionBarDrawerToggle.setDrawerIndicatorEnabled(true);
actionBarDrawerToggle.syncState();
fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.main_container,new HomeFragment());
fragmentTransaction.commit();
/* Accessing database getApplicationContext( */
db = new SQLiteHandler(this);
HashMap<String,String> user = db.getUserDetails();
String id = user.get("id");
String name = user.get("name");
String email = user.get("email");
Log.d("Soulsystem", name);
Log.d("Soulsystem", email);
/*Access navigation header here to set name and to call the attributes of header*/
NavigationView navigationView = (NavigationView)findViewById(R.id.navigation_view);
View header = navigationView.getHeaderView(0);
uname = (TextView)header.findViewById(R.id.rahul);
uname.setText(name);
AppController.getInstance().setClientConsultancyDetails(id,name,email);
profileImage = (ImageView)header.findViewById(R.id.navigation_view_Image);
profileImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
profile();
}
});
// session manager
session = new SessionManager(getApplicationContext());
if (!session.isLoggedIn()) {
logoutUser();
}
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.contact:
contact();
break;
case R.id.logout:
logoutUser();
/* item.setChecked(true);
drawerLayout.closeDrawers();*/
break;
}
return true;
}
});
}
protected void onPostCreate(@Nullable Bundle savedInstanceState) {
actionBarDrawerToggle.syncState();
super.onPostCreate(savedInstanceState);
}
private void logoutUser() {
session.setLogin(false);
db.deleteUsers();
Intent intent = new Intent(NavigationMenu.this, MainActivity.class);
startActivity(intent);
}
//call to Profile image fragment
private void profile(){
Log.d("NavigationMenu", "In profile Image view");
fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.main_container, new ClientProfile());
fragmentTransaction.commit();
drawerLayout.closeDrawers();
}
//call to Contact image fragment
private void contact(){
Log.d("NavigationMenu", "In profile Image view");
fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.main_container, new Contact());
fragmentTransaction.commit();
drawerLayout.closeDrawers();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.drawe_menu, menu);
return false;
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem item= menu.findItem(R.menu.drawe_menu);
item.setVisible(false);
super.onPrepareOptionsMenu(menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (actionBarDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
答案 0 :(得分:1)
尝试听工具栏
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
drawerLayout.openDrawer(GravityCompat.START);
}
}
});