Using LiveData with Data Binding

后端 未结 4 438
情书的邮戳
情书的邮戳 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:47

    For those who came across this question looking for an example like I did, here's one:

    In layout .xml put the LiveData element with it's type:

    <layout>
        <data>
            <variable
                name="myString"
                type="android.arch.lifecycle.MutableLiveData&lt;String&gt;"/>
        </data>
    
        ...
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text='@{myString}'
            ...
         />
    
        ...
    
    </layout>
    

    In your code set it's value and the lifecycle owner:

    MutableLiveData<String> myString = new MutableLiveData<>();
    
    ...
    
    binding.setLifecycleOwner(this);
    binding.setMyString(myString);
    

    That's it :)

    Note that the default value of LiveData elements is null so assign initial values to make sure you get the desired effect right away or use this to enforce nullability.

    EDIT: Use androidx.lifecycle.MutableLiveData&lt;String&gt; as type if you use androidx.

    0 讨论(0)
  • 2020-12-28 14:51

    The Android Studio 3.1 (currently in Canary 6) will fix this issue, since LiveData can be used as observable field:

    Updates to Data Binding:

    You can now use a LiveData object as an observable field in data binding expressions. The ViewDataBinding class now includes a new setLifecycle method that you need to use to use to observe LiveData objects.

    Source: Android Studio 3.1 Canary 6 is now available

    0 讨论(0)
  • 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:

    <layout>
        <data>
            <variable
                name="viewmodel"
                type="com.abc.xyz.viewmodels.MyViewModel"/>
        </data>
    
        ...
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text='@{viewmodel.myString}'
            ...
         />
    
        ...
    
    </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
        }
    }
    
    0 讨论(0)
  • 2020-12-28 15:11

    For androidx will be:

    androidx.lifecycle.MutableLiveData

    <layout>
        <data>
            <variable
                name="myString"
                type="androidx.lifecycle.MutableLiveData&lt;String&gt;"/>
        </data>
    
        ...
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text='@{myString}'
            ...
         />
    
        ...
    
    </layout>
    

    And for Kotlin:

      val myStr = MutableLiveData<String>()
    
    ...
    
     binding.apply {
                setLifecycleOwner(this)
                this.myString = myStr
            }
    

    Good luck! :)

    0 讨论(0)
提交回复
热议问题