我正在尝试创建带有项目列表的android wear应用。为了启用循环滚动,我通过在kotlin中进行编码来使用WearableRecyclerView。但是面对此错误 Unresolved reference :适配器 java中的相同代码不会引发此错误!我还提到了kotlin中的内部和外部类概念。适配器类是RecyclerView的抽象类,而WearableRecyclerView又对其进行了扩展。任何指导将很有用,谢谢!!
这是我的代码
class CustomRecyclerAdapter(context: Context,dataSet: Array<String>): WearableRecyclerView.Adapter<CustomRecyclerAdapter.viewHolder>() {
var mDataSet: Array<String>
var mcontext: Context
init {
mDataSet = dataSet
mcontext=context
}
inner class viewHolder(view: View) : WearableRecyclerView(mcontext) {
val mTextView: TextView
init {
mTextView = view.findViewById(R.id.textView)
}
override fun toString(): String {
return mTextView.text as String
}
}
fun onCreateViewHolder(viewGroup: ViewGroup, viewType: Int): viewHolder {
val view = LayoutInflater.from(viewGroup.context)
.inflate(R.layout.recycler_row_item, viewGroup, false)
return viewHolder(view)
}
fun onBindViewHolder(viewHolder: viewHolder, position: Int) {
// Replaces content of view with correct element from data set
viewHolder.mTextView.text = mDataSet[position]
}
// Return the size of your dataset (invoked by the layout manager)
fun getItemCount(): Int {
return mDataSet.size
}
}
最佳答案
您的类inner class viewHolder(view: View) : WearableRecyclerView(mcontext)
必须扩展WearableRecyclerView.ViewHolder
而不是WearableRecyclerView
,例如:
inner class viewHolder(view: View) : WearableRecyclerView.ViewHolder(view)