Android Jetpack

BindingAdapter + databinding

----___<<<<< 2022. 8. 5. 04:14

 

class MainActivity : AppCompatActivity() {

    private lateinit var viewModel: MainViewModel
    private lateinit var binding : ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {

        super.onCreate(savedInstanceState)
        binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
        viewModel = ViewModelProvider(this).get(MainViewModel::class.java)
        binding.vm = viewModel
        binding.lifecycleOwner = this

        binding.plus.setOnClickListener {
            viewModel.plus()
        }

    }

}

 

@BindingAdapter("app:myAge")
fun myAgeSetting(view : TextView, age : Int) {

    if(age > 10) {
        view.text = age.toString() + "보다 많음"
    } else {
        view.text = age.toString() + "보다 적음"
    }

}

@BindingAdapter("app:myImg")
fun myImgSetting(view : ImageView, age:Int) {
    if(age > 10) {
        view.setImageResource(R.drawable.bok)
    } else {
        view.setImageResource(R.drawable.bok2)
    }
}

 

class MainViewModel : ViewModel() {

    private var _mutableAge = MutableLiveData(0)
    val liveWord : LiveData<Int>
        get() = _mutableAge

    fun plus(){
        _mutableAge.value = _mutableAge.value?.plus(1)
    }


}

'Android Jetpack' 카테고리의 다른 글

dataStore - 2  (0) 2022.08.15
dataStore - 1  (0) 2022.08.06
ROOM TypeConverter  (0) 2022.07.23
ROOM Embeded  (0) 2022.07.22
ROOM Migration  (0) 2022.07.22