替换选项卡中的片段。之前的片段仍然在新片段后面活跃

时间:2013-03-03 22:47:16

标签: android android-fragments android-viewpager android-tabs

我目前有3个标签。选择的每个选项卡都可以通过滑动或按下选项卡来加载一个片段,然后膨胀XML布局。我也在使用viewpager和fragmentPagerAdapter。

在其中一个选项卡(以及将来的其他选项卡)中,我希望能够单击片段中的按钮,并使用新选项卡替换当前选项卡中的当前片段。在我离开的那一刻,按钮将显示新标签,但它位于旧标签的顶部,我仍然可以看到调用新片段并与之交互的旧按钮,以及按钮新标签。

如何确保正确更换片段?并且旧的片段放在后台上,所以我可以用标签返回它?

这是我更改片段的代码

public class CreateWorkoutFragment extends Fragment {

    private Button addExerciseButton;
    private FragmentNavigationHelper fragmentNavigationHelper;
    static CreateWorkoutFragment instance;

    public CreateWorkoutFragment() {
    }

    /*public static CreateWorkoutFragment newInstance()
    {
          if(instance == null)
          {
              instance = new CreateWorkoutFragment();
          }

        return instance;
    } */


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        ViewGroup view = (ViewGroup) inflater.inflate(R.layout.create_workout, null);

        fragmentNavigationHelper = new FragmentNavigationHelper();
        addExerciseButton = (Button) view.findViewById(R.id.addExercise_button);
        addExerciseButton.setOnClickListener( new OnClickListener()
        {
            @Override
            public void onClick(View v) {
                //fragmentNavigationHelper.changeFragmentWithTab((Fragment) new ExerciseCategoryFragment());

                Fragment newFragment = new ExerciseCategoryFragment();
                FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
                transaction.replace(R.id.fragment_container, newFragment);
                transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                transaction.addToBackStack(null);
                transaction.commit();
            }
        });

        return view;
    }
} 

我认为问题可能是因为我使用了relativelayout的id,所有内容都包含在我的createWorkout.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/createWorkout_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView
        android:id="@+id/createWorkoutTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:contentDescription="@string/begin_workout_title_description"
        android:src="@drawable/create_workout_image"
        android:visibility="visible" />

    <TextView
        android:id="@+id/day_textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/createWorkoutTitle"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="19dp"
        android:layout_marginLeft="14dp"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/dateMonth_textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/day_textview"
        android:layout_alignParentRight="true"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <Button
        android:id="@+id/addExercise_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true" />

    <Button
        android:id="@+id/NFCToggle_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_toLeftOf="@+id/addExercise_button"  />

</RelativeLayout>

任何帮助或推动正确的方向都会非常感激,因为我真的很难过,但似乎找不到任何能让我接近答案的东西。

提前致谢

0 个答案:

没有答案