[英]How to update recyclerView list due to variable value kotlin?
I'm doing questionnaire which is based on RV with its own adapter.我正在做基于带有自己适配器的 RV 的问卷调查。 My answers I send to server api.我的答案发送到服务器 api。 I would like to enable answers options in the list when I receive 200 response.当我收到 200 响应时,我想在列表中启用答案选项。 So I added observed variable in adapter:所以我在适配器中添加了观察变量:
var foo: Boolean by Delegates.observable(false) { property, oldValue, newValue ->
if (newValue){
Handler().postDelayed({
notifyItemChanged(0,9)
}, 1000)
}
}
and change it when I receive 200 from server:并在我从服务器收到 200 时更改它:
AnswersAdapter().foo = true
then at onBindViewHolder I try to enable items:然后在 onBindViewHolder 我尝试启用项目:
override fun onBindViewHolder(holder: AnswerHolder, position: Int, payloads: MutableList<Any>) {
if (payloads.isNotEmpty()) {
when (payloads[0]) {
...
9 -> {
itemEnabler(Constants.ENABLE_CHECKBOX_ITEM)
}
}
} else {
super.onBindViewHolder(holder, position, payloads)
}
}
and finally function for enabling:最后是 function 用于启用:
fun itemEnabler(type: Int) {
when (type) {
Constants.ENABLE_CHECKBOX_ITEM -> {
for (i in 0 until itemCount) {
if (i != checkedPosition) {
Timber.i("=--==-=-=-=-")
notifyItemChanged(i, Constants.ENABLE_CHECKBOX_ITEM)
}
}
}
....
}
}
where checkedPosition
is equal to selected answer.其中checkedPosition
等于选择的答案。 But my items can't be enabled in this way:( As I see my function also isn't called from foo variable. What I did wrong and how to fix it?但是我的项目不能以这种方式启用:(我看到我的 function 也没有从 foo 变量中调用。我做错了什么以及如何解决它?
itemEnabler
function is called but items as a result aren't enabled at all itemEnabler
function 被调用,但项目因此根本没有启用
Looks like you recreate adapter in line AnswersAdapter().foo = true
You should use something like this code:看起来您在AnswersAdapter().foo = true
行中重新创建了适配器您应该使用类似以下代码的内容:
class Fragment {
private val answersAdapter = AnswersAdapter()
override fun onCreate(savedInstanceState: Bundle?) {
recycler.adapter = answersAdapter
}
// When server return 200
fun onSuccess() {
answersAdapter.foo = true // Reuse already created adapter
}
}
Alternate solution is use model with enabled
field替代解决方案是使用enabled
字段的 model
data class ListData(val isChecked: Boolean)
And after this you can use diff-util ( https://developer.android.com/reference/androidx/recyclerview/widget/DiffUtil ) to update all changed data at screen.在此之后,您可以使用 diff-util ( https://developer.android.com/reference/androidx/recyclerview/widget/DiffUtil ) 在屏幕上更新所有更改的数据。
And your onBindViewHolder function will be much more simpler:你的 onBindViewHolder function 会简单得多:
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int){
holder.itemView.checkbox.isEnabled = data[position].enabled
}
There are also good libraries like Groupie ( https://github.com/lisawray/groupie ) that reduces RecyclerViews adapters boilerplate code and already have DiffUtil support under the hood.还有像 Groupie ( https://github.com/lisawray/groupie )这样的优秀库可以减少 RecyclerViews 适配器样板代码,并且已经在底层提供了 DiffUtil 支持。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.