【问题标题】:Find internal views of a dynamically created fragment查找动态创建的片段的内部视图
【发布时间】:2016-10-12 19:06:36
【问题描述】:

我已经动态地将一个片段添加到另一个片段。

我需要在该片段中找到一些视图。

我如何做到这一点?

这是我的代码:

@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();

我想在该动态片段中找到这些视图:

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);

【问题讨论】:

    标签: java android android-layout android-fragments fragmentmanager


    【解决方案1】:

    所以,将View view 保存到一个临时变量中,稍后再调用它。

    tmpView = view;

    以及稍后的某个地方:

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

    或者,也许,试试吧:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-08-03
      • 1970-01-01
      • 2016-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多