1

Hi I've pseudoinfinite ViewPager with FragmentStatePagerAdapter. I dynamically add CalendarFragments into it. I have member variable mUpdatedShiftsCont what is connect point to another inner fragment:

CalendarFragment.java:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle) {
  ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.calendar_fragment, container, false);
  mCalendarView = (GridView) rootView.findViewById(R.id.calendar);
  mUpdatedShiftsCont = (FrameLayout)  rootView.findViewById(R.id.calendar_updated_shifts_container);

  return rootView;
}

layout.calendar_fragment:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:id="@+id/calendar_fragment_id"
  android:orientation="vertical" >

  ...

  <FrameLayout
     android:paddingTop="30dp"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:id="@+id/calendar_updated_shifts_container" />

</LinearLayout>

When I try add inner fragment to connect point,

getFragmentManager().beginTransaction()
  .add(mUpdatedShiftsCont.getId(), fragment).commit();

all nested fragments are placed in one parent fragment in ViewPager:

Does anyone have idea how to solve this? Thanks in advance.

skywall
  • 3,956
  • 1
  • 34
  • 52

1 Answers1

1

I have finally found where is problem. When I was adding inner fragment to fragment in ViewPager, I used method mUpdatedShiftsCont.getId() to get container ID. When I try to print containers' IDs in all ViewPager's fragments I get this result:

Log.d("CF", "ID: " + String.valueOf(mUpdatedShiftsCont.getId()));

09-27 23:14:05.154    8388-8388/cz.skywall.sichtovnik_v2 D/CF: ID: 2131492885
09-27 23:14:05.170    8388-8388/cz.skywall.sichtovnik_v2 D/CF: ID: 2131492885
09-27 23:14:05.193    8388-8388/cz.skywall.sichtovnik_v2 D/CF: ID: 2131492885
09-27 23:14:05.225    8388-8388/cz.skywall.sichtovnik_v2 D/CF: ID: 2131492885
09-27 23:14:05.256    8388-8388/cz.skywall.sichtovnik_v2 D/CF: ID: 2131492885

So different containers (because different fragments) have same ID. Solution:

mUpdatedShiftsCont.setId(1000 + mCurrentMonth + mCurrentYear);
getFragmentManager().beginTransaction()
  .add(1000 + mCurrentMonth + mCurrentYear, fragment).commit();

I have set new IDs based od fragment's content. They are now unique (probably) and It works as I want to.

skywall
  • 3,956
  • 1
  • 34
  • 52
  • 1
    If there's only a small number of finite fragments in your pager, you could define the IDs in your res/values folder and use those (to guarantee uniqueness). – MH. Sep 27 '14 at 23:49
  • Thanks for advice. Unfortunately I have o lot of fragments. – skywall Sep 28 '14 at 10:56
  • 1
    @skywall you solve my problem, thanks! Since I only have 4 fragment, so I follow @MH. suggestion to define IDs in `res/values/ids`. – Weiyi Aug 07 '15 at 02:57