ANDROID RECYCLERVIEW - DiffUtil and Data binding with RecyclerView 02

Dmitry Klokov·2020년 12월 27일
0

ANDROID-KOTLIN-FUNDAMENTALS

목록 보기
13/19
post-thumbnail
post-custom-banner

Use DataBinding with RecyclerView

<!-- list_item_sleep_night.xml -->

<layout
        ...>
    <data>
        <variable
            name="sleep"
            type="com.example.android.trackmysleepquality.database.SleepNight" />
    </data>
  	...
</layout>

Create binding adapters

Concept

In a previous codelab, you used the Transformations class to take LiveData and generate formatted strings to display in text views. However, if you need to bind different types, or complex types, you can provide binding adapters to help data binding use those types. Binding adapters are adapters that take your data and adapt it into something that data binding can use to bind a view, like text or an image.

You are going to implement three binding adapters, one for the quality image, and one for each text field. In summary, to declare a binding adapter, you define a method that takes an item and a view, and annotate it with @BindingAdapter. In the body of the method, you implement the transformation. In Kotlin, you can write a binding adapter as an extension function on the view class that receives the data.

Before

After

BindingUtils.kt & list_item_sleep_night.xml

profile
Power Weekend
post-custom-banner

0개의 댓글