1

添加运行时视图,以便我可以在 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是需要在运行时添加到数据的视图。

4

1 回答 1

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

只需使您的视图数据绑定兼容,您就可以设置数据、处理程序和所有内容。

于 2018-10-15T10:55:33.117 回答