Android导航抽屉单击事件问题

我正在创建一个包含导航抽屉活动的应用程序。我正在主屏幕中加载不同的片段。现在,当我当时必须调用Fragment时,它可以正常工作,但是当我想调用某种服务(如call,图像选择器)时,我添加了setNavigationItemSelectedListener,它工作正常,但其他所有片段现在都无法加载。

  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中的单击事件,并根据NavigationUI文档导航到相关屏幕。这必然在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;

}

以上是 Android导航抽屉单击事件问题 的全部内容, 来源链接: utcz.com/qa/414695.html

回到顶部