美文网首页
DataBinding三种获取界面的方法

DataBinding三种获取界面的方法

作者: 阳光下的美好_6e13 | 来源:发表于2018-12-14 17:24 被阅读4次

    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三种获取界面的方法

          本文链接:https://www.haomeiwen.com/subject/yitrnxtx.html