无法从 Kotlin 中的自定义 ResyclerView 扩展适配器
Not able to extend Adapter from a custom ResyclerView in Kotlin
我正在尝试基于此 post 实现一个空状态回收器视图。我已经将解决方案迁移到 Kotlin,但问题是我无法从Kotlin 中新定义的自定义回收器视图。我观察到相同的 CustomRecyclerView.Adapter 可以在 Java.
中扩展
自定义 RecyclerView 实现
open class CustomRecyclerView: RecyclerView {
private var emptyStateView : View? = null
constructor(context: Context) : super(context)
constructor(context: Context , attrs: AttributeSet) : super(context,attrs)
constructor(context: Context , attrs: AttributeSet, defstyle: Int) : super(context,attrs,defstyle)
var observer: AdapterDataObserver = object : AdapterDataObserver() {
override fun onChanged() {
super.onChanged()
initEmptyView()
}
override fun onItemRangeRemoved(positionStart: Int, itemCount: Int) {
super.onItemRangeRemoved(positionStart, itemCount)
initEmptyView()
}
override fun onItemRangeInserted(positionStart: Int, itemCount: Int) {
super.onItemRangeInserted(positionStart, itemCount)
initEmptyView()
}
}
private fun initEmptyView() {
emptyStateView?.let {
it.visibility = if (adapter == null || adapter!!.itemCount == 0) View.VISIBLE else View.GONE
this@CustomRecyclerView.visibility = if (adapter == null || adapter!!.itemCount == 0) View.GONE else View.VISIBLE
}
}
override fun setAdapter(adapter: Adapter<*>?) {
val oldAdapter = getAdapter()
super.setAdapter(adapter)
oldAdapter?.unregisterAdapterDataObserver(observer)
adapter?.registerAdapterDataObserver(observer)
}
/**
* @param emptyView is the view which is going to display when the recycler view is empty
* **/
fun setEmptyView(emptyView: View) {
this.emptyStateView = emptyView
initEmptyView()
}}
在 java 和 kotin
中为扩展实现添加图像
总结评论:继承自RecyclerView.Adapter
,见下文
YourAdapter: RecyclerView.Adapter<YourViewHolder>()
提示一下,这里是使用多个构造函数扩展 class 的 Kotlin 方法:
class CustomRecyclerView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
) : RecyclerView(context, attrs, defStyleAttr)
有关详细信息,请查看 @JvmOverloads
应该是RecyclerView.Adapter..并将适配器设置为customRecyclerView。
我正在尝试基于此 post 实现一个空状态回收器视图。我已经将解决方案迁移到 Kotlin,但问题是我无法从Kotlin 中新定义的自定义回收器视图。我观察到相同的 CustomRecyclerView.Adapter 可以在 Java.
中扩展自定义 RecyclerView 实现
open class CustomRecyclerView: RecyclerView {
private var emptyStateView : View? = null
constructor(context: Context) : super(context)
constructor(context: Context , attrs: AttributeSet) : super(context,attrs)
constructor(context: Context , attrs: AttributeSet, defstyle: Int) : super(context,attrs,defstyle)
var observer: AdapterDataObserver = object : AdapterDataObserver() {
override fun onChanged() {
super.onChanged()
initEmptyView()
}
override fun onItemRangeRemoved(positionStart: Int, itemCount: Int) {
super.onItemRangeRemoved(positionStart, itemCount)
initEmptyView()
}
override fun onItemRangeInserted(positionStart: Int, itemCount: Int) {
super.onItemRangeInserted(positionStart, itemCount)
initEmptyView()
}
}
private fun initEmptyView() {
emptyStateView?.let {
it.visibility = if (adapter == null || adapter!!.itemCount == 0) View.VISIBLE else View.GONE
this@CustomRecyclerView.visibility = if (adapter == null || adapter!!.itemCount == 0) View.GONE else View.VISIBLE
}
}
override fun setAdapter(adapter: Adapter<*>?) {
val oldAdapter = getAdapter()
super.setAdapter(adapter)
oldAdapter?.unregisterAdapterDataObserver(observer)
adapter?.registerAdapterDataObserver(observer)
}
/**
* @param emptyView is the view which is going to display when the recycler view is empty
* **/
fun setEmptyView(emptyView: View) {
this.emptyStateView = emptyView
initEmptyView()
}}
在 java 和 kotin
中为扩展实现添加图像总结评论:继承自RecyclerView.Adapter
,见下文
YourAdapter: RecyclerView.Adapter<YourViewHolder>()
提示一下,这里是使用多个构造函数扩展 class 的 Kotlin 方法:
class CustomRecyclerView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
) : RecyclerView(context, attrs, defStyleAttr)
有关详细信息,请查看 @JvmOverloads
应该是RecyclerView.Adapter..并将适配器设置为customRecyclerView。