Kotlin 中的抽象回收器片段
Abstract recycler fragment in Kotlin
我正在尝试创建一个抽象片段 class 来设置回收器视图。这是我的适配器的相关部分:
class PeopleAdapter(context: Context?) : RecyclerView.Adapter<PeopleAdapter.PersonViewHolder>() {
inner class PersonViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
// ...
}
// ...
}
这是摘要片段:
abstract class RecyclerFragment : Fragment() {
protected abstract fun createAdapter(context: Context?): RecyclerView.Adapter<RecyclerView.ViewHolder>
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.recycler_view, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val adapter = createAdapter(context)
recycler_view.adapter = adapter
// ...
}
}
我想这样使用它:
class PeopleFragment : RecyclerFragment() {
override fun createAdapter(context: Context?): RecyclerView.Adapter<RecyclerView.ViewHolder> {
return PeopleAdapter(context)
}
}
Android Studio 在 return PeopleAdapter(context)
:
上抛出错误
Type mismatch: inferred type is PeopleAdapter but
RecyclerView.Adapter was expected
我不明白为什么这是个问题。 PersonAdapter
扩展了 RecyclerView.Adapter
,PersonViewHolder
扩展了 RecyclerView.ViewHolder
,所以不是暗示 PersonAdapter
扩展了 RecyclerView.Adapater<RecyclerView.ViewHolder>
吗?
我尝试按如下方式更改 createAdapter
方法:
protected abstract fun <T : RecyclerView.ViewHolder?> createAdapter(context: Context?): RecyclerView.Adapter<T>
我也更改了实现:
override fun <T : RecyclerView.ViewHolder?> createAdapter(context: Context?): RecyclerView.Adapter<T> {
return PeopleAdapter(context)
}
这一次,错误是:
Type mismatch: inferred type is PeopleAdapter but
RecyclerView.Adapter was expected
同样,我希望类型一致,但不确定我做错了什么。
在抽象中定义泛型 class,并在具体实现中提供它们:
class PeopleAdapter(context: Context?) : RecyclerView.Adapter<PeopleAdapter.PersonViewHolder>() {
inner class PersonViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
// ...
}
// ...
}
abstract class RecyclerFragment<VH : RecyclerView.ViewHolder, out T : RecyclerView.Adapter<VH>> : Fragment() {
protected abstract fun createAdapter(context: Context?): T
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.recycler_view, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val adapter = createAdapter(context)
recycler_view.adapter = adapter
// ...
}
}
class PeopleFragment : RecyclerFragment<PeopleAdapter.PersonViewHolder, PeopleAdapter>() {
override fun createAdapter(context: Context?): PeopleAdapter {
return PeopleAdapter(context)
}
}
你目前能得到的最接近的是一个未经检查的演员表:
return PeopleAdapter(context) as RecyclerView.Adapter<RecyclerView.ViewHolder>
我正在尝试创建一个抽象片段 class 来设置回收器视图。这是我的适配器的相关部分:
class PeopleAdapter(context: Context?) : RecyclerView.Adapter<PeopleAdapter.PersonViewHolder>() {
inner class PersonViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
// ...
}
// ...
}
这是摘要片段:
abstract class RecyclerFragment : Fragment() {
protected abstract fun createAdapter(context: Context?): RecyclerView.Adapter<RecyclerView.ViewHolder>
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.recycler_view, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val adapter = createAdapter(context)
recycler_view.adapter = adapter
// ...
}
}
我想这样使用它:
class PeopleFragment : RecyclerFragment() {
override fun createAdapter(context: Context?): RecyclerView.Adapter<RecyclerView.ViewHolder> {
return PeopleAdapter(context)
}
}
Android Studio 在 return PeopleAdapter(context)
:
Type mismatch: inferred type is PeopleAdapter but RecyclerView.Adapter was expected
我不明白为什么这是个问题。 PersonAdapter
扩展了 RecyclerView.Adapter
,PersonViewHolder
扩展了 RecyclerView.ViewHolder
,所以不是暗示 PersonAdapter
扩展了 RecyclerView.Adapater<RecyclerView.ViewHolder>
吗?
我尝试按如下方式更改 createAdapter
方法:
protected abstract fun <T : RecyclerView.ViewHolder?> createAdapter(context: Context?): RecyclerView.Adapter<T>
我也更改了实现:
override fun <T : RecyclerView.ViewHolder?> createAdapter(context: Context?): RecyclerView.Adapter<T> {
return PeopleAdapter(context)
}
这一次,错误是:
Type mismatch: inferred type is PeopleAdapter but RecyclerView.Adapter was expected
同样,我希望类型一致,但不确定我做错了什么。
在抽象中定义泛型 class,并在具体实现中提供它们:
class PeopleAdapter(context: Context?) : RecyclerView.Adapter<PeopleAdapter.PersonViewHolder>() {
inner class PersonViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
// ...
}
// ...
}
abstract class RecyclerFragment<VH : RecyclerView.ViewHolder, out T : RecyclerView.Adapter<VH>> : Fragment() {
protected abstract fun createAdapter(context: Context?): T
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.recycler_view, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val adapter = createAdapter(context)
recycler_view.adapter = adapter
// ...
}
}
class PeopleFragment : RecyclerFragment<PeopleAdapter.PersonViewHolder, PeopleAdapter>() {
override fun createAdapter(context: Context?): PeopleAdapter {
return PeopleAdapter(context)
}
}
你目前能得到的最接近的是一个未经检查的演员表:
return PeopleAdapter(context) as RecyclerView.Adapter<RecyclerView.ViewHolder>