parent
6fef3d8097
commit
cf2c48cc9e
@ -1,33 +0,0 @@ |
||||
package com.gyf.csams.ui.model |
||||
|
||||
import androidx.lifecycle.LiveData |
||||
import androidx.lifecycle.MutableLiveData |
||||
import androidx.lifecycle.ViewModel |
||||
import androidx.lifecycle.viewModelScope |
||||
import kotlinx.coroutines.Job |
||||
import kotlinx.coroutines.delay |
||||
import kotlinx.coroutines.launch |
||||
|
||||
|
||||
class MainViewModel:ViewModel() { |
||||
val poetry= listOf("床前明月光","疑是地上霜","举头望明月","低头思故乡") |
||||
|
||||
private val _poetryIndex=MutableLiveData(0) |
||||
var poetryIndex:LiveData<Int> = _poetryIndex |
||||
var job:Job? = null |
||||
|
||||
|
||||
fun addAsync(delayMillis:Int){ |
||||
if(job == null || job?.isCompleted==true) { |
||||
job = viewModelScope.launch { |
||||
_poetryIndex.postValue( |
||||
if (_poetryIndex.value == poetry.size-1) 0 else _poetryIndex.value?.plus( |
||||
1 |
||||
) |
||||
) |
||||
delay(timeMillis = delayMillis.toLong()) |
||||
} |
||||
} |
||||
|
||||
} |
||||
} |
@ -0,0 +1,72 @@ |
||||
package com.gyf.csams.ui.model |
||||
|
||||
import androidx.lifecycle.LiveData |
||||
import androidx.lifecycle.MutableLiveData |
||||
import androidx.lifecycle.ViewModel |
||||
import androidx.lifecycle.viewModelScope |
||||
import com.gyf.csams.R |
||||
import kotlinx.coroutines.Job |
||||
import kotlinx.coroutines.delay |
||||
import kotlinx.coroutines.launch |
||||
|
||||
/** |
||||
* 跑马灯 |
||||
* |
||||
*/ |
||||
class MarqueeViewModel:ViewModel() { |
||||
|
||||
val marqueeTexts= listOf("床前明月光","疑是地上霜","举头望明月","低头思故乡") |
||||
|
||||
private val _marqueeIndex=MutableLiveData(0) |
||||
var marqueeIndex:LiveData<Int> = _marqueeIndex |
||||
var marqueeJob:Job? = null |
||||
|
||||
|
||||
|
||||
fun addAsync(delayMillis:Int){ |
||||
if(marqueeJob == null || marqueeJob?.isCompleted==true) { |
||||
marqueeJob = viewModelScope.launch { |
||||
_marqueeIndex.postValue( |
||||
if (_marqueeIndex.value == marqueeTexts.size-1) 0 else _marqueeIndex.value?.plus( |
||||
1 |
||||
) |
||||
) |
||||
delay(timeMillis = delayMillis.toLong()) |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
class CarouselViewModel:ViewModel(){ |
||||
val imageList= listOf(R.drawable.ic_launcher_foreground,R.drawable.ic_account_fill,R.drawable.ic_all_fill,R.drawable.ic_home_fill) |
||||
|
||||
private val _index=MutableLiveData(0) |
||||
|
||||
val index:LiveData<Int> = _index |
||||
|
||||
var job:Job? = null |
||||
|
||||
init { |
||||
start() |
||||
} |
||||
|
||||
fun start(){ |
||||
job = viewModelScope.launch { |
||||
do{ |
||||
_index.postValue(if (_index.value==imageList.size-1) 0 else _index.value?.plus(1)) |
||||
println("值更新为 :$_index") |
||||
delay(5000) |
||||
}while (job?.isActive==true) |
||||
} |
||||
} |
||||
|
||||
fun stop(){ |
||||
println("停止更新") |
||||
job?.cancel() |
||||
} |
||||
|
||||
} |
||||
|
||||
class MainViewModel:ViewModel(){ |
||||
|
||||
} |
Loading…
Reference in new issue