livedata

2022-06-15  本文已影响0人  闫鹏飞写字的地方

官方地址:https://developer.android.google.cn/topic/libraries/architecture/livedata

B站视频:https://www.bilibili.com/video/BV1fY4y1i7BG?p=4&vd_source=c9e619eb6c2ba53337eccc49eb025732

class NameViewModel : ViewModel() {

    // Create a LiveData with a String
    val currentName: MutableLiveData<String> by lazy {
        MutableLiveData<String>()
    }

    // Rest of the ViewModel...
}
private const val TAG = "MyTest"

class MainActivity : AppCompatActivity() {

    // Use the 'by viewModels()' Kotlin property delegate
    // from the activity-ktx artifact
    private val model: NameViewModel by viewModels()

    private var nameTextView:TextView? = null
    private var clickButton:Button? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // Other code to setup the activity...

        nameTextView = findViewById(R.id.nameTextView)
        clickButton = findViewById(R.id.clickButton)

        clickButton?.setOnClickListener{
            val anotherName = "John Doe"
            //主线程用setValue
            model.currentName.setValue(anotherName)
            //子线程用postValue
//            model.currentName.postValue(anotherName)
        }

        // Create the observer which updates the UI.
        val nameObserver = Observer<String> { newName ->
            // Update the UI, in this case, a TextView.
            nameTextView?.setText(newName)
        }

        // Observe the LiveData, passing in this activity as the LifecycleOwner and the observer.
        model.currentName.observe(this, nameObserver)

    }
}

postValue 与 setValue
https://www.cnblogs.com/button123/p/14871526.html

map

class LivedataForMapActivity : AppCompatActivity() {

    private val testLiveData = MutableLiveData<Int>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_livedata_for_map)
        val tvContent = findViewById<TextView>(R.id.tvContent)
        val function: (input: Int) -> String = { input -> "livedata发送过来的数据是->$input" }
        var liveMap = Transformations.map(testLiveData, function)
        liveMap.observe(this){
            tvContent.text = it
        }
    }

    fun seed(view: android.view.View) {
        testLiveData.value = 123;
    }
}

switchmap

class LivedataForSwitchMapActivity : AppCompatActivity() {

    private val switchLiveData = MutableLiveData<Boolean>()
    private val liveData1 = MutableLiveData<String>()
    private val liveData2 = MutableLiveData<String>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_livedata_for_switch_map)
        val tvContent = findViewById<TextView>(R.id.tvContent)

        switchLiveData.value = false
        liveData1.value = "111"
        liveData2.value = "222"

        var switchMapLiveData =
            Transformations.switchMap(switchLiveData, object : Function<Boolean, LiveData<String>> {
                override fun apply(input: Boolean): LiveData<String> {
                    if (input) {
                        return liveData1
                    }
                    return liveData2
                }
            })

        switchMapLiveData.observe(this){
            tvContent.text = it
        }
    }

    fun seed(view: android.view.View) {
        var value = switchLiveData.value
        switchLiveData.value = !value!!
    }

}
上一篇 下一篇

猜你喜欢

热点阅读