Androidx:在一个片段中使用按钮导航到另一个片段

huangapple go评论83阅读模式
英文:

Androidx: Using a button in one fragment to navigate to another fragment

问题

我是安卓开发的新手,目前正在开发我的第一个应用程序。在开发过程中,我遇到了一个我认为很简单的问题,但是我似乎无法解决。大多数我找到的在线资源都已经过时,并且包含了不推荐使用的方法。话虽如此,让我们来看看问题所在:

在我的 MainActivity.java 中,我使用了侧边抽屉导航,可以让我在不同的片段之间切换。以下是代码示例:

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
    switch(item.getItemId()) {
        case R.id.nav_home:
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,new HomeFragment()).commit();
            break;
        case R.id.nav_account:
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new AccountFragment()).commit();
            break;
        case R.id.nav_goals:
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new GoalsFragment()).commit();
            break;
        case R.id.nav_progress:
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new ProgressFragment()).commit();
            break;
        case R.id.nav_food_diary:
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new FoodDiaryFragment()).commit();
            break;
        case R.id.nav_nutrition:
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new NutritionFragment()).commit();
            break;
        case R.id.nav_settings:
            getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new SettingsFragment()).commit();
            break;
    }

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

所有这些片段之间的导航都正常工作。但是,我的问题是,我在其中一个片段上有一个按钮,我想使用这个按钮来跳转到另一个不能通过导航抽屉访问的片段,我们称之为 fragment_profile

请问是否有可能在现有片段上通过按钮的点击事件来导航到这个新的片段呢?

非常感谢您的帮助。

英文:

I am new to android development and have been in the process of developing my very first app and have run into what I understand as a very simple complication but I can not seem to solve it, and most online resources that I have come across have been outdated and include deprecated methods. That being said, onto the issue:

In my MainActivity.java I am using a side drawer navigation that allows me to move between fragments. Here is the code:

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch(item.getItemId()) {
case R.id.nav_home:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,new HomeFragment()).commit();
break;
case R.id.nav_account:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new AccountFragment()).commit();
break;
case R.id.nav_goals:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new GoalsFragment()).commit();
break;
case R.id.nav_progress:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new ProgressFragment()).commit();
break;
case R.id.nav_food_diary:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new FoodDiaryFragment()).commit();
break;
case R.id.nav_nutrition:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new NutritionFragment()).commit();
break;
case R.id.nav_settings:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new SettingsFragment()).commit();
break;
}
drawer.closeDrawer(GravityCompat.START);
return true;
} 

The navigation between all of these fragments works perfectly. But my issue is that I have a button that exists on one of these fragments that I want to use to take me to another fragment that will not be accessible through the navigation drawer. Lets call this fragment_profile.
Will it be possible to navigate to this new fragment from an existing fragment using the onclick of a button.

Any help would be appreciated.

答案1

得分: 2

补充维卡斯所说的内容。
他说当你需要从一个 Activity 到一个 Fragment 时,你需要使用 getSupportFragmentManager(),就像下面这样:

@Override
public void onClick(View view){
   getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new ProfileFragment()).commit();
}

注意在这里我们需要传入 ProfileFragment() 类,它的布局 fragment_profile 会自行加载,因为你将会将 ProfileFragment()fragment_profile 布局设置在一起。

接下来,将来你可能想要从一个 Fragment 到另一个 Fragment,在这种情况下,你只需要在 getFragmentManager() 之前添加 getActivity(),代码会变成这样:

@Override
public void onClick(View view){
   
 getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new ProfileFragment()).commit();
}
英文:

Adding on to what Vikas said.
He told that when you need to go from an Activity to Fragment you need the getSupportFragmentManager() like below

@Override
public void onClick(View view){
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new ProfileFragment()).commit();
}

Note how we need to pass in the class for ProfileFragment() here, its layout fragment_profile will be loaded on its own as you will be setting ProfileFragment() with fragment_profile layout.

Next you may in future want to go from one Fragment to another in which case you will only need to add getActivity() before the getFragmentManager(), so it would become

@Override
public void onClick(View view){
getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new ProfileFragment()).commit();
}

答案2

得分: 1

将一个片段放入屏幕中,你只需要一个 FragmentManager。正如你在代码中所看到的,当导航选项中的一个被点击时,你会调用下面的代码:

getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new SettingsFragment()).commit();

用选定的片段替换现有的片段。当用户点击按钮时,也是完全相同的方式。

//你的点击监听器回调
@Override
public void onClick(View view){
   getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new ProfileFragment()).commit();
}

唯一需要注意的是,宿主应该有名为 fragment_container 的片段容器,就是这样。

英文:

to put a fragment in a screen all you need is a fragmentmananger. as you can see in your code how you have changed when one of the option in the navigation gets clicked then you are calling

getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new SettingsFragment()).commit();

to replace the existing fragment with the selected one. exactly same when user clicks on the button like below.

//your click listener callback
@Override
public void onClick(View view){
getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new ProfileFragment()).commit();
}

the only thing you need to take care is that the host should have the fragment container fragment_container thats it.

huangapple
  • 本文由 发表于 2020年8月21日 15:50:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/63518727.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定