每当 StateFlow 更新和发出数据时,ListAdapter 都不会收到通知
ListAdapter is not being notified whenever data is updated and emitted by StateFlow
StateFlow
在更改后发出新数据,但 ListAdapter
不是 updated/notified,但是当配置更改时(即设备从纵向模式旋转到横向模式)更新是发生:
class TutorialListFragment : Fragment() {
private lateinit var binding: FragmentTutorialListBinding
private val viewModel: ITutorialViewModel by viewModels<TutorialViewModelImpl>()
private lateinit var adapter: TutorialAdapter
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
binding = FragmentTutorialListBinding.inflate(inflater, container, false)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val recyclerView = binding.recyclerView
adapter = TutorialAdapter()
recyclerView.adapter = adapter
loadData()
}
private fun loadData() {
viewModel
.getTutorialList()
val tutorialList: MutableList<TutorialResponse> = mutableListOf()
viewModel
.tutorialListStateFlow
.onEach { list ->
list.forEach {tutorialResponse->
tutorialList.add(tutorialResponse)
Log.e("TUTORIAL_LIST_FRAG", "$tutorialResponse")
}
adapter.submitList(tutorialList)
}.launchIn(viewLifecycleOwner.lifecycleScope)
}
}
视图模型是:
class TutorialViewModelImpl: ViewModel(), ITutorialViewModel {
private val mTutorialRepository: ITutorialRepository = TutorialRepositoryImpl()
private val _tutorialListStateFlow = MutableStateFlow<List<TutorialResponse>>(mutableListOf())
override val tutorialListStateFlow: StateFlow<List<TutorialResponse>>
get() = _tutorialListStateFlow.asStateFlow()
init {
mTutorialRepository
.getTutorialListSuccessListener {
viewModelScope
.launch {
_tutorialListStateFlow.emit(it)
Log.e("TUTORIAL_GL_VM", "$it")
}
}
}
override fun getTutorialList() {
// Get list
mTutorialRepository.getTutorialList()
}
}
当我查看 Logcat
时,我看到了这一行:
Log.e("TUTORIAL_GL_VM", "$it")
打印所有更改,但 ListAdapter
中没有更新。
我假设你来自 mTutorialRepository
的数据不是一个流,所以你必须添加 .toList()
如果你想在 stateFlow
中发出列表以获得通知
mTutorialRepository.getTutorialListSuccessListener {
viewModelScope.launch {
// here add .toList()
_tutorialListStateFlow.emit(it.toList())
}
}
或者如果它仍然不起作用,请尝试像这样更改您的 loadData()
private fun loadData() {
// idk what are doing with this ??
viewModel.getTutorialList()
lifecycleScope.launch {
viewModel.tutorialListStateFlow.collect { list ->
adapter.submitList(list)
}
}
}
StateFlow
在更改后发出新数据,但 ListAdapter
不是 updated/notified,但是当配置更改时(即设备从纵向模式旋转到横向模式)更新是发生:
class TutorialListFragment : Fragment() {
private lateinit var binding: FragmentTutorialListBinding
private val viewModel: ITutorialViewModel by viewModels<TutorialViewModelImpl>()
private lateinit var adapter: TutorialAdapter
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
binding = FragmentTutorialListBinding.inflate(inflater, container, false)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val recyclerView = binding.recyclerView
adapter = TutorialAdapter()
recyclerView.adapter = adapter
loadData()
}
private fun loadData() {
viewModel
.getTutorialList()
val tutorialList: MutableList<TutorialResponse> = mutableListOf()
viewModel
.tutorialListStateFlow
.onEach { list ->
list.forEach {tutorialResponse->
tutorialList.add(tutorialResponse)
Log.e("TUTORIAL_LIST_FRAG", "$tutorialResponse")
}
adapter.submitList(tutorialList)
}.launchIn(viewLifecycleOwner.lifecycleScope)
}
}
视图模型是:
class TutorialViewModelImpl: ViewModel(), ITutorialViewModel {
private val mTutorialRepository: ITutorialRepository = TutorialRepositoryImpl()
private val _tutorialListStateFlow = MutableStateFlow<List<TutorialResponse>>(mutableListOf())
override val tutorialListStateFlow: StateFlow<List<TutorialResponse>>
get() = _tutorialListStateFlow.asStateFlow()
init {
mTutorialRepository
.getTutorialListSuccessListener {
viewModelScope
.launch {
_tutorialListStateFlow.emit(it)
Log.e("TUTORIAL_GL_VM", "$it")
}
}
}
override fun getTutorialList() {
// Get list
mTutorialRepository.getTutorialList()
}
}
当我查看 Logcat
时,我看到了这一行:
Log.e("TUTORIAL_GL_VM", "$it")
打印所有更改,但 ListAdapter
中没有更新。
我假设你来自 mTutorialRepository
的数据不是一个流,所以你必须添加 .toList()
如果你想在 stateFlow
中发出列表以获得通知
mTutorialRepository.getTutorialListSuccessListener {
viewModelScope.launch {
// here add .toList()
_tutorialListStateFlow.emit(it.toList())
}
}
或者如果它仍然不起作用,请尝试像这样更改您的 loadData()
private fun loadData() {
// idk what are doing with this ??
viewModel.getTutorialList()
lifecycleScope.launch {
viewModel.tutorialListStateFlow.collect { list ->
adapter.submitList(list)
}
}
}