提问者:小点点

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


这是我通过mobile_navigation创建碎片的主要活动。xml现在在Action Bar中,我设置了两个项目,一个是搜索,这是我通过意向发送的另一个活动。现在,对于购物车项目,我想将其发送到同一活动的购物车片段。如何发送??我是开发新手,请帮助

protected void onCreate(Bundle savedInstanceState){ super . onCreate(savedInstanceState);

    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);
}

}


共1个答案

匿名用户

在导航图中创建一个操作,并在if()条件下添加以下代码:

Navigation.findNavController(view).navigate(R.id.action_fragment1_to_fragment2);