我无法将片段中的 recyclerview 与适配器连接起来

I could not connect my recyclerview in fragment with adapter

我的片段class

class RecyclerFragment : Fragment() {
var recycler:RecyclerView? =null

private var param1: String? = null
private var param2: String? = null


override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    recycler = view?.findViewById(R.id.recycler)

    }


override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_recycler, container, false)
}

override fun onActivityCreated(savedInstanceState: Bundle?) {
    super.onActivityCreated(savedInstanceState)
    recycler?.layoutManager = LinearLayoutManager(this.context,RecyclerView.VERTICAL,false)


    recycler?.adapter = Custom_Adapter() // error-Unresolved reference: Custom_Adapter


}


}

//这是我的适配器

 class Custom_Adapter() : RecyclerView.Adapter<Custom_Adapter.ViewHolder>() {


override fun onCreateViewHolder(viewGroup: ViewGroup, viewType: Int): Custom_Adapter.ViewHolder {
 var myViewInflater = LayoutInflater.from(viewGroup.context).inflate(R.layout.custom_layout,viewGroup,false)
    return ViewHolder(myViewInflater)
}

override fun onBindViewHolder(holder: Custom_Adapter.ViewHolder, position: Int) {

}

override fun getItemCount(): Int {
    return 9

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

}
}

像这样更新你的回收器片段。

class RecyclerFragment : Fragment(R.layout.fragment_recycler) {
var recycler:RecyclerView? =null

    private var param1: String? = null
    private var param2: String? = null    


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        recycler = view.findViewById(R.id.recycler)
        recycler?.layoutManager = LinearLayoutManager(this.context,RecyclerView.VERTICAL,false)
        recycler?.adapter = Custom_Adapter()
    }


}

并且您必须将 类 Recucler_ItemCustom_Adapter 移动到正确的 java 包中。

从您的回购 link 中的一条评论中,Custom_Adapter 在您的 src/test 目录下。它需要在 src/mainRecyclerFragment 可以看到它 - 这就是为什么您会收到 unresolved reference 错误。把它和其他东西一起放在那里

maintest 是不同的 source sets,它基本上是一种将内容分开的方法。您所有的生产代码都放在 main 中,默认情况下它在 test 中看不到任何内容。当您 运行 测试时,那些使用 test 中的所有内容(可能 androidTest)并包含 main 中的所有内容,因为测试需要访问生产代码以,你知道,测试一下!