android - [Kotlin][RecyclerView] 错误未连接适配器
问题描述
需要帮忙。我在 Stack Overflow 中检查了其他一些解决方案,但我没有到达让我的 Fragment 工作。
问题:当我显示我的 Fragment 时,我的屏幕非常空,并且出现错误“ E/RecyclerView:未连接适配器;跳过布局”
3个问题(肯定链接):
- E/RecyclerView:没有附加适配器;跳过布局错误
- 当我用“两个数据列表”初始化 MyAdapter 时:没有变化
- 当我到达片段时,我可以在 logcat 中看到 onResume() 被调用但屏幕上没有变化。
片段 (XML)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/ServiceIssuesReader"
tools:context=".ui.service.ServiceIssuesReaderFragment">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/my_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layoutManager="LinearLayoutManager"
/>
</RelativeLayout>
一行数据(一天)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip" >
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_marginRight="6dip"
android:contentDescription="TODO"
android:src="@mipmap/ic_launcher" />
<TextView
android:id="@+id/secondLine"
android:layout_width="fill_parent"
android:layout_height="26dip"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_toRightOf="@id/icon"
android:text="Description"
android:textSize="12sp" />
<TextView
android:id="@+id/firstLine"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@id/secondLine"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_alignWithParentIfMissing="true"
android:layout_toRightOf="@id/icon"
android:gravity="center_vertical"
android:text="Example application"
android:textSize="16sp" />
</RelativeLayout>
片段类
class ServiceIssuesReaderFragment: Fragment() {
var mAdapter: RecyclerView.Adapter<MyAdapter.ViewHolder>? = null
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val rootView = inflater.inflate(R.layout.fragment_issues_reader, container, false)
mAdapter = MyAdapter(mutableListOf<String>())
var recyclerView = view?.findViewById<RecyclerView>(R.id.my_recycler_view) as RecyclerView
recyclerView.setHasFixedSize(true)
recyclerView.layoutManager = LinearLayoutManager(context)
recyclerView.adapter = mAdapter
return rootView
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onResume() {
super.onResume()
// Adding stub data
val input: MutableList<String> = ArrayList()
for (i in 0..99) {
Log.d("onResume:: ", ">> + [Test$i]")
input.add("Test$i")
}
(mAdapter as MyAdapter).addItems(input)
mAdapter?.notifyDataSetChanged()
}
}
适配器类
class MyAdapter (private var values: MutableList<String>): RecyclerView.Adapter<MyAdapter.ViewHolder>() {
// Provide a reference to the views for each data item
// Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder
inner class ViewHolder(v: View) : RecyclerView.ViewHolder(v) {
// each data item is just a string in this case
var txtHeader: TextView
var txtFooter: TextView
var layout: View
init {
layout = v
txtHeader = v.findViewById<TextView>(R.id.firstLine)
txtFooter = v.findViewById<TextView>(R.id.secondLine)
}
}
fun add(position: Int, item: String) {
values.add(position, item)
notifyItemInserted(position)
}
fun remove(position: Int) {
values.removeAt(position)
notifyItemRemoved(position)
}
fun addItems (newValues: MutableList<String>){
values.addAll(newValues)
notifyDataSetChanged()
}
// Create new views (invoked by the layout manager)
override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int
): ViewHolder {
// create a new view
val inflater = LayoutInflater.from(
parent.context
)
//val v: View = inflater.inflate(R.layout.row_layout, parent, false)
val v: View = inflater.inflate(R.layout.row_layout_issue, parent, false)
// set the view's size, margins, paddings and layout parameters
return ViewHolder(v)
}
// Replace the contents of a view (invoked by the layout manager)
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
// - get element from your dataset at this position
// - replace the contents of the view with that element
val name = values[position]
holder.txtHeader.text = name
holder.txtHeader.setOnClickListener { remove(position) }
holder.txtFooter.text = "Footer: $name"
}
// Return the size of your dataset (invoked by the layout manager)
override fun getItemCount(): Int {
return values.size
}
}
我尝试在 onCreateView、onCreate 和 onResume 中初始化 Recycler,但没有更好的结果。为什么它不起作用的想法?
解决方案
所以,我找到了解决方案!
要删除错误未连接适配器错误,我更改
recyclerView.layoutManager = LinearLayoutManager(context)
recyclerView.adapter = mAdapter
至
recyclerView.apply {
layoutManager = LinearLayoutManager(context)
adapter = mAdapter
}
(我在另一个线程中看到了相反的情况,但谁知道......)为什么它有效?我不知道,但这是工作!
推荐阅读
- c# - C# switch case 数组包含字符串
- php - Wordpress 4.9.8 错误:常量 STYLESHEETPATH
- php - 将包含科学计数法数字的字符串转换为 PHP 中的普通字符串
- c# - Kestrel 如何将数据传回控制台应用程序
- c# - 如何从excel文件中提取图像并通过C# windows服务将其保存到文件中
- node.js - 如何在客户端显示来自 mongodb 的数据?
- c - 为什么在使用 gcc 优化时 do...while 比 while 慢
- oauth - 执行 Refresh_Token 时出现 Invalid_grant 错误
- python - 同时多个异步请求
- arrays - 使用 Swift 计算嵌套数组中的唯一 ID