0

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)

Sornin
  • 105
  • 2
  • 10
  • You're adding two fragments to one container, so of course only one will be visible - I expect the most recently added one, `dishInWeekNotEmpty` – PPartisan Oct 16 '20 at 14:43
  • But it doesn't works like a scrollView that contains itself a linearLayout ? – Sornin Oct 16 '20 at 14:50
  • No. It isn't like adding a view - all that's happening is you're loading fragments on top of each other. Because you're calling `add`, the other fragments are probably still there, but they wont be visible because only the latest fragment is on top – PPartisan Oct 16 '20 at 14:51
  • OK thanks for the clarifications, I'll try a very different way to do it. Thanks again. – Sornin Oct 16 '20 at 14:55

1 Answers1

0

Hey you should not put it in OnCreateView but in OnCreate.