RecyclerView.Adapter-错误:公共函数在Kotlin中公开其内部返回类型

cha*_*l03 15 android kotlin

我在Kotlin中实现了一个RecylcerView.Adapter类.我收到编译时错误,请参阅以下代码中的注释.

// Compile time Error: 'public' function exposes its 'internal' return type ViewHolder
class DietListAdapter(context: Context, private val foodList: ArrayList<Food>) : RecyclerView.Adapter<DietListAdapter.ViewHolder>() {

    private val inflater: LayoutInflater
    private var onItemClick: Callback<Void, Int>? = null

    init {
        inflater = LayoutInflater.from(context)
    }
    // Compile time Error: 'public' function exposes its 'internal' return type ViewHolder
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DietListAdapter.ViewHolder {
        val holder = ViewHolder(inflater.inflate(R.layout.layout_food_list_item, parent, false))
        return holder
    }
    // Compile time Error: 'public' function exposes its 'internal' parameter type ViewHolder
    override fun onBindViewHolder(holder: DietListAdapter.ViewHolder, position: Int) {
        holder.textViewFoodName.text = foodList[position].foodName
        holder.textViewFoodDesc.text = foodList[position].foodDesc

        holder.itemView.setOnClickListener {
            if (onItemClick != null)
                onItemClick!!.callback(foodList[position].foodId)
        }
    }
    ...
    ...
    internal inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

        var textViewFoodName: TextView
        var textViewFoodDesc: TextView

        init {
            textViewFoodName = itemView.findViewById(R.id.textViewFoodName) as TextView
            textViewFoodDesc = itemView.findViewById(R.id.textViewFoodDesc) as TextView
        }
    }
    ...
    ...
}
Run Code Online (Sandbox Code Playgroud)

我在Kotlin文档中检查了它,没有解决方案.

还有其他人遇到过这个问题吗?

cha*_*l03 37

我的坏,一个愚蠢的错误.我在Android Studio中将Java代码转换为Kotlin,因此它将内部类转换为内部内部类.

我刚删除内部它工作正常.

我打算删除这个问题,只是想到有人可能会遇到同样的问题,所以刚刚发布了一个答案.

  • 它确实帮助了我:) (4认同)
  • 或者您也可以在适配器类上添加`internal`关键字.就像`内部类SomeClassName` (2认同)