Android ViewPager重叠中的动态内部片段

Android ViewPager重叠中的动态内部片段,android,android-fragments,android-viewpager,fragmentpageradapter,Android,Android Fragments,Android Viewpager,Fragmentpageradapter,您好,我有一个带有FragmentStatePagerAdapter的伪无限ViewPager。我动态地向其中添加CalendarFragments。我有一个成员变量mUpdatedShiftsCont什么是连接到另一个内部片段的连接点: CalendarFragment.java: @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle) { ViewGro

您好,我有一个带有FragmentStatePagerAdapter的伪无限
ViewPager
。我动态地向其中添加
CalendarFragment
s。我有一个成员变量
mUpdatedShiftsCont
什么是连接到另一个内部片段的连接点:

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;
}
<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>
mUpdatedShiftsCont.setId(1000 + mCurrentMonth + mCurrentYear);
getFragmentManager().beginTransaction()
  .add(1000 + mCurrentMonth + mCurrentYear, fragment).commit();
布局。日历\u片段:

@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;
}
<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>
mUpdatedShiftsCont.setId(1000 + mCurrentMonth + mCurrentYear);
getFragmentManager().beginTransaction()
  .add(1000 + mCurrentMonth + mCurrentYear, fragment).commit();
所有嵌套片段都放置在
ViewPager
中的一个父片段中:


有人知道如何解决这个问题吗?提前谢谢。

我终于找到了问题所在。当我在
ViewPager
的片段中添加内部片段时,我使用方法
mUpdatedShiftsCont.getId()
获取容器ID。当我尝试在所有
ViewPager
的片段中打印容器ID时,我得到以下结果:

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
所以不同的容器(因为不同的片段)具有相同的ID。解决方案:

@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;
}
<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>
mUpdatedShiftsCont.setId(1000 + mCurrentMonth + mCurrentYear);
getFragmentManager().beginTransaction()
  .add(1000 + mCurrentMonth + mCurrentYear, fragment).commit();

我已经根据od片段的内容设置了新的ID。它们现在是唯一的(可能),它可以按照我的意愿工作。

你想添加或替换你的内部片段吗?我想在
ViewPager
中将内部片段添加到片段中。请查看此内容,谢谢,我以前读过此内容,但实际上没有回答我的问题。我想告诉你的是不要使用add-replace-getFragmentManager().beginTransaction().replace(mupdatedShift.getId(),fragment.commit();如果您的寻呼机中只有少量有限片段,您可以在res/values文件夹中定义ID并使用这些ID(以确保唯一性)。谢谢您的建议。不幸的是我有很多碎片。@skywall你解决了我的问题,谢谢!因为我只有4个碎片,所以我跟随@MH。建议在
res/values/IDs
中定义id。