上述代码出现了如下错误:Type mismatch: inferred type is View? but View was expected

动态 未结 0 92
小小草
小小草 LV1 2023年9月23日 23:40 发表
这个错误是因为getChildAt()方法返回的是View?类型,而你在使用时需要传入View类型。可以将getChildAt()改成getChildViewHolder(),这样就可以获取到ViewHolder对象,然后再通过ViewHolder获取View: ``` override fun onLayoutChildren(recycler: RecyclerView.Recycler?, state: RecyclerView.State?) { super.onLayoutChildren(recycler, state) var totalHeight = 0 for (i in 0 until childCount) { val child = getChildViewHolder(getChildAt(i)).itemView totalHeight += child.height } if (totalHeight > height) { mRvChat.postDelayed({ mRvChat.smoothScrollToPosition(itemCount - 1) }, 1000) } } ``` 这样就可以避免类型不匹配的问题了。
收藏(0)  分享
相关标签: 智能问答
问题没解决?让AI助手帮你作答 AI助手
0个回复
  • 消灭零回复