java - 查找动态创建的 fragment 的内部 View

标签 java android android-layout android-fragments fragmentmanager

我已动态地将一个 fragment 添加到另一个 fragment 。

我需要在该 fragment 中找到一些 View 。

我如何实现这一目标?

这是我的代码:

@Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
    {
        View view = inflater.inflate(R.layout.fragment_educational, container, false);

        if (view != null)
        {
            ButterKnife.inject(this, view);
        }


        DisposableEducationalFragment disposableEducationalFragment = new DisposableEducationalFragment();
        getChildFragmentManager().beginTransaction().add(R.id.fragmentContainer,disposableEducationalFragment).addToBackStack(null).commit();

我想在该动态 fragment 中找到这些 View :

howItWorks = (RelativeLayout) fragmentView.findViewById(R.id.HowItWorks);
            howWeHelp = (RelativeLayout) fragmentView.findViewById(R.id.HowWeHelp);
            knowYourBloodPressure = (RelativeLayout) fragmentView.findViewById(R.id.KnowYourBloodPressure);
            learnAboutBirthControl = (RelativeLayout) fragmentView.findViewById(R.id.LearnAboutBirthControl);

最佳答案

因此,将View view保存到临时变量中并稍后调用它。

tmpView = View ;

稍后的某个地方:

howItWorks = (RelativeLayout) tempView.findViewById(R.id.HowItWorks);

或者,也许,只是尝试一下:

howItWorks = (RelativeLayout) getActivity().findViewById(R.id.HowItWorks);

关于java - 查找动态创建的 fragment 的内部 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40006481/

相关文章:

java - 对话框上的 NullPointerException - Android

android - 计算 flexbox 中的行数

java - 使用支持包中的 fragment 创建选项卡栏

android - 如何以编程方式设置一些 imeOptions

java Callable FutureTask Excecuter : How to listen to finished task

JavaFX 抛出 ArrayIndexOutOfBoundsException

java - 配置文件 Java CPU 负载

android - 使用不带 MediaExtractor 的 MediaCodec 解码 mp4/h.264,预期访问单元格式

java - 将图像加载为文件

android - 如何实现 CordovaInterface 以便我可以在 webview 中使用相机?