无法访问持有者中的视图

时间:2018-04-30 23:32:14

标签: android android-recyclerview kotlin anko

我正在尝试创建RecyclerView适配器并关注this文档。

private class Adapter(private val list: List<HashMap<String, Any>>, private val ctx: Context) : RecyclerView.Adapter<Adapter.ViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): Adapter.ViewHolder {
        var mTextView: TextView

        return ViewHolder(ctx.UI {
            relativeLayout {
                mTextView = textView("1 + 1 = 2")
            }
        }.view)
    }

    override fun getItemCount() = list.size

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.mTextView.text = "1 + 1 = 3" // Should work?
    }

    class ViewHolder(view: View) : RecyclerView.ViewHolder(view)
}

如何访问mTextView中的onBindViewHolder

2 个答案:

答案 0 :(得分:1)

如果你正在使用kotlin扩展并且文本视图的id为mTextView,那么它应该是:

override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.itemView.mTextView.text = "1 + 1 = 3" // Should work?
    }

您还可以在ViewHolder中定义变量并直接使用它们,这是性能方面的最佳方式,因为它不会强制对findviewbyid进行不必要的调用:

class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
    val myTextView = itemView.mTextView
}

稍后访问它:

 holder.myTextView.text = "some text"

答案 1 :(得分:0)

您需要在此处为​​TextView内的ViewHolder创建一个属性。使用Anko并不是特别容易,但这里有一些方法可以做到。

1。通过您希望能够从View引用的每个ViewHolder作为单独的构造函数参数。

对于此解决方案,您必须像这样修改ViewHolder类,为TextView添加属性:

class ViewHolder(view: View, val mTextView: TextView) : RecyclerView.ViewHolder(view)

您可以用稍微复杂的方式初始化此属性:

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): Adapter.ViewHolder {
    lateinit var mTextView: TextView

    val view = ctx.UI {
        relativeLayout {
            mTextView = textView("1 + 1 = 2")
        }
    }.view

    return ViewHolder(view, mTextView)
}

虽然这看起来不是很好,但是当您最初创建TextView时,您仍然保留对View的引用,这样才有效。

2。为您的Anko创建companion object { private const val TEXT_VIEW_ID = 123 // Arbitrary positive number } 个实例ID,稍后按ID查找。

首先,您必须为您的ID创建常量 - ID可以是任何正数,只需确保它们在您使用它们的范围内是唯一的。一种方法是将它们放在一个伴侣对象中,如下所示:

TextView

然后,您需要将此ID提供给override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): Adapter.ViewHolder { return ViewHolder(ctx.UI { relativeLayout { textView("1 + 1 = 2") { id = TEXT_VIEW_ID } } }.view) }

TextView

最后,您可以使用ViewHolderfindViewById课程中再次找到class ViewHolder(view: View) : RecyclerView.ViewHolder(view) { val mTextView: TextView = view.findViewById(TEXT_VIEW_ID) }

TextView

当然这会慢一点,因为当您将View传递给ViewHolder时,您已经失去了mTextView引用,然后你再次查找它。

请注意,我坚持使用CREATE TABLE SAMPLE.ALERTS (job_id UUID, section text, result text, PRIMARY KEY(job_id,section)); INSERT INTO SAMPLE.ALERTS (job_id, section, result) values (now(),'0100','successful'); INSERT INTO SAMPLE.ALERTS (job_id, section, result) values (now(),'0100','successful'); INSERT INTO SAMPLE.ALERTS (job_id, section, result) values (now(),'0200','successful'); INSERT INTO SAMPLE.ALERTS (job_id, section, result) values (now(),'0200','failed'); INSERT INTO SAMPLE.ALERTS (job_id, section, result) values (now(),'0300','failed'); INSERT INTO SAMPLE.ALERTS (job_id, section, result) values (now(),'0300','successful'); INSERT INTO SAMPLE.ALERTS (job_id, section, result) values (now(),'0400','failed'); INSERT INTO SAMPLE.ALERTS (job_id, section, result) values (now(),'0400','failed'); INSERT INTO SAMPLE.ALERTS (job_id, section, result) values (now(),'0700','failed'); INSERT INTO SAMPLE.ALERTS (job_id, section, result) values (now(),'0700','failed'); 来获得答案,以便更容易理解,但通常not recommended在Kotlin中使用匈牙利表示法或任何其他名称前缀