android-fragments 当我在任何其他片段中时,如何在同一Activity中单击操作栏项(在选择的选项项上)时设置购物车片段

mpgws1up  于 2022-11-14  发布在  Android
关注(0)|答案(1)|浏览(144)

这是主活动,我通过mobile_navigation. xml创建了片段。现在,在操作栏中,我设置了2个项目,一个是搜索,另一个是我通过Intent发送的活动。现在,对于购物车项目,我想将其发送到同一活动的购物车片段。我如何发送它??我是开发部门的新手,请帮助
创建时的示例状态为:

binding = ActivityMainBinding.inflate(getLayoutInflater());
    setContentView(binding.getRoot());

    setSupportActionBar(binding.appBarMain.toolbar);
    DrawerLayout drawer = binding.drawerLayout;
    NavigationView navigationView = binding.navView;
    // 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_my_archaka,R.id.nav_my_cart)
            .setOpenableLayout(drawer)
            .build();
    NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main);
    NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
    NavigationUI.setupWithNavController(navigationView, navController);

    parentframelayout = findViewById(R.id.parentframelayout);




}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onSupportNavigateUp() {
    NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main);
    return NavigationUI.navigateUp(navController, mAppBarConfiguration)
            || super.onSupportNavigateUp();
}

@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
    int id = item.getItemId();

    if (id == R.id.main_search) {

        Intent searchintent = new Intent(this, SearchActivity.class);
        startActivity(searchintent);
        return true;
    }

    if (id == R.id.main_cart) {

////如何将此相同活动片段发送到购物车;

return true;

    } 

    return super.onOptionsItemSelected(item);
}

}

pdsfdshx

pdsfdshx1#

在navgraph中创建一个操作,并在if()条件下添加以下代码:
Navigation.findNavController(view).navigate(R.id.action_fragment1_to_fragment2);

相关问题