我目前有 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;
}
}
我认为问题可能是因为我使用了相关布局的 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>
任何帮助或朝着正确的方向推动将不胜感激,因为我真的被难住了,似乎找不到任何能让我接近答案的东西。
提前致谢