Using LiveData with Data Binding

后端 未结 4 439
情书的邮戳
情书的邮戳 2020-12-28 14:28

With the stabilization of Android Architecture Components I started updating all my basic ViewModels to the new implementation ofViewModel. In my understanding,

4条回答
  •  生来不讨喜
    2020-12-28 14:53

    The accepted answer does not give an example. So here's one I've tested and it works.

    In layout:

    
        
            
        
    
        ...
    
        
    
        ...
    
    
    

    In fragment:

    override fun onCreateView(inflater: LayoutInflater, 
             container: ViewGroup?, savedInstanceState: Bundle?): View? {
            val binding: FragmentAlbumBinding = DataBindingUtil.inflate(
                inflater, R.layout.fragment_album, container, false)
            binding.apply {
                fragment = this
                viewModel = albumViewModel
                lifecycleOwner = this
        }
    }
    

提交回复
热议问题