gpt4 book ai didi

java - Android抽屉导航点击事件问题

转载 作者:行者123 更新时间:2023-12-01 18:52:27 26 4
gpt4 key购买 nike

我正在创建一个包含抽屉导航 Activity 的应用程序。我正在主屏幕中加载不同的 fragment 。现在,当我必须调用 Fragment 时,它工作得很好,但是当我想调用某种服务(例如调用、图像选择器)时,我添加了 setNavigationItemSelectedListener ,它工作得很好,但所有其他 fragment 现在都没有加载。

  protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

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, R.id.nav_franchise,
R.id.nav_tieUP, R.id.nav_giftVoucher, R.id.nav_faq, R.id.nav_privacy_policy,
R.id.nav_tools, R.id.nav_send)
.setDrawerLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);

navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {

int id = menuItem.getItemId();
if (id == R.id.callUs) {
Intent intent = new Intent(Intent.ACTION_CALL);

intent.setData(Uri.parse("tel:" + "XXXxxxXXX"));

if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CALL_PHONE},REQUEST_PHONE_CALL);
}
else
{
startActivity(intent);
}
}

drawer.closeDrawer(GravityCompat.START);
return true;
}
});

}

最佳答案

当您调用 NavigationUI.setupWithNavController(navigationView, navController) 时,您是在说您希望 NavController 处理来自 NavigationView 的点击事件,导航到相关屏幕根据the NavigationUI documentation 。这必然会在内部调用 setNavigationItemSelectedListener()

通过随后调用 setNavigationItemSelectedListener,您可以删除原始监听器,这就是您的其他项目不再执行任何操作的原因。您可以通过调用 NavigationUI.onNavDestinationSelected() 来触发默认行为

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {

int id = menuItem.getItemId();
if (id == R.id.callUs) {
Intent intent = new Intent(Intent.ACTION_CALL);

intent.setData(Uri.parse("tel:" + "XXXxxxXXX"));

if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CALL_PHONE},REQUEST_PHONE_CALL);
}
else
{
startActivity(intent);
}
}
else
{
// Make your navController object final above
// or call Navigation.findNavController() again here
NavigationUI.onNavDestinationSelected(menuItem, navController);
}
drawer.closeDrawer(GravityCompat.START);
return true;

}

关于java - Android抽屉导航点击事件问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59710350/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com