DataBinding三种获取界面的方法

1.第一种,在Activity里设置的,也是比较常用的

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
     ActivityClothesListBinding binding= DataBindingUtil.setContentView(this,R.layout.activity_clothes_list);
    viewModel=new ClothesListViewModel(this,new Category());      
    binding.setViewmodel(viewModel);  }

2.第二种

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    final View root = inflater.inflate(R.layout.addtask_frag, container, false);
    if (mViewDataBinding == null) {
        mViewDataBinding = AddtaskFragBinding.bind(root);
    }
    mViewDataBinding.setViewmodel(mViewModel);
    setHasOptionsMenu(true);
    setRetainInstance(false);
    return mViewDataBinding.getRoot();
}

3.第三种

 public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    mTasksFragBinding = TasksFragBinding.inflate(inflater, container, false);

    mTasksFragBinding.setView(this);

    mTasksFragBinding.setViewmodel(mTasksViewModel);

    setHasOptionsMenu(true);

    View root = mTasksFragBinding.getRoot();

    return root;

你可能感兴趣的:(DataBinding三种获取界面的方法)