Java 无法从Kotlin中的自定义ResCyclerView扩展适配器

Java 无法从Kotlin中的自定义ResCyclerView扩展适配器,java,android,kotlin,android-recyclerview,abstract-class,Java,Android,Kotlin,Android Recyclerview,Abstract Class,我正试图基于此实现空状态回收器视图。我已将解决方案迁移到Kotlin,但问题是我无法从Kotlin中新定义的自定义回收器视图扩展CustomRecyclerView.Adapter(Adapter是在RecyclerView中定义的抽象类)。我观察到同样的CustomRecyclerViewAdapter可以在Java中扩展 Custome RecyclerView实现 开放类CustomRecyclerView:RecyclerView{ 私有变量emptyStateView:视图?=null

我正试图基于此实现空状态回收器视图。我已将解决方案迁移到Kotlin,但问题是我无法从Kotlin中新定义的自定义回收器视图扩展CustomRecyclerView.Adapter(Adapter是在RecyclerView中定义的抽象类)。我观察到同样的CustomRecyclerViewAdapter可以在Java中扩展

Custome RecyclerView实现
开放类CustomRecyclerView:RecyclerView{
私有变量emptyStateView:视图?=null
构造函数(上下文:上下文):超级(上下文)
构造函数(context:context,attrs:AttributeSet):super(context,attrs)
构造函数(context:context,attrs:AttributeSet,defstyle:Int):super(context,attrs,defstyle)
变量观察者:AdapterDataObserver=对象:AdapterDataObserver(){
override-fun-onChanged(){
super.onChanged()
initEmptyView()
}
覆盖已删除的文件(位置开始:Int,项目计数:Int){
super.onItemRangeRemoved(位置开始,项目计数)
initEmptyView()
}
覆盖已插入的项目(位置开始:Int,项目计数:Int){
super.onItemRangeInserted(positionStart,itemCount)
initEmptyView()
}
}
私人娱乐initEmptyView(){
清空状态视图?让我们{
it.visibility=if(adapter==null | | adapter!!.itemCount==0)View.VISIBLE else View.go
this@CustomRecyclerView.visibility=if(adapter==null | | adapter!!.itemCount==0)View.GONE else View.VISIBLE
}
}
覆盖有趣的设置适配器(适配器:适配器?){
val oldapter=getAdapter()
super.setAdapter(适配器)
oldAdapter?.UnregisteredAdapterDataObserver(观察者)
适配器?.registerAdapterDataObserver(观察者)
}
/**
*@param emptyView是回收器视图为空时将显示的视图
* **/
fun setEmptyView(emptyView:View){
this.emptyStateView=emptyView
initEmptyView()
}}
为java和kotin中的扩展实现添加图像


要总结注释:从
RecyclerView.Adapter继承,请参见下文

YourAdapter: RecyclerView.Adapter<YourViewHolder>()

有关更多信息,请选中它应为RecyclerView.Adapter。。并将适配器设置为customRecyclerView

你到底有什么问题?在Kotlin中,您可以像在Java中一样扩展适配器:
classyourAdapter:RecyclerView.Adapter()
。您链接的教程正是这样做的。您是否收到任何IDE错误?我收到错误:未解析的引用适配器您可能正在做的是
为您的适配器分类:YourCustomRecyclerView.Adapter()
,而应该做的是
为您的适配器分类:RecyclerView.Adapter()
。因为
Adapter
RecyclerView
本身的一部分,而不是它的子类,所以请参见RV源代码第6700行:
public abstract static class Adapter
谢谢@Droidman,这很有意义。让我尝试使用普通的RecyclerView.Adapter。添加更多细节:
YourAdapter: RecyclerView.Adapter<YourViewHolder>()
class CustomRecyclerView @JvmOverloads constructor(
        context: Context,
        attrs: AttributeSet? = null,
        defStyleAttr: Int = 0
) : RecyclerView(context, attrs, defStyleAttr)