如何在framgent视图分页器内部添加片段

时间:2018-09-23 21:14:38

标签: java android android-fragments android-viewpager

我正在使用视图寻呼机。它包含三个片段。单击按钮时,我需要在一个视图分页器片段上添加片段,但是这样做存在问题。

这是应该打开新片段的Java代码,但是其中有问题

        Toast.makeText(context, "clicked", Toast.LENGTH_SHORT).show();
        Bundle lessonData = new Bundle();
        LessonContent lessonContent = new LessonContent();
        lessonData.putString("lessonTitle", mylist.getTitle());
        lessonData.putString("lessonContent", mylist.getContent());
        lessonContent.setArguments(lessonData);
        ((Activity) context).getFragmentManager().beginTransaction()
                .add(lessonContent, null)//I think this line is wrong
                .addToBackStack(null)
                .commit();

1 个答案:

答案 0 :(得分:1)

.add(lessonContent, null)//I think this line is wrong

应该替换为:

.replace(R.id.yourfragmentcontainerid, lessonContent) // or android.R.id.content

然后它应该工作。关键是,它并没有显示布局,所以它什么也不显示。

相关问题