【问题标题】:How to setup default navigation drawer and select different fragments?如何设置默认导航抽屉并选择不同的片段?
【发布时间】:2020-09-04 20:31:27
【问题描述】:

我是新手开发人员,我正在将我的应用程序中的默认(现成)导航抽屉与 androidx 集成。我在导航片段时遇到问题,因为当我单击抽屉菜单项时它不起作用。有两个片段与其 id 链接并设置 onClick 侦听器,但它不起作用。它只显示主要片段,但不导航到其他片段。

我不熟悉 onCreateOptionsMenu(Menu menu)onSupportNavigateUp() 这样的默认函数,它们是在我创建默认抽屉活动时自行创建的,所以我只是评论这些函数并按照教程我实现了 NavigationView.OnNavigationItemSelectedListener给了我onNavigationItemSelected(@NonNull MenuItem item) 函数,我在其中编写了导航片段的代码。但它不起作用,当我调试时,它显示 onNavigationItemSelected(@NonNull MenuItem item) 没有被调用。

public class FindBarber extends AppCompatActivity implements.

NavigationView.OnNavigationItemSelectedListener {


private AppBarConfiguration mAppBarConfiguration;
ActionBarDrawerToggle toggle;
Toolbar toolbar;
DrawerLayout drawer;
NavigationView navigationView;
int OPEN, CLOSE;


 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_find_barber);
    toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    drawer = findViewById(R.id.drawer_layout);
    navigationView = findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    toggle = new ActionBarDrawerToggle(this, drawer, toolbar, OPEN, CLOSE);
    drawer.addDrawerListener(toggle);
    toggle.syncState();


    // 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_profile, R.id.nav_kutit_wallet,
            R.id.nav_terms_of_use, R.id.nav_rate_the_app, R.id.nav_contact_us)
            .setDrawerLayout(drawer)
            .build();*/
   /* NavController navController = Navigation.findNavController(this, R.id.content_frame);
    NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
    NavigationUI.setupWithNavController(navigationView, navController);*/

       FragmentManager fmanager = getSupportFragmentManager();
       fmanager.beginTransaction().replace(R.id.content_frame, new GMap()).commit();


}

/*@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.find_barber, menu);
    return true;
}
*/
 // @Override
/* public boolean onSupportNavigateUp() {
    *//*NavController navController = Navigation.findNavController(this, R.id.content_frame);*//*
    return NavigationUI.navigateUp(navController, mAppBarConfiguration)
            || super.onSupportNavigateUp();
}*/

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

    switch (item.getItemId())
    {
        case R.id.nav_home:
            getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, new GMap()).commit();
            break;

        case R.id.nav_profile:
            getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, new Profile()).commit();
            break;
    }
    drawer.closeDrawer(GravityCompat.START);
    return true;
}
}

没有错误消息,也没有任何应用程序崩溃发生。当我单击导航抽屉项目以导航任何片段时,除了我在屏幕上显示的主要默认片段之外,它不显示任何内容。

【问题讨论】:

    标签: android navigation-drawer


    【解决方案1】:

    调用 loadFragment(new Fragment(),"My Fragment") onNavigationItemSelected

    public void loadFragment(Fragment fragment, String title_name){  
     FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();  
     fragmentTransaction.replace(R.id.customer_container,fragment,title_name);
     fragmentTransaction.addToBackStack(fragment.getClass().getSimpleName());
     fragmentTransaction.commit();
     }
    

    【讨论】:

    • 什么也没发生。它没有帮助,但无论如何感谢您的支持。
    • public void loadFragment(Fragment fragment){ FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.content_frame,fragment); transaction.addToBackStack(fragment.getClass().getSimpleName());事务.commit(); }
    • @Override public boolean onNavigationItemSelected(@NonNull MenuItem item) { switch (item.getItemId()) { case R.id.nav_home: loadFragment(new GMap());休息; case R.id.nav_profile: loadFragment(new Profile());休息; }drawer.closeDrawer(GravityCompat.START);返回真; } }
    【解决方案2】:

    请尝试以下行:

    navigationView.getMenu().performIdentifierAction(R.id.posts, 0);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-08
      相关资源
      最近更新 更多