简体   繁体   English

由于变量值 kotlin,如何更新 recyclerView 列表?

[英]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 变量中调用。我做错了什么以及如何解决它?

UPDATE更新

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.

相关问题 Kotlin:如何更新 RecyclerView 的值 - Kotlin : How to update a value of a RecyclerView 如何使用 kotlin 从另一个 RecyclerView 更新 RecyclerView - How to update a RecyclerView from another RecyclerView with kotlin 如何从适配器获取变量值到android kotlin recyclerview中的活动? - How to get the variable value from adapter to activities in android kotlin recyclerview? 如何使用 Kotlin 和 Room 更新 RecyclerView? - How to update RecyclerView using Kotlin and Room? 使用 kotlin 创建视图后如何更新 recyclerview - How to update recyclerview after view was created with kotlin 如何使用Kotlin Android更新我的Recyclerview? - How to update my Recyclerview using kotlin android? 使用 Groupie RecyclerView 库和 Kotlin 删除项目时如何通知和更新列表 - How to notify and update list when item has been deleted using Groupie RecyclerView library and Kotlin 如何从 RecyclerView 适配器(Kotlin)评估片段中的变量(更改值)以进行条件处理? - How to evaluate a variable (changing value) in a Fragment from a RecyclerView Adapter (Kotlin) for conditional processing? Kotlin - 如何从数据类列表中更新特定值 - Kotlin - How to update particular value from a list of data class 如何使用 DiffUtils 更新 RecyclerView 中的列表? - How to update list in RecyclerView with DiffUtils?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM