添加运行时视图,以便如何在MVVM中添加该视图数据。
这是代码
FragmentHomeBinding fragmentHomeBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_home, container, false);
FragmentHomeViewModel fragmentHomeViewModel = new FragmentHomeViewModel(getActivity());
fragmentHomeBinding.setViewModel(fragmentHomeViewModel);
/*Adding Dynamic View At run time.*/
View dynamicToFromView = getLayoutInflater()
.inflate(R.layout.dynamic_from_to, fragmentHomeBinding.llDynamicLayoutContainer, false);
fragmentHomeBinding.llDynamicLayoutContainer.addView(dynamicToFromView);
R.layout.dynamic_from_to
是需要在运行时添加到数据的视图。
答案 0 :(得分:1)
您可以通过以下代码轻松将其存档,
DynamicFromToBinding dynamicFromToBinding = DataBindingUtil.inflate(getLayoutInflater(), R.layout.dynamic_from_to, fragmentHomeBinding.llDynamicLayoutContainer, false);
dynamicFromToBinding.setData(data)
View dynamicToFromView = dynamicFromToBinding.getRoot();
fragmentHomeBinding.llDynamicLayoutContainer.addView(dynamicToFromView);
只需使您的视图数据绑定兼容,就可以设置数据,处理程序和全部。