简体   繁体   English

从另一个片段导航片段,例如导航链接

[英]Navigate fragments from another fragment like like Navigation links

In the application, I have:在应用程序中,我有:

  1. One main activity一项主要活动
  2. 4 fragments 4个片段

2.a. 2.a. Home fragment 2.b.主页片段 2.b。 Data fragment 2.c.数据片段 2.c。 Picture fragment 2.d.图片片段2.d。 Link fragment链接片段

So inside of the home fragment, there are 3 pictures which links to Data, picture and link fragment.所以在主页片段内部,有3张图片链接到数据,图片和链接片段。 There is a NavigationView which also links to the fragment.有一个 NavigationView 也链接到该片段。

Now, I want to add these links to each picture so it can work as a Navigation links and forwards to the same fragment.现在,我想将这些链接添加到每张图片中,以便它可以用作导航链接并转发到同一个片段。 How to achieve that?如何做到这一点?

Currently, I am using setOnClickListener inside home fragment to forward each fragment but I believe there are better ways to do that?目前,我在主片段中使用 setOnClickListener 来转发每个片段,但我相信有更好的方法可以做到这一点? This is because when i use setOnClickListener this does not update the current nav link.这是因为当我使用 setOnClickListener 时,这不会更新当前的导航链接。

Layout布局

That is a very common task and Google introduced the Navigation component for such tasks.这是一项非常常见的任务,Google 为此类任务引入了导航组件。 See https://developer.android.com/guide/navigation/navigation-getting-started for reference.请参阅https://developer.android.com/guide/navigation/navigation-getting-started以供参考。

Some reference code (you also need the navigation graph):一些参考代码(您还需要导航图):

In MainActivity在 MainActivity

public class MainActivity extends AppCompatActivity {
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      NavController navController = Navigation.findNavController(this, 
      R.id.nav_host_fragment);
    }
}

In MainActivity XML:在 MainActivity XML 中:

<fragment
    android:id="@+id/nav_host_fragment"
    android:name="androidx.navigation.fragment.NavHostFragment"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    app:defaultNavHost="true"
    app:layout_constraintBottom_toTopOf="@+id/bottomNavigationBorder"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:navGraph="@navigation/nav_graph" />

In "onViewCreated" of the navigation Fragment在导航片段的“onViewCreated”中

NavController navController = Navigation.findNavController(view);
btnAction.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
      Bundle bundle = new Bundle();
      bundle.putInt("ACTION", "1");
      navController.navigate(R.id.action_mainFragment_to_nextFragment, bundle);
   }
});

The function navController.navigate() can one or two arguments. function navController.navigate() 可以是一个或两个 arguments。 The bundle is optional and is used if you want to send information to the target fragment.捆绑包是可选的,如果您想将信息发送到目标片段,则使用该捆绑包。 The "R.id....." attribute is defined in the navigation graph and describes the link between two fragments / activities. “R.id.....”属性在导航图中定义,描述了两个片段/活动之间的链接。

Hope this helps.希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM