首页 > 解决方案 > [Kotlin][RecyclerView] 错误未连接适配器

问题描述

需要帮忙。我在 Stack Overflow 中检查了其他一些解决方案,但我没有到达让我的 Fragment 工作。

问题:当我显示我的 Fragment 时,我的屏幕非常空,并且出现错误“ E/RecyclerView:未连接适配器;跳过布局

3个问题(肯定链接):

片段 (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,但没有更好的结果。为什么它不起作用的想法?

标签: androidkotlinandroid-recyclerview

解决方案


所以,我找到了解决方案!

要删除错误未连接适配器错误,我更改

   recyclerView.layoutManager = LinearLayoutManager(context)
   recyclerView.adapter = mAdapter

  recyclerView.apply {
     layoutManager = LinearLayoutManager(context)
     adapter = mAdapter
  }

(我在另一个线程中看到了相反的情况,但谁知道......)为什么它有效?我不知道,但这是工作!


推荐阅读