在 FrameLayout Kotlin 中显示 RecyclerView

Display RecyclerView in FrameLayout Kotlin

我一直在 E/RecyclerView: No adapter attached; skipping layout。 尽我所能,但似乎没有解决。 在这里浏览了一会儿,发现了一些类似的问题,但我不知道具体要改变什么 案件。 如果您需要更多信息,请随时询问。

class ProdItemAdapter(private val prodList: List<ProduktItem>) :
        RecyclerView.Adapter<ProdItemAdapter.ProdItemViewHolder>() {
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ProdItemViewHolder {
            val itemView =
                LayoutInflater.from(parent.context).inflate(R.layout.card_view_prod, parent, false)
    
            return ProdItemViewHolder(itemView)
        }
    
        override fun onBindViewHolder(holder: ProdItemViewHolder, position: Int) {
            val currentItem = prodList[position]
    
            holder.cvProdName.text = currentItem.name
            holder.cvProdCode.text = currentItem.code
        }
    
        override fun getItemCount() = prodList.size
    
        class ProdItemViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
            val cvProdName: TextView = itemView.cv_name
            val cvProdCode: TextView = itemView.cv_code
        }
    }

RecyclerView-Adapter: Adding some Breakpoints inside this class, all get passed.

class FragProd : Fragment() {
        private var param1: Int? = null
        private var listener: OnFragmentInteractionListener? = null

        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            arguments?.let {
                param1 = it.getInt(ARG_SECTION_NUMBER)
            }

            val pl = getList()
            rvProd.adapter = ProdItemAdapter(pl)
            rvProd.layoutManager = LinearLayoutManager(context)
        }

        override fun onCreateView(
            inflater: LayoutInflater,
            container: ViewGroup?,
            savedInstanceState: Bundle?
        ): View? {
            return inflater.inflate(R.layout.fragment_frag_prod, container, false)
        }
             .......................

Fragment

试试这个代码,在 OnCreateView 中调用你的适配器。

override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        View view = inflater.inflate(R.layout.fragment_frag_prod, container, false)
        val pl = getList()
        rvProd.adapter = ProdItemAdapter(pl)
        rvProd.layoutManager = LinearLayoutManager(context)
        return view
    }