在 ViewPager 中滑动选项卡时如何在片段之间发送数据

How to send data between fragments when swiping tabs in ViewPager

我有 activity CoordinatorLayout、AppBarLayout、Toolbar、TabLayout、ViewPAger 和 2 个片段。

片段描述:

  1. 有设置(ListView 填充了数据)
  2. 根据所选数据运行的游戏

想法是在第一个和第二个选项卡(片段)之间来回切换。 从第 1 次到第 2 次每次滑动时,第 2 个选项卡会根据第 1 个选项卡中选择的数据进行一些更新。

如何在滑动事件中将那些选择的数据从第一个选项卡发送到第二个选项卡。

使用EventBus.

EventBus simplifies the communication between components. It performs well with Activities, Fragments, and background threads.

Here 是 EventBus 的一个很好的例子。

EventBus 将帮助您在两个组件之间发送数据。