嘿,我正在学习android kotlin 中的状态流。我正在reyclerview的帮助下创建反向对话日历视图。在我mainactivity
里面有一个fragment
,在里面我有reyclerview
。我的目标是在我的recyclerview中进行分页,所以我首先加载几个月,而不是在这个答案的帮助下在我的 reyclerview 中添加越来越多的数据。我成功地实现了它。但问题是当我达到阈值并触发新数据时。我的整个列表被引用并且它没有被更新它删除了以前的数据。我不明白为什么会在MutableStateFlow中发生这种情况,而且我是这个流程的新手. 我的Github项目链接在这里。请指导我在这里做错了什么。我的目标是预先添加 stateFlow 数据。谢谢
对话片段.kt
class ConversationFragment(val customManager: CustomManager) : Fragment() {
companion object {
private const val DATA = "data"
fun create(
data: List<ConversationDate>,
customManager: CustomManager
): ConversationFragment {
val fragment = ConversationFragment(customManager)
val bundle = Bundle()
bundle.putParcelableArrayList(
DATA,
ArrayList<Parcelable>(data)
)
fragment.arguments = bundle
return fragment
}
}
private var _binding: ConversationFragmentLayoutBinding? = null
private val binding get() = _binding!!
private val visibleThreshold = 7
private var previousTotalItemCount = 0
private var loading = true
private val weeklyAdapter = ConversationWeeklyAdapter()
private val viewModel by viewModels<FragmentViewModel>()
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
setupViewModel()
_binding = ConversationFragmentLayoutBinding.inflate(inflater, container, false)
setupView()
return binding.root
}
private fun setupViewModel() {
viewModel.data = arguments?.getParcelableArrayList(DATA)
viewModel.startDate = customManager.startDate()
viewModel.endDate = customManager.endDate()
}
private fun setupView() {
viewModel.weeklyFormatData()
activity?.lifecycleScope?.launchWhenCreated {
activity?.repeatOnLifecycle(Lifecycle.State.CREATED) {
viewModel.prepareMutableStateFlow.collectLatest {
weeklyAdapter.submitList(it)
}
}
}
binding.conversationView.apply {
adapter = weeklyAdapter
layoutManager = LinearLayoutManager(context).apply {
orientation = LinearLayoutManager.HORIZONTAL
stackFromEnd = true
}
addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)
val firstVisibleItemPosition =
(recyclerView.layoutManager as LinearLayoutManager).findFirstVisibleItemPosition()
val totalItemCount = recyclerView.adapter?.itemCount
if (totalItemCount != null) {
if (totalItemCount < previousTotalItemCount) {
previousTotalItemCount = totalItemCount
if (totalItemCount == 0) {
loading = true
}
}
}
if (totalItemCount != null) {
if (loading && (totalItemCount > previousTotalItemCount)) {
loading = false
previousTotalItemCount = totalItemCount
}
}
if (!loading && firstVisibleItemPosition < visibleThreshold) {
customManager.loadMoreData()
loading = true
}
}
})
}
}
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
fun loadMoreData(item: List<ConversationDate>) {
viewModel.startDate =
SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).parse("2021-11-01")
viewModel.endDate =
SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).parse("2021-11-30")
val previousData = viewModel.data
if (previousData != null) {
viewModel.data = previousData + item
}
viewModel.weeklyFormatData()
}
}
片段视图模型.kt
class FragmentViewModel(app: Application) : AndroidViewModel(app) {
var data: List<ConversationDate>? = null
var startDate: Date? = null
var endDate: Date? = null
private val dateRange: MutableList<Date>
get() {
val datesInRange = mutableListOf<Date>()
val tempCalendar = Calendar.getInstance()
tempCalendar.time = startDate as Date
while (tempCalendar.time <= endDate) {
datesInRange.add(tempCalendar.time)
tempCalendar.add(Calendar.DATE, 1)
}
return datesInRange
}
val prepareMutableStateFlow: MutableStateFlow<List<ConversationCount>> =
MutableStateFlow(emptyList())
fun weeklyFormatData() {
val conversationCount = mutableListOf<ConversationCount>()
viewModelScope.launch {
dateRange.forEachIndexed { index, dateRangeValue ->
val findData = data?.find {
dateRangeValue == it.dateObject
}
conversationCount.add(
ConversationCount(
setDay(dateRangeValue),
index,
findData != null
)
)
}
prepareMutableStateFlow.value = conversationCount
}
}
private fun setDay(date: Date): String? {
return SimpleDateFormat("dd", Locale.getDefault()).format(date)
}
}
我的模拟 api 响应链接
问题描述
加载应用程序时,将出现片段屏幕并在其上显示一些日期。我将startDate设置为2021-12-01并将endDate设置为今天的日期,即2022-01-13
。因此,当开始滚动并达到加载更多数据的阈值时。我通过了startDate作为2021-11-01和endDate作为2021-11-30。它已添加到列表中,但之前的数据已被删除。我不明白为什么会这样。我附上我的 YouTube链接