ANDROID RECYCLERVIEW - DiffUtil and Data binding with RecyclerView 03

Dmitry Klokov·2020년 12월 27일
0

ANDROID-KOTLIN-FUNDAMENTALS

목록 보기
14/19
post-thumbnail

SUMMARY

DiffUtil

  • RecyclerView has a class called DiffUtil which is for calculating the differences between two lists.
  • DiffUtil has a class called ItemCallBack that you extend in order to figure out the difference between two lists.
  • In the ItemCallback class, you must override the areItemsTheSame() and areContentsTheSame() methods.

ListAdapter

  • To get some list management for free, you can use the ListAdapter class instead of RecyclerView.Adapter.
    However, if you use ListAdapter you have to write your own adapter for other layouts, which is why this codelab shows you how to do it.
  • To open the intention menu in Android Studio, place the cursor on any item of code and press Alt+Enter (Option+Enter on a Mac). This menu is particularly helpful for refactoring code and creating stubs for implementing methods. The menu is context-sensitive, so you need to place cursor exactly to get the correct menu.

Data binding

  • Use data binding in the item layout to bind data to the views.

Binding adapters

  • You previously used Transformations to create strings from data. If you need to bind data of different or complex types, provide binding adapters to help data binding use them.
  • To declare a binding adapter, define a method that takes an item and a view, and annotate the method with @BindingAdapter. In Kotlin, you can write the binding adapter as an extension function on the View. Pass in the name of the property that the adapter adapts. For example:
  • In the XML layout, set an app property with the same name as the binding adapter. Pass in a variable with the data. For example:

LEARN MORE

Android developer documentation :

Other resources :

profile
Power Weekend

0개의 댓글