【问题标题】:Kotlin || Recycleview item is not inflating: Unreachable code on each overridden method of RecycleView.Adapter科特林 || Recyclerview 项目没有膨胀:RecyclerView.Adapter 的每个重写方法上的代码无法访问
【发布时间】:2017-07-07 06:31:44
【问题描述】:

我正在 Kotlin 中创建自定义 Recycleview
我在代码中没有遇到任何异常,它运行完美,但令人担忧的是,该项目没有出现在 Recycleview 上,因为代码在 Adapter 类中无法访问。

警告:-RecycleView 适配器的覆盖方法上的代码无法访问。

请检查我的适配器和活动类

适配器类

class CustomRecycleAdapter(val data: ArrayList<String>) : RecyclerView.Adapter<CustomRecycleAdapter.ViewHolder>() {
    override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ViewHolder {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
        val v = LayoutInflater.from(parent?.context).inflate(R.layout.recycle_item, parent, false)
        return ViewHolder(v)

    }

    override fun getItemCount(): Int {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
        return data.size;
    }

    override fun onBindViewHolder(holder: ViewHolder?, position: Int) {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
        val item = data[position]
        println("Data is here==>>> "+item)
        holder?.textViewName?.text = item;
    }

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {


        val textViewName = itemView.findViewById<TextView>(R.id.tvName)

    }
}

活动类

class FirstRecycleViewExample : AppCompatActivity() {
    val data = arrayListOf<String>()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.first_recycleview)

        val recycler_view =   findViewById<RecyclerView>(R.id.recycler_view)

        data.add("First Data")
        data.add("Second Data")
        data.add("Third Data")
        data.add("Forth Data")
        data.add("Fifth Data")

        //creating our adapter
        val adapter = CustomRecycleAdapter(data)

        //now adding the adapter to recyclerview
        recycler_view.adapter = adapter

    }


}

【问题讨论】:

  • 去掉 TODO("not implemented") 看看是否有效
  • @AlfMoh ,删除了无法访问的错误..但是列表中的数据又一次没有被错误处理
  • 在构造函数中传递上下文并在 Layout.from() 中使用该上下文。

标签: android android-recyclerview kotlin


【解决方案1】:

您尚未为 RecyclerView 添加布局管理器。只需添加 LinearLayoutManagerGridLayoutManager

recycler_view.layoutManager = LinearLayoutManager(this, LinearLayout.VERTICAL, false)

【讨论】:

  • 谢谢乔尔..真是个愚蠢的错误,我做了:( ...在android中我用过很多次..谢谢
【解决方案2】:

您的代码不会基于您使用 TODO 函数运行

查看 kotlin 网站了解更多详情TODO function

这将始终抛出异常,除非您将其删除或将其注释掉

comment out TODO

要验证答案,请检查您的日志消息并将错误消息与 TODO 中的消息集交叉引用

如果这不是问题,那么很可能是您没有将 recycle_view.layoutManager 设置为 LinearLayoutManager、GridLayoutManager 等。

这将在您在主 UI 上显示回收视图时设置内容布局的类中完成,例如setContentView(R.layout.activity_main)

希望这会有所帮助:)

【讨论】:

  • 感谢您的输入..+1 输入:)
猜你喜欢
  • 2021-11-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多