简体   繁体   中英

Android FragmentTransaction is committing only one Fragment

I'm facing a wall with Android ... I have a TabLayout working really fine with ViewPager . Into this ViewPager I'm rendering the same Fragment for each TabItem . In this Fragment , I have a LinearLayout which I want to commit into it two other Fragments . To do this I'm using a FragmentTrasaction (it worked fine on other use case) BUT it commit only one Fragment in the ViewPager . Only one Fragment is committed in all tabs. There's 7 Tabs (for a week) in each Tab there should be 2 Fragments , so at the end there should be 14 Fragment in total ... But there 's only one that moves each 3 slides gesture...

Here's the code I have into the OnCreateView of my Fragment that contains the LinearLayout :

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        
        final View view = inflater.inflate(R.layout.fragment_week_creator, container, false);
        TextView textViewDayOfWeek = view.findViewById(R.id.textViewDayOfWeek);
        textViewDayOfWeek.setText(this.dayOfWeek.getLongCharSeq());

        FragmentManager fragmentManager = getFragmentManager();
        assert fragmentManager != null;
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

        DishInWeek dishInWeekEmpty = DishInWeek.newInstance(true);
        DishInWeek dishInWeekNotEmpty = DishInWeek.newInstance(false);

        fragmentTransaction.add(R.id.linearLayoutWeekCreator, dishInWeekEmpty, "dishInWeek1");
        fragmentTransaction.add(R.id.linearLayoutWeekCreator, dishInWeekNotEmpty, "dishInWeek2");

        Log.i("dev", "Here I am !");
        fragmentTransaction.commit();

        return view;
    }

and here's the XML of the same Fragment :

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".fragment.WeekCreatorFragment">

    <TextView
        android:id="@+id/textViewDayOfWeek"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:textSize="24dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.022" />

    <LinearLayout
        android:id="@+id/linearLayoutWeekCreator"
        android:layout_width="409dp"
        android:layout_height="668dp"
        android:layout_marginTop="16dp"
        android:orientation="vertical"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textViewDayOfWeek" />

    </androidx.constraintlayout.widget.ConstraintLayout>

Is anyone faced this wall or anyone can help me passing this hard step ?

(I had some issues in the the XML on StackOverflow, please do not pay attention to the bad format of the second block of code, I'm sorry)

嘿,你不应该把它放在 OnCreateView 中,而应该放在 OnCreate 中。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM