添加运行时 View ,以便如何在MVVM中添加该 View 数据。
这是代码
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
是在运行时需要添加到数据中的 View 。 最佳答案
您可以通过以下代码轻松将其存档,
DynamicFromToBinding dynamicFromToBinding = DataBindingUtil.inflate(getLayoutInflater(), R.layout.dynamic_from_to, fragmentHomeBinding.llDynamicLayoutContainer, false);
dynamicFromToBinding.setData(data)
View dynamicToFromView = dynamicFromToBinding.getRoot();
fragmentHomeBinding.llDynamicLayoutContainer.addView(dynamicToFromView);
只需使您的 View 数据绑定(bind)兼容,就可以设置数据,处理程序等。