πŸ€”
TIL
  • README
  • root
    • Android
      • TextView
      • Intent
      • RecyclerView
      • ImageView
      • Style
      • Activity
      • Databinding
      • EditText
      • RxJava
      • Fragment
    • Code
      • κ°€μž₯ λ§Žμ€ 정보λ₯Ό κ°€μ§„ 객체
    • Shell
      • script
        • 초기 μ„€μ •
        • κΈ°λ³Έ 문법
      • chmod
  • Blog
  • Github
Powered by GitBook
On this page
  • observableField.set() λ©”μ†Œλ“œ 호좜 ν›„, notify κ°€ μ•ˆλ˜λŠ” 이슈
  • executePendingBindings()
  • bindingAdapter 와 콜백
  • custom BindingAdapter λ§Œλ“€μ‹œ μ£Όμ˜μ‚¬ν•­
  • <merge>
  • <include>
  • String format

Was this helpful?

  1. root
  2. Android

Databinding

observableField.set() λ©”μ†Œλ“œ 호좜 ν›„, notify κ°€ μ•ˆλ˜λŠ” 이슈

observableField 의 set λ©”μ†Œλ“œλ₯Ό μ΄μš©ν•˜μ—¬ 데이터λ₯Ό μ „λ‹¬ν–ˆμŒμ—λ„ λΆˆκ΅¬ν•˜κ³  가끔 μ •μƒμ μœΌλ‘œ μ „λ‹¬λ˜μ§€ μ•ŠλŠ” μ΄μŠˆκ°€ μžˆμ—ˆμŒ.

μ΄μœ λŠ” set λ©”μ†Œλ“œ λ‚΄λΆ€κ΅¬μ—μ„œ 이전에 μ „λ‹¬λ˜μ—ˆλ˜ 데이터와 같은 경우 notify λ₯Ό ν˜ΈμΆœν•˜μ§€ μ•ŠμŒ. λ”°λΌμ„œ set 호좜 이후에 λͺ…μ‹œμ μœΌλ‘œ notifyChange() λ₯Ό ν˜ΈμΆœν•΄μ£Όλ©΄ μ •μƒμ μœΌλ‘œ 전달됨 .

observableField.set(data);
observableField.notifyChange();

executePendingBindings()

κ°•μ œλ‘œ λ°”μΈλ”©λ˜μ–΄μžˆλŠ” 데이터듀을 μ΄μš©ν•˜μ—¬ View λ₯Ό κ°±μ‹ ν•œλ‹€. 보톡 recyclerView μ—μ„œ 많이 μ‚¬μš©ν•¨. κ²°κ΅­ UI thread 에 view set ν…ŒμŠ€νŠΈκ°€ μŒ“μ΄λŠ” κ²ƒμ΄λ―€λ‘œ λ„ˆλ¬΄ 많이 호좜돼면 퍼포먼슀 이슈 생길 수 있음.

bindingAdapter 와 콜백

  1. λ©”μ†Œλ“œ μ‹œκ·Έλ‹ˆμ³λ₯Ό κ·ΈλŒ€λ‘œ λ§žμΆ°μ„œ 전달.

@{viewModel::onChange}

2. ν•΄λ‹Ή 콜백의 κ΅¬ν˜„μ²΄λ₯Ό λ„˜κ²¨μ€Œ

@{Listener}

3. λžŒλ‹€μ‹ 이용

@{param -> viewModel.onChange(param)}

μ΄λ•Œ μΈμžλŠ” 1개만 κ°€λŠ₯ν•˜λ‹€.

custom BindingAdapter λ§Œλ“€μ‹œ μ£Όμ˜μ‚¬ν•­

  • BindinAdapter λ©”μ†Œλ“œμ˜ 첫번째 μΈμžλŠ” 항상 View νƒ€μž…μ΄μ–΄μ•Ό ν•œλ‹€.

<merge>

λ ˆμ΄μ•„μ›ƒ μ΅œμƒμœ„ λ ˆλ²¨μ—μ„œ <merge> λ₯Ό μ‚¬μš©ν•˜κ³  데이터바인딩 μ μš©μ‹œ inflate κ³Όμ •μ—μ„œ μ—λŸ¬. 개발자 κ°€μ΄λ“œμ—μ„œλ„ μ‚¬μš©ν•˜μ§€ 말라고 λ‚˜μ™€μžˆμŒ. μ΄μœ λŠ” μ’€ 더 μ°Ύμ•„λ³Ό 것

<include>

<OuterView>

    <include/>

</OuterView>

μ΄λŸ¬ν•œ ꡬ쑰의 View κ°€ μžˆμ„ λ•Œ Outer depth μ—μ„œ include 의 View 에 μ •μ˜λœ 데이터λ₯Ό λ„˜κ²¨μ£Όκ³  싢을 경우 bind: ν™œμš©ν•  것

μ΄λ•Œ bind 에 λ“€μ–΄κ°€λŠ” 이름은 λ°˜λ“œμ‹œ include μ—μ„œ μ§€μ •ν•œ variable name κ³Ό μΌμΉ˜ν•΄μ•Όν•¨.

String format

// strings.xml
<string name="user_list_title">μ°Έμ—¬μœ μ Έ %d</string>

// activity.xml
<TextView
    android:text="@{@string/user_list_title(viewModel.userCount)}"/>

PreviousActivityNextEditText

Last updated 5 years ago

Was this helpful?