抽象的回收者片段在Kotlin

问题描述 投票:0回答:1

我正在尝试创建一个设置回收器视图的抽象片段类。这是我的适配器的相关部分:

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)上抛出错误:

类型不匹配:推断类型是PeopleAdapter,但期望使用RecyclerView.Adapter

我不明白为什么这是一个问题。 PersonAdapter延伸RecyclerView.AdapterPersonViewHolder延伸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)
}

这一次,错误是:

类型不匹配:推断类型是PeopleAdapter,但期望使用RecyclerView.Adapter

再一次,我期待这些类型排队,但不确定我做错了什么。

android inheritance kotlin android-recyclerview abstract
1个回答
1
投票

在抽象类中定义泛型类型,并在具体实现中提供它们:

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>

© www.soinside.com 2019 - 2024. All rights reserved.