在运行时添加的视图与MVVM中的数据绑定

时间:2018-10-15 10:29:07

标签: android performance android-layout mvvm android-mvvm

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

1 个答案:

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

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