我已动态地将一个 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/