Hyooosong/step.01#6
Conversation
Step.01 dataBinding local_value Related to : #2
jinsu4755
left a comment
There was a problem hiding this comment.
๋๋ฌด๋๋ฌด ์๊ณ ํ์ด~ ํ์ง๋ง invalidateAll ์ด๋ผ๋ ์น๊ตฌ๋ ์กฐ๊ธ ๋ ์ฐพ์๋ณด๊ณ ์์๋ณด๊ณ ๊ณ ์น ์ ์์ ๊ณ ์ณ๋ณด์!
| dataBinding { | ||
| enabled = true |
| import sotp.semina.practicegitanddatabinding.databinding.ActivityMainBinding | ||
|
|
||
| class MainActivity : AppCompatActivity() { | ||
| private lateinit var binding: ActivityMainBinding |
There was a problem hiding this comment.
์ด๊ฑฐ ์ฐธ๊ณ ํด๋ณด๋ฉด ์ข์ ๊ฑฐ ๊ฐ์!
|
|
||
| class MainActivity : AppCompatActivity() { | ||
| private lateinit var binding: ActivityMainBinding | ||
| var profilename = "profileName" |
There was a problem hiding this comment.
์ผ๋จ ๋ณ์ ์ด๋ฆ ๊ท์น์ ๋ฐ๋ผ profileName ์ด๋ผ๊ณ ์ ์ด์ฃผ๋ฉด ๋ ์ข์ ๊ฒ ๊ฐ์!
๋ง์ฝ ๋ถํ์ํ ๋ณ์๋ผ๋ฉด ์ง์์ฃผ๋ ๊ฒ์ด ์ข์
์ฌ์ง์ด ๊ทธ๊ฒ val๋ ์๋๊ณ var์ด๋ผ๋ฉด ํด๋น ํ๋กํผํฐ๋ ์๋ฐ์์ ๋งด๋ฒ๋ณ์,getter,setter์ธ๋ฐ ๋ณ์ ํ๋ ์๋ชป ๋ฃ์ด๋๋ฉด ๋ถํ์ํ ์ฝ๋๋ฅผ ๋ฌด๋ ค 3๊ฐ์ง๋ ๋ง๋ค๊ฒ ๋๋ฏ๋ก ๋๋ ์ข๋ค๊ณ ์๊ฐํ์ง ์์...
| profileName.text = content | ||
| private fun setProfileName(content:String) { | ||
| profilename = content | ||
| binding.invalidateAll() |
There was a problem hiding this comment.
์ต? ๋ ์ด ์น๊ตฌ๋ฅผ LiveData๋ง ์จ์ ์ฒ์๋ณด๊ณ ์์๋ดค๋๋ฐ ํ ...ํผํฌ๋จผ์ค์ ์ผ๋ก ์์ฃผ ์ ์ข์๊ฑฐ ๊ฐ์.
Invalidates all binding expressions and requests a new rebind to refresh UI. ๋ผ๋๋ฐ
UI๋ฅผ ๊ณ ์น๊ธฐ ์ํด ๋ชจ๋ ๋ฐ์ดํฐ ๊ฒฐํฉ์์ ๋ฌด์ํ๊ณ ๋ค์ ๋ฐ์ธ๋ฉํ๋ค๋ notifyDataSetChanged ๋ณด๋ค ๋ ํ ๊ธฐ๋ถ์ธ๊ฑธ..? ๋ทฐ๊ฐ ๊ฐ๋จํ ๋ชจ๋ฅด๊ฒ ์ง๋ง ๋ทฐ๊ฐ ๋ณต์กํ๋ค๋ฉด ํด๋น ํจ์์ ์ฌ์ฉ์ ํ๋ฉด ๊น๋นก์์ด ์ผ์ด๋ ์ ์๋ค ์๊ฐํด.
๊ทธ๋ฅ ํด๋น ํ
์คํธ ๋ทฐ์ text๋ฅผ ๋ณ๊ฒฝํ๋ฉด ๋๋๊ฑธ
textView .text์ ๋ณ๊ฒฝ์ ์ํด์ ๋ชจ๋ ๋ฐ์ดํฐ๋ฅผ ๋ค์ ์ฐ๊ฒฐํ๋ค..? ์์ฃผ ๋นํจ์จ์ ์ด๋ผ ์๊ฐํด
| binding.textProfileName, | ||
| getEditTextToString(binding.inputChangedProfile) | ||
| ) | ||
| setProfileName(getEditTextToString(findViewById(R.id.input_changed_profile))) |
There was a problem hiding this comment.
์ด ๋ถ๋ถ์ dataBinding์ ์ฌ์ฉํจ์๋ findViewById๋ฅผ ์ฌ์ฉํ๊ณ ์๋ค!
dataBinding์ด ์ด๋ฌํ ๊ธ๋ฃจ์ฝ๋๋ฅผ ์ฌ๋ผ์ง๊ฒ ํ๊ธฐ ์ํจ์ธ๊ฑธ?
| app:layout_constraintStart_toStartOf="parent" | ||
| app:layout_constraintTop_toBottomOf="@+id/image_profile_image" | ||
| tools:text="profile name" /> | ||
| tools:text="@{mainActivity.profilename}" /> |
There was a problem hiding this comment.
์์ ๋ฆฌ๋ทฐ๋ค๊ณผ ์ด์ด๋ณด๋ฉด ์๋ง ์ด ๋ถ๋ถ์ด ์ด๋ ๊ฒ ๋ ํ์๊ฐ ์์์์ ์ ์ ์์ ๊ฑฐ์ผ!
๋ญ๊ฐ ์ฝ๋๊ฐ ๋ง์น ํด๋น profileName์ด LiveData์ด๋ฉฐ Activity๋ฅผ ViewModel์ฒ๋ผ ์ฐ๊ฒฐํ๋๋ฐ ๋ ๋ค ์ฌ์ฉ์ค์ด์ง ์์ผ๋ฏ๋ก ๊ตณ์ด? ์ด๋ด ํ์๋ ์์ ๊ฑฐ ๊ฐ์
๋ค๋ง ๋ณด๋ค ์๋ฒฝํ DataBinding์ ์ํด์ LiveData, ViewModel๋ฅผ ์จ์ค๋ค๋ฉด ์ด๋ ๊ฒ ํ๋ ๊ฒ์ด ๋ง์!
Step.01 dataBinding local_value ์ค์ต