确保这件事:
1)您的片段(例如:ViewPagerFragment1)将由viewpager选择,具有一个FrameLayout作为根布局,ID为“container”,例如:
<FrameLayout
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:id="@+id/container"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout.... and so on
</FrameLayout>
2) 在 ViewPagerFragment1 类中,您必须在触发操作后将新片段替换/添加到 FrameLayout。例如:
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.selection:
// Create new fragment and transaction
NewFragment newFragment = new NewFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.container, newFragment, "NewFragment");
transaction.addToBackStack(null);
transaction.commit();
break;
default:
break;
}
}