SHARE
TWEET

Untitled

a guest Apr 20th, 2019 90 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package sample.binding
  2.  
  3. import android.arch.lifecycle.MutableLiveData
  4. import android.databinding.DataBindingUtil
  5. import android.os.Bundle
  6. import android.support.v7.app.AppCompatActivity
  7. import android.view.View
  8. import com.devindi.wallpaper.R
  9. import com.devindi.wallpaper.databinding.BindingExampleBinding
  10.  
  11. class CounterActivityBinding : AppCompatActivity() {
  12.  
  13.     val viewModel = CounterViewModel()
  14.  
  15.     override fun onCreate(savedInstanceState: Bundle?) {
  16.         super.onCreate(savedInstanceState)
  17.  
  18.         val view = View(this)
  19.  
  20.         val binding = DataBindingUtil.setContentView<BindingExampleBinding>(this, R.layout.binding_example)
  21.         binding.setLifecycleOwner(this)
  22.         binding.counter = viewModel
  23.     }
  24. }
  25.  
  26. class CounterViewModel {
  27.  
  28.     private val model = CounterRepository()
  29.  
  30.     val counter = model.counter
  31.  
  32.     fun increment(view: View) {
  33.         model.increment()
  34.     }
  35. }
  36.  
  37. class CounterRepository {
  38.  
  39.     val counter = MutableLiveData<Int>()
  40.  
  41.     init {
  42.         counter.value = 0
  43.     }
  44.  
  45.     fun increment() {
  46.         counter.value = counter.value!! + 1
  47.     }
  48. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top