Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/213.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android-Recyclerview中onBindViewHolder中的OnResume_Android_Android Recyclerview - Fatal编程技术网

Android-Recyclerview中onBindViewHolder中的OnResume

Android-Recyclerview中onBindViewHolder中的OnResume,android,android-recyclerview,Android,Android Recyclerview,我有一个RecyclerView,它列出了onBindViewHolder中每个文章/项目中的文章,我正在为评论加载另一个RecyclerView,当用户返回活动时,我需要更新它们 是否有一种方法可以检测父RecyclerView的OnResume内部onBindViewHolder的活动?我假设您有一个包含文章详细信息和评论的数据类。您将此列表传递给第一个回收器,该回收器在onBind中使用数据填充注释回收器。如果您必须更新注释,只需将带有更新注释的文章列表传递给文章的适配器。它将触发onBi

我有一个RecyclerView,它列出了
onBindViewHolder
中每个文章/项目中的文章,我正在为评论加载另一个RecyclerView,当用户返回活动时,我需要更新它们


是否有一种方法可以检测父RecyclerView的
OnResume
内部
onBindViewHolder
的活动?

我假设您有一个包含文章详细信息和评论的数据类。您将此列表传递给第一个回收器,该回收器在onBind中使用数据填充注释回收器。如果您必须更新注释,只需将带有更新注释的文章列表传递给文章的适配器。它将触发
onBindViewHolder
,在其中您将有新的注释。在代码中是这样的:

data class Article (
    val name: String
    ... other details
    val comments: List<Comment>
)
在活动中:

override fun onResume() {
    super.onResume()

    //get new comments here and set it for new articles
    val newArticles = oldArticles.map { article -> it.copy(comments = newComments)}
    articlesAdapter.submitList(newArticles)
}

顺便说一下,AdapterDelegates似乎是解决您问题的更好方法。阅读更多信息:

onResume是一种生命周期方法,onBindViewHolder与recyclerview相关,这两者并没有真正相遇:)您希望做的是更新内部的适配器onResume@a_local_nobody但我无法从活动的OnResume更新子recyclerview适配器,因为似乎无法跟踪用户当前正在查看的父recyclerview的当前项。我想如果没有大的解决办法就没有解决办法了你是说更新parent recyclerview上的文章?如果是,那么这不是我需要的,这样我会触发API再次获取注释。如果不是这样,但仍然需要有整个新的注释列表,那么我没有它^^我只有一个已加载的特定注释id的更新信息,并且我需要更新。如果我没有发现问题,您应该在viewmodel中保留状态,如果只更新了一条注释,然后在您的状态中只更新一个,即:
val newComment=artciles.findOrNull{predicate}?.comments.findOrNull{it.id==idFetchedFromApi}?.copy(根据需要在此处更新)
,然后以相同的方式更新文章的注释。好的,我知道了,将注释放在文章列表中,以便在再次加载onBindViewHolder并且有注释列表时跳过API
override fun onResume() {
    super.onResume()

    //get new comments here and set it for new articles
    val newArticles = oldArticles.map { article -> it.copy(comments = newComments)}
    articlesAdapter.submitList(newArticles)
}