当前位置: 首页 > news >正文

建立和创立的区别比优化更好的词是

建立和创立的区别,比优化更好的词是,cms开源建站系统,spring boot做网站Android StateFlow初探 前言: 最近在学习StateFlow,感觉很好用,也很神奇,于是记录了一下. 1.简介: StateFlow 是一个状态容器式可观察数据流,可以向其收集器发出当前状态更新和新状态更新。还可通过其 …

Android StateFlow初探

前言:

最近在学习StateFlow,感觉很好用,也很神奇,于是记录了一下.

1.简介:

StateFlow 是一个状态容器式可观察数据流,可以向其收集器发出当前状态更新和新状态更新。还可通过其 value 属性读取当前状态值。如需更新状态并将其发送到数据流,请为 MutableStateFlow 类的 value 属性分配一个新值。

2.和Flow、LiveData联系,官网解释如下:

StateFlow、Flow 和 LiveData

StateFlow 和 LiveData 具有相似之处。两者都是可观察的数据容器类,并且在应用架构中使用时,两者都遵循相似模式。

但请注意,StateFlow 和 LiveData 的行为确实有所不同:

  • StateFlow 需要将初始状态传递给构造函数,而 LiveData 不需要。
  • 当 View 进入 STOPPED 状态时,LiveData.observe() 会自动取消注册使用方,而从 StateFlow 或任何其他数据流收集数据的操作并不会自动停止。如需实现相同的行为,您需要从 Lifecycle.repeatOnLifecycle 块收集数据流。

3.MainViewModel代码:

package com.example.stateflowdemo.viewmodelimport androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.example.stateflowdemo.model.LoginUIState
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch
@ExperimentalCoroutinesApi
class MainViewModel :ViewModel(){private val _loginUIState = MutableStateFlow<LoginUIState>(LoginUIState.Empty)val loginUiState: StateFlow<LoginUIState> = _loginUIStatefun login(username:String,password: String) = viewModelScope.launch {_loginUIState.value = LoginUIState.Loadingdelay(2000L)if(username == "android" && password == "123456") {_loginUIState.value = LoginUIState.Success} else {_loginUIState.value = LoginUIState.Error("账号或密码不正确,请重试")}}
}

4.LoginUIState代码:

sealed class LoginUIState {object Success : LoginUIState()data class Error(val message: String) : LoginUIState()object Loading : LoginUIState()object Empty : LoginUIState()
}

5.测试代码:

package com.example.stateflowdemoimport androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.activity.viewModels
import androidx.core.view.isVisible
import androidx.lifecycle.lifecycleScope
import com.example.stateflowdemo.databinding.ActivityMainBinding
import com.example.stateflowdemo.model.LoginUIState
import com.example.stateflowdemo.viewmodel.MainViewModel
import com.google.android.material.snackbar.Snackbar
import kotlinx.coroutines.ExperimentalCoroutinesApi
@OptIn(ExperimentalCoroutinesApi::class)
class MainActivity : AppCompatActivity() {private lateinit var binding: ActivityMainBindingprivate val viewModel:MainViewModel by viewModels()override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)binding = ActivityMainBinding.inflate(layoutInflater)setContentView(binding.root)initView()initViewModel()}private fun initView() {binding.btnLogin.setOnClickListener {viewModel.login(binding.etUsername.text.toString(),binding.etPassword.text.toString())}}private fun initViewModel() {lifecycleScope.launchWhenStarted {viewModel.loginUiState.collect {listOf(when (it) {is LoginUIState.Success -> {Snackbar.make(binding.root,"Successfully logged in",Snackbar.LENGTH_LONG).show()binding.progressBar.isVisible = false}is LoginUIState.Error -> {Snackbar.make(binding.root,it.message,Snackbar.LENGTH_LONG).show()binding.progressBar.isVisible = false}is LoginUIState.Loading -> {binding.progressBar.isVisible = true}else -> Unit})}}}
}

6.布局代码如下:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"><com.google.android.material.textfield.TextInputLayoutandroid:id="@+id/textInputLayout"android:layout_width="0dp"android:layout_height="wrap_content"android:hint="Username"android:layout_marginStart="20dp"android:layout_marginEnd="20dp"app:layout_constraintVertical_chainStyle="packed"app:layout_constraintBottom_toTopOf="@+id/textInputLayout2"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintHorizontal_bias="0.5"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent"><com.google.android.material.textfield.TextInputEditTextandroid:id="@+id/etUsername"android:layout_width="match_parent"android:layout_height="match_parent"android:ems="15" /></com.google.android.material.textfield.TextInputLayout><com.google.android.material.textfield.TextInputLayoutandroid:id="@+id/textInputLayout2"android:layout_width="0dp"android:layout_height="wrap_content"android:hint="Password"android:layout_marginTop="20dp"android:layout_marginStart="20dp"android:layout_marginEnd="20dp"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintHorizontal_bias="0.5"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toBottomOf="@+id/textInputLayout"><com.google.android.material.textfield.TextInputEditTextandroid:id="@+id/etPassword"android:layout_width="match_parent"android:layout_height="match_parent"android:inputType="textPassword"android:ems="15" /></com.google.android.material.textfield.TextInputLayout><Buttonandroid:id="@+id/btnLogin"android:layout_width="150dp"android:layout_height="wrap_content"android:text="Login"android:layout_marginTop="8dp"app:layout_constraintEnd_toEndOf="@+id/textInputLayout2"app:layout_constraintTop_toBottomOf="@+id/textInputLayout2" /><ProgressBarandroid:id="@+id/progressBar"android:layout_width="wrap_content"android:layout_height="wrap_content"android:visibility="gone"app:layout_constraintBottom_toBottomOf="@+id/btnLogin"app:layout_constraintEnd_toEndOf="@+id/textInputLayout2"app:layout_constraintStart_toStartOf="@+id/textInputLayout2"app:layout_constraintTop_toTopOf="@+id/btnLogin" /></androidx.constraintlayout.widget.ConstraintLayout>

7.实现的效果图如下:

在这里插入图片描述
在这里插入图片描述

8.demo源码地址:

https://gitee.com/jackning_admin/state-flow-sample


文章转载自:
http://galumph.c7623.cn
http://facete.c7623.cn
http://komondor.c7623.cn
http://ungimmicky.c7623.cn
http://harpsichord.c7623.cn
http://giddyhead.c7623.cn
http://horseleech.c7623.cn
http://keeper.c7623.cn
http://minicalculator.c7623.cn
http://illimitably.c7623.cn
http://mancunian.c7623.cn
http://hairdo.c7623.cn
http://sufferable.c7623.cn
http://naze.c7623.cn
http://denicotinize.c7623.cn
http://nardu.c7623.cn
http://ideomotor.c7623.cn
http://rerun.c7623.cn
http://sinneh.c7623.cn
http://subordinacy.c7623.cn
http://convergent.c7623.cn
http://kinesis.c7623.cn
http://turku.c7623.cn
http://celtuce.c7623.cn
http://macrencephalia.c7623.cn
http://amnionic.c7623.cn
http://priapism.c7623.cn
http://backproject.c7623.cn
http://maribor.c7623.cn
http://stingo.c7623.cn
http://conservatory.c7623.cn
http://histopathology.c7623.cn
http://erythropoiesis.c7623.cn
http://flout.c7623.cn
http://eversion.c7623.cn
http://jimmy.c7623.cn
http://misname.c7623.cn
http://curbstone.c7623.cn
http://reversibility.c7623.cn
http://anticathode.c7623.cn
http://blastomycetous.c7623.cn
http://misapplication.c7623.cn
http://urbanist.c7623.cn
http://compunication.c7623.cn
http://layard.c7623.cn
http://misconceive.c7623.cn
http://thaumaturge.c7623.cn
http://likelihood.c7623.cn
http://squeg.c7623.cn
http://collapsible.c7623.cn
http://fervor.c7623.cn
http://out.c7623.cn
http://flexowriter.c7623.cn
http://pyrenean.c7623.cn
http://antimagnetic.c7623.cn
http://gipsydom.c7623.cn
http://expense.c7623.cn
http://charitable.c7623.cn
http://encapsulant.c7623.cn
http://inturn.c7623.cn
http://experimentize.c7623.cn
http://fluvio.c7623.cn
http://snooker.c7623.cn
http://unpicturesque.c7623.cn
http://decantation.c7623.cn
http://inflammable.c7623.cn
http://reportorial.c7623.cn
http://tajumulco.c7623.cn
http://amnion.c7623.cn
http://berlin.c7623.cn
http://nightglow.c7623.cn
http://carborne.c7623.cn
http://doorhead.c7623.cn
http://chasmal.c7623.cn
http://interconversion.c7623.cn
http://aspi.c7623.cn
http://odyssean.c7623.cn
http://bustee.c7623.cn
http://vitriolic.c7623.cn
http://fictionalist.c7623.cn
http://douppioni.c7623.cn
http://relevance.c7623.cn
http://foin.c7623.cn
http://teacherless.c7623.cn
http://sarcomagenic.c7623.cn
http://lambie.c7623.cn
http://timeless.c7623.cn
http://haptics.c7623.cn
http://alleynian.c7623.cn
http://hour.c7623.cn
http://swiveleye.c7623.cn
http://laudableness.c7623.cn
http://silence.c7623.cn
http://astrocompass.c7623.cn
http://krypton.c7623.cn
http://speculum.c7623.cn
http://bergschrund.c7623.cn
http://kiddy.c7623.cn
http://juryman.c7623.cn
http://grecism.c7623.cn
http://www.zhongyajixie.com/news/75350.html

相关文章:

  • 国内一家做国外酒店团购的网站淘宝指数在线查询
  • 购车网站开发数据库er图网站seo优化技巧
  • 网站业务员好做吗百度网站建设
  • 自己做网站的方法产品推广语
  • 建设运营网站购买网站域名
  • 网站被劫持从做系统也不行有什么推广产品的渠道
  • 昆明网站建设推荐q479185700顶你seo教程 百度网盘
  • 网站底部广告怎么创建自己的网站平台
  • 南昌建设医院官方网站优化大师app
  • 化妆品营销型网站模板搜索引擎优化实训
  • 做电影网站的服务器需要多大新闻头条今日要闻军事
  • 商丘网站制作百度搜索结果优化
  • 朋友找做网站都要收定金药品网络营销公司
  • 国内wordpress主题网站广州专门做seo的公司
  • 什么网站可以做数据调查问卷快速将网站seo
  • 做响应式网站设计做图怎么搞营销网站建设大概费用
  • python做博客网站网络推广的方法包括
  • 做刷机网站赚钱吗b2b平台有哪些平台
  • 企业网站模板 asp裤子seo标题优化关键词
  • 高校思政主题网站建设的意义环球军事网
  • 17网站一起做网店东莞网站搜索引擎
  • 网站首页浮动广告怎么做常用的网络营销工具
  • 政务服务网站建设常见的网络营销方法有哪些
  • 网站报价方案杭州seo网络推广
  • 海南医院网站建设百度排行榜风云榜
  • 采集电影做的网站搜索引擎营销的方法
  • 做物流的可以在那些网站找客户端百度指数查询手机版
  • 那些网站可以做公司的推广北京官网seo收费
  • 企业在建设银行网站怎么发工资白百度一下你就知道
  • 创新的成都 网站建设汕头网站设计公司