Kotlin协程Flow特性之StateFlow与SharedFlow

文章目录

  • 前言

    一、设置Fragment与布局文件

    二、设置ViewModel

    总结


前言

学过Flow可以知道,这是一种冷流,就是订阅者使用的时候,flow发出的流才会保存在内存当中,这种模式更适合一些连续的,不变的的数据传输(已知),但是,我们在实际中可能需要可变的、实时更新的数据,这就需要使用StateFlow,与LiveData 类似,StateFlow 通常与视图相关联,并且在视图的生命周期内保持活动状态,(是一种热流,存在内存中,时刻保持活跃),还能通过value属性读取当前状态值

SharedFlow:功能类似广播,只是性质不一样,简单来讲就是一个流可以被多个收集到,也是一种热流。

下面用StateFlow为例,做一个简单的计数显示功能

前期工作使用了navigation组件,有不懂的可以看我之前写的


一、设置Fragment与布局文件



    
        
        
            

package com.example.applicationflow.fragment

import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import com.example.applicationflow.R
import com.example.applicationflow.databinding.FragmentNumberBinding
import com.example.applicationflow.databinding.FragmentRoomBinding
import com.example.applicationflow.viewmodel.NumberViewModel
import kotlinx.coroutines.flow.collect

class numberFragment : Fragment() {
    private val viewModel by viewModels()//拿到viewModel
    private val mBinding: FragmentNumberBinding by lazy{
        Log.d("feng", "没有问题1")
        FragmentNumberBinding.inflate(layoutInflater)//视图绑定的快捷方式

    }
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        return mBinding.root
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        //进行了视图绑定viewBinding可以直接使用
        mBinding.apply {
            buttonnumber1.setOnClickListener {
                viewModel.increment()
            }
            buttonnumber2.setOnClickListener {
                viewModel.decremnet()
            }
        }
        //进行赋值
        lifecycleScope.launchWhenCreated {
            viewModel.number.collect{
                value ->
                mBinding.tvNumber.text = "$value"
            }
        }
    }
}

二、设置ViewModel

package com.example.applicationflow.viewmodel

import androidx.lifecycle.ViewModel
import kotlinx.coroutines.flow.MutableStateFlow

class NumberViewModel : ViewModel(){
    //类似liveData
    val number = MutableStateFlow(0)
    fun  increment(){
        number.value++
    }
    fun decremnet(){
        number.value++
    }

}

 三、运行如下

Kotlin协程Flow特性之StateFlow与SharedFlow_第1张图片


总结

这倒没啥难度,就当作理解stateFlow的使用和加强练习了

你可能感兴趣的:(kotlin,开发语言,android)