我是一个比较新的安卓用户,我想在我的应用程序中添加一个导航抽屉,所以我添加了一个新的活动,并选择导航抽屉的活动。
活动课
import android.os.Bundle;
import android.view.View;
import android.view.Menu;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import com.google.android.material.navigation.NavigationView;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
public class HomeActivity extends AppCompatActivity {
private AppBarConfiguration mAppBarConfiguration;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow)
.setDrawerLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.home, menu);
return true;
}
@Override
public boolean onSupportNavigateUp() {
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
return NavigationUI.navigateUp(navController, mAppBarConfiguration)
|| super.onSupportNavigateUp();
}
}
当我运行应用程序并尝试点击另一个项目时,什么都没有发生,它仍然是在主片段上,到目前为止,我所看到的大部分信息是旧的3.6.3版本之前,其他只是工作 到目前为止,我已经尝试了什么- 卸载并重新安装android studio-更新到最新的android studio(目前是4)-创建其他碎片-盯着新的导航抽屉项目运行它...同样的事情。
一直在这个过去的几天没有什么我尝试似乎工作
标题
你可以通过使用下面的行来轻松地关闭抽屉,在点击抽屉的每个项目时处理它。
drawer_layout.closeDrawer(GravityCompat.END)
你可以实现 onClick
在导航抽屉的项目上。
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener
并添加此方法来处理点击监听器。
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
// Handle navigation view item clicks here.
switch (item.getItemId()) {
case R.id.nav_home: {
//do somthing
break;
}
case R.id.nav_gallery: {
//do somthing
break;
}
case R.id.nav_slideshow: {
//do somthing
break;
}
}
//close navigation drawer
drawer.closeDrawer(GravityCompat.END);
return true;
}
和其他方法来处理点击。
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
int id=menuItem.getItemId();
//it's possible to do more actions on several items, if there is a large amount of items I prefer switch(){case} instead of if()
if (id==R.id.nav_home){
Toast.makeText(getApplicationContext(), "Home", Toast.LENGTH_SHORT).show();
}
if (id==R.id.nav_gallery){
Toast.makeText(getApplicationContext(), "nav_gallery", Toast.LENGTH_SHORT).show();
}
if (id==R.id.nav_slideshow){
Toast.makeText(getApplicationContext(), "slideshow", Toast.LENGTH_SHORT).show();
}
//This is for maintaining the behavior of the Navigation view
NavigationUI.onNavDestinationSelected(menuItem,navController);
//This is for closing the drawer after acting on it
drawer.closeDrawer(GravityCompat.START);
return true;
}
});