Android学习笔记19

今天建立了dialog的viewmodel去实作原本的动作

class Dvm(application: Application): AndroidViewModel(application) {

    val account = MutableLiveData<String>()
    //若要监听输入值时使用
    val progressbar = View.VISIBLE
    val progress = MutableLiveData<Int>()

    private val myScope = object : CoroutineScope {
        override val coroutineContext: CoroutineContext
            get() = job
    }
    private val job = Job()

    fun coroutine() {
        myScope.launch(Dispatchers.Main) {
            showprogress()
        }
    }

    private suspend fun showprogress() = withContext(Dispatchers.IO) {
        for (i in 0..100){
            progress.postValue(i)
            SystemClock.sleep(500)
        }
    }
}

那原本我在fragment中我适用Thread去跑的那如果在viewmodel中记得要使用coroutine
原本的fragment就变成这样

class FragmentDialog: DialogFragment() {

    private val viewModel: DialogViewModel<MainDialogResult> by activityViewModels()

    private val mviewModel by lazy {
        initViewModel(Application(), Dvm::class.java)
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val binding = DialogfragmentBinding.inflate(inflater,container,false)
        binding.lifecycleOwner = this
        binding.viewmodel = mviewModel
        binding.login.setOnClickListener {
            viewModel.result.postValue(MainDialogResult.Ok(binding.account.text.toString() , binding.password.text.toString()))
            dismiss()
        }

        binding.cancel.setOnClickListener {
            viewModel.result.postValue(MainDialogResult.Cancel)
            dismiss()
        }

        mviewModel.coroutine()

        return binding.root
    }
    
}

是不是简洁了不少,虽然我也想把onclickk写在viewmodel但是因为他的资料值是跟activity连结的,所以想过去虽然不会报错,但就无法转移资料
最後是xml

<?xml version="1.0" encoding="utf-8"?>
<layout
    xmlns:android="http://schemas.android.com/apk/res/android">
   <data>
      <variable
          name="viewmodel"
          type="com.example.myapplication.Dvm" />

      <variable
          name="data"
          type="com.example.myapplication.ViewModel" />
   </data>
   <LinearLayout
       android:orientation="vertical"
       android:layout_height="wrap_content"
       android:layout_width="match_parent">
      <EditText
          android:id="@+id/account"
          android:text="@={viewmodel.account}"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:hint="account"/>
      <EditText
          android:id="@+id/password"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:hint="password"/>
      <ProgressBar
          android:visibility="@{viewmodel.progressbar}"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          style="?android:attr/progressBarStyle"
          android:layout_gravity="center" />
      <ProgressBar
          android:max="100"
          android:progress="@{viewmodel.progress}"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          style="?android:attr/progressBarStyleHorizontal"
          android:layout_gravity="center"
          android:id="@+id/progressBar2" />
      <LinearLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:orientation="horizontal">
         <Button
             android:id="@+id/login"
             android:layout_width="0dp"
             android:layout_height="wrap_content"
             android:layout_weight="0.5"
             android:text="login"/>
         <Button
             android:id="@+id/cancel"
             android:layout_width="0dp"
             android:layout_height="wrap_content"
             android:layout_weight="0.5"
             android:text="cancel"/>

      </LinearLayout>
   </LinearLayout>

</layout>

<<:  Day13-Webhook 实作(二)LINEBot 之 Echo bot

>>:  Day 13 Flask 与 Tensorflow Serving 的沟通

33岁转职者的前端笔记-DAY 13 图片格式及影音格式

图片 网页上一定会有多许多图片元素我们在开发网站时需多了解图片的特性及格式,在开发的过程会更加顺利。...

[Day9] Android : Kotlin笔记:JetPack - Fragment KTX

Fragment KTX 首先要在app的build.gradle加入: dependencies ...

Day-13 Pytorch Tensors

程序语言会有一些常见的资料组单位,例如 python 会有 list,C、C++ 有 array ...

[Day 3] SRE - Log写好一点,对团队好一些

LogSeverity 有在写Log的人都知道Log需要被分级,而分级对於问题的除错,是很重要的,当...

[DAY8]制作容器(七)

会发生css路径的问题可能是因为override的部分没有设定好,所以再重作一个container ...