Android的MVVM设计模式

2025-01-16

Model-View-ViewModel,与苹果的MVC模式基本一致。

怎么做?

创建一个ViewModel的Class文件

继承ViewModel()

注意:只改变private变量,即_count,将count暴露出去

class CounterViewModel: ViewModel() {
    private val _count = mutableStateOf(0)

    //Expose the count as an immutable state
    val count: MutableState<Int> = _count

    fun increment(){
        _count.value++
    }

    fun decrement(){
        _count.value--
    }
}

引用ViewModel的变量

MainActivity里面引用变量

setContent里面声明viewModel

setContent {
    val viewModel: CounterViewModel by viewModels()

    CounterViewModelTheme {
        Surface (modifier = Modifier.fillMaxSize()) {
            TheCounterApp(viewModel)
        }
    }
}

在UI界面中使用

viewModel.count.value的形式获取变量。

@Composable
fun TheCounterApp(viewModel: CounterViewModel){

    Column(
        modifier = Modifier.fillMaxSize(),
        verticalArrangement = Arrangement.Center,
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        Text(
            text = "Count: ${viewModel.count.value}",
            fontSize = 24.sp,
            fontWeight = FontWeight.Bold
        )
        Spacer(modifier = Modifier.height(16.dp))
        Row{
            Button(onClick = {viewModel.increment()}) {
                Text("Increment")
            }
            Button(onClick = {viewModel.decrement()}) {
                Text("Decrement")
            }
        }

    }
}

Model的部分

创造一个class文件CounterModel

数据是一个data class

data class CounterModel (
    var count: Int
)

创建一个Repository类

class CounterRepository(){
    private var _counter = CounterModel(0)

    fun getCounter() = _counter

    fun incrementCounter(){
        _counter.count++
    }

    fun decrementCounter(){
        _counter.count--
    }
}

viewModel中使用

class CounterViewModel(): ViewModel() {
    private val _repository: CounterRepository = CounterRepository()
    private val _count = mutableStateOf(_repository.getCounter().count)

    //Expose the count as an immutable state
    val count: MutableState<Int> = _count

    fun increment(){
        _repository.incrementCounter()
        _count.value = _repository.getCounter().count
    }

    fun decrement(){
        _count.value--
        _repository.decrementCounter()
        _count.value = _repository.getCounter().count
    }
}