为什么 Activity 类中的生命周期作用域没有响应?

问题描述 投票:0回答:1

当我点击负责类别的类别RecycleView(categoryRecyclerView)中的某个项目时,分类代码发生变化。 之后的目标是从goodsRecyclerView相应地重新生成recyclerView。

第一次运行它时,它被完全调用,但当你更改类别时它不会反映。

在ViewModel中,_selectSubDispClasSeq是类别分类代码,我想在_goods中反映它。


下面是我的代码。

@AndroidEntryPoint 类 DetailCategoryActivity : BaseActivity({ ActivityDetailCategoryBinding.inflate(it)}) {

private val viewModel: DetailCategoryViewModel by viewModels()

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(binding.root)

    val dispClasSeq = intent.getIntExtra("dispClasSeq", 0)

    val categoryRecyclerView = binding.rvSublistCategory
    val categoryLayoutManager = GridLayoutManager(this, 3)

    val categoryAdapter = SubListAdapter(emptyList()) { selectedDispClasItem ->
        viewModel.onSubItemClicked(selectedDispClasItem)
    }

    categoryRecyclerView.adapter = categoryAdapter
    categoryRecyclerView.layoutManager = categoryLayoutManager


    val goodsRecyclerView = binding.rvGoods
    val goodsLayoutManager = GridLayoutManager(this, 2)

    val goodsAdapter = GoodsAdapter()
    goodsRecyclerView.adapter = goodsAdapter
    goodsRecyclerView.layoutManager = goodsLayoutManager

    lifecycleScope.launch {
        viewModel.subDispClasInfos.collect { subDispClasInfos ->
            viewModel.loadSubDispClasInfos(dispClasSeq)
            categoryAdapter.submitList(subDispClasInfos)
        }
    }

    lifecycleScope.launch {
        viewModel.goods.collect { goods ->
            Log.d("goods", goods.toString())
            val subDsipClasSeq = viewModel.selectedSubDispClasSeq.value
            viewModel.loadGoodsInfos(dispClasSeq, subDsipClasSeq, "추천순")
            goodsAdapter.submitData(goods)
        }
    }

}

}

@HiltViewModel

类 DetailCategoryViewModel @Inject 构造函数( 私有 valDetailItemRepository:DetailItemRepository, 私人 val GoodsItemRepository:GoodsItemRepository ) : ViewModel() {

private val _subDispClasInfos = MutableStateFlow<List<DetailCategoryItem>>(emptyList())
val subDispClasInfos: StateFlow<List<DetailCategoryItem>> get() = _subDispClasInfos

private val _goods: MutableStateFlow<PagingData<Goods>> = MutableStateFlow(PagingData.empty())
val goods: StateFlow<PagingData<Goods>> get() = _goods

private val _selectedSubDispClasSeq = MutableStateFlow(0)
val selectedSubDispClasSeq: StateFlow<Int> get() = _selectedSubDispClasSeq

fun loadSubDispClasInfos(dispClasSeq: Int) {
    viewModelScope.launch {
        try {
            detailItemRepository.getCategorySublistItemsFlow(dispClasSeq).collect { result ->
                _subDispClasInfos.emit(result)
            }
        } catch (e: Exception) {
            Log.d("result", e.toString())
        }
    }
}

fun loadGoodsInfos(prntsDispClasSeq: Int, subDispClasSeq: Int, searchValue: String) {
    viewModelScope.launch {
        try {
            Log.d("loadGoodsInfos1", subDispClasSeq.toString())
            val result = goodsItemRepository.getGoodsItemsFlow(prntsDispClasSeq, subDispClasSeq, searchValue)
                .first()
            _goods.emit(result)
        } catch (e: Exception) {
            Log.d("result", e.toString())
        }
    }
}

fun onSubItemClicked(item: DetailCategoryItem) {
    viewModelScope.launch {
        _selectedSubDispClasSeq.emit(item.dispClasSeq)
        loadGoodsInfos(item.prntsDispClasSeq, selectedSubDispClasSeq.value, "추천순")
    }
}

}

当我输出日志时,确认了loadGoodsInfos函数中_selectedSubDispClasSeq的值变化正常。但我在活动课上观察不到。

lifecycleScope.launch {
        viewModel.goods.collect { goods ->
            Log.d("goods", goods.toString())
            val subDsipClasSeq = viewModel.selectedSubDispClasSeq.value
            viewModel.loadGoodsInfos(dispClasSeq, subDsipClasSeq, "추천순")
            goodsAdapter.submitData(goods)
        }
    }
android kotlin kotlin-coroutines coroutine
1个回答
0
投票

看起来问题可能与以下事实有关:您正在收集

goods
块内的
lifecycleScope.launch
流,但您没有在同一协程范围内观察到
_selectedSubDispClasSeq
的更改。因此,对
_selectedSubDispClasSeq
的更改可能不会反映在
goods
流集合中。

要解决此问题,您可以尝试使用

combine
包中的
kotlinx.coroutines.flow
运算符组合两个流。
combine
运算符允许您观察多个流并对其中任何一个流的变化做出反应。以下是修改代码的方法:

lifecycleScope.launch {
    combine(viewModel.selectedSubDispClasSeq, viewModel.goods) { subDispClasSeq, goods ->
        Log.d("goods", goods.toString())
        viewModel.loadGoodsInfos(dispClasSeq, subDispClasSeq, "추천순")
        goodsAdapter.submitData(goods)
    }.collect()
}

确保导入 kotlinx.coroutines.flow.combine 函数:

import kotlinx.coroutines.flow.combine

这应该有助于确保在更新

_selectedSubDispClasSeq
流程时正确观察到
goods
中的变化。

© www.soinside.com 2019 - 2024. All rights reserved.