计时器在片段之间共享

时间:2015-09-23 10:54:33

标签: android android-fragments wear-os android-fragmentactivity

我有一个FragmentActivity,附有3个片段。这3个片段有一个天文台,我希望它们在它们之间共享相同的时间......

  • 我试图同时开始()他们所有人,但有时他们突然从00.00开始......
  • 我试图通过onChronometerTick()将时间存储在FragmentActivity上,但它似乎不起作用......

有人能告诉我如何做我想做的事吗?

片段1

public class Run1Fragment extends Fragment {
private Chronometer chronometer;
private WearRunActivity runActivity;
private Run2Fragment run2;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.layout_run1, container, false);
    chronometer = (Chronometer) view.findViewById(R.id.chronometer);
    runActivity = new WearRunActivity();
    chronometer.setOnChronometerTickListener(runActivity);
    chronometer.start();

    return view;
}

片段2

public class Run2Fragment extends Fragment {
private Chronometer r2Chronometer;
Run1Fragment run1;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.layout_run2, container, false);
    r2Chronometer = (Chronometer) view.findViewById(R.id.chronometer);

    /*r2Chronometer.setBase(run1.getChronometer().getBase());
    r2Chronometer.start();*/

    return view;
}

FragronActivity onChronometerTick监听器

@Override
public void onChronometerTick(Chronometer chronometer) {
    System.out.println("CRONOMETROOOO");
    this.chronometer.setBase(chronometer.getBase());
}

1 个答案:

答案 0 :(得分:0)

好的,我找到了解决方案!

我没有为所有片段创建计时器,而是将其集成到片段活动片段中,因此只有我想要更改的文本才会被放置在其他片段中。