尽管使用替换,片段重叠,删除方法来更改片段

时间:2016-01-08 09:02:38

标签: android android-fragments countdowntimer

我在活动中有两个倒计时器。一个计算在一个有锻炼,锻炼和运动后的片段上花费的时间(忽略反馈片段)。另一个计算所有练习的总时间。一旦锻炼完成,它将被下一个片段取代。这是代码:( IGNORE changeexercise方法 - >基于某些条件,它在preexercisefragment和exercisefragment中添加了必要的片段)

public void replaceFragment(int fragment_num, ArrayList<String> used_exercise_data, long millisUnfinished) {
    Fragment frag = getFragmentManager().findFragmentByTag(String.valueOf(fragment_num));
    if(fragment_num==1){
        getFragmentManager().beginTransaction().replace(R.id.workout_layout, exerciseFragment, "2").commit();
    } else if(fragment_num==2) {
        if(used_exercise_data.get(2).equals("bbb")){
            getFragmentManager().beginTransaction().remove(frag).commit();
            ex_num = ex_num + 1;
            ChangeExercise(ex_num, millisUnfinished);
        } else if (used_exercise_data.get(2).substring(used_exercise_data.get(2).length()-1).equals("a")){
            getFragmentManager().beginTransaction().replace(R.id.workout_layout, postExerciseFragment, "3").commit();
        } else {
            getFragmentManager().beginTransaction().replace(R.id.workout_layout, feedbackFragment, "4").commit();
        }
    } else if (fragment_num==3) {
        getFragmentManager().beginTransaction().replace(R.id.workout_layout, feedbackFragment, "4").commit();
    } else if (fragment_num==4) {
        getFragmentManager().beginTransaction().remove(frag).commit();
        ex_num = ex_num + 1;
        ChangeExercise(ex_num, millisUnfinished);
    }
}

现在这里是替换片段时会发生什么的示例屏幕截图。即使删除了一个片段并添加了一个新片段,也会产生这种结果。

Screenshot of the overlapped fragments http://jiyofit.in/appwork/Capture.JPG

当我添加具有倒数计时器的片段名称TotalTimerFragment时,这种情况就开始发生了。它总结了所有练习的时间,然后倒计时。这是代码。 total_timer_textview位于layout_totaltimer布局中。在获得总时间后,活动通过接口调用方法TotalTimerStartStop。

public class TotalTimerFragment extends Fragment{
TotalTimeCounter timeCounter;
TextView totalWorkoutTimer;
static long total_millis;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_totaltimer, container, false);
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
}

public class TotalTimeCounter extends CountDownTimer {
    TextView totalWorkoutTimer;
    View view;

    public TotalTimeCounter(long millisInFuture, long countDownInterval) {
        super(millisInFuture, countDownInterval);
        view = getView();
    }

    @Override
    public void onTick(long millisUntilFinished) {
        total_millis = millisUntilFinished;
        String total_hms = String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(total_millis),
                        TimeUnit.MILLISECONDS.toMinutes(total_millis) - TimeUnit.HOURS.toMinutes(TimeUnit.
                        MILLISECONDS.toHours(total_millis)), TimeUnit.MILLISECONDS.toSeconds(total_millis) -
                        TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(total_millis)));
        totalWorkoutTimer = (TextView) view.findViewById(R.id.total_timer_textview);
        totalWorkoutTimer.setText(total_hms);
    }

    @Override
    public void onFinish() {

    }
}

public void TotalTimerStartStop (long totalTimeVal){
    timeCounter = new TotalTimeCounter(totalTimeVal, 1000);
    timeCounter.start();
}
}

看起来前一个片段仍然附加。但我不明白为什么。感谢。

活动的xml

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:orientation="horizontal"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
  android:id="@+id/workout_layout"
  tools:context="in.jiyofit.the_app.WorkoutActivity"
  android:weightSum="1">

   <fragment
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:name="in.jiyofit.the_app.TotalTimerFragment"
    android:id="@+id/timer_fragment"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />
  </RelativeLayout>

其中一个片段的xml(其他类似)

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="horizontal"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:id="@+id/postexercise_fragment">

 <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <pl.droidsonroids.gif.GifImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/postexercise_gifView"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Large Text"
        android:id="@+id/postexercise_timer"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:padding="10dp"
        android:textSize="30sp"/>

   </RelativeLayout>

   </LinearLayout>

TotalTimerFragment的布局

  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical" android:layout_width="match_parent"
  android:layout_height="match_parent">

  <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:id="@+id/total_timer_textview"
    android:layout_gravity="center_horizontal"
    android:layout_margin="10dp"/>
  </LinearLayout>

0 个答案:

没有答案