I have set an ItemClickLister for my RecyclerView like this:
ItemClickSupport.addTo(recyclerView!!).setOnItemClickListener(
object : ItemClickSupport.OnItemClickListener {
override fun onItemClicked(recyclerView: RecyclerView?, position: Int, v: View?) {
val row = recyclerView!!.getChildAt(position)
val el = row.findViewById(R.id.active_expandablelayout) as ExpandableLayout
if (el.isExpanded) {
el.collapse()
} else {
el.expand()
}
}
}
)
using the ItemClickSupport library which I translated into Kotlin.
I get an error on object
(line 2) that says:
object must be declared abstract or implement abstract member.
I'm really new to Kotlin and I couldn't find any solution here on SO either.
Any help is greatly appreciated.
Edit:
Here is my ItemClickSupport.kt
:
class ItemClickSupport private constructor(private val mRecyclerView: RecyclerView) {
private var mOnItemClickListener: OnItemClickListener? = null
private var mOnItemLongClickListener: OnItemLongClickListener? = null
private val mOnClickListener = View.OnClickListener { v ->
if (mOnItemClickListener != null) {
val holder = mRecyclerView.getChildViewHolder(v)
mOnItemClickListener!!.onItemClicked(mRecyclerView, holder.adapterPosition, v)
}
}
private val mOnLongClickListener = View.OnLongClickListener { v ->
if (mOnItemLongClickListener != null) {
val holder = mRecyclerView.getChildViewHolder(v)
return@OnLongClickListener mOnItemLongClickListener!!.onItemLongClicked(mRecyclerView, holder.adapterPosition, v)
}
false
}
private val mAttachListener = object : RecyclerView.OnChildAttachStateChangeListener {
override fun onChildViewAttachedToWindow(view: View) {
if (mOnItemClickListener != null) {
view.setOnClickListener(mOnClickListener)
}
if (mOnItemLongClickListener != null) {
view.setOnLongClickListener(mOnLongClickListener)
}
}
override fun onChildViewDetachedFromWindow(view: View) {
}
}
init {
mRecyclerView.setTag(R.id.item_click_support, this)
mRecyclerView.addOnChildAttachStateChangeListener(mAttachListener)
}
fun setOnItemClickListener(listener: OnItemClickListener): ItemClickSupport {
mOnItemClickListener = listener
return this
}
fun setOnItemLongClickListener(listener: OnItemLongClickListener): ItemClickSupport {
mOnItemLongClickListener = listener
return this
}
private fun detach(view: RecyclerView) {
view.removeOnChildAttachStateChangeListener(mAttachListener)
view.setTag(R.id.item_click_support, null)
}
interface OnItemClickListener {
fun onItemClicked(recyclerView: RecyclerView, position: Int, v: View)
}
interface OnItemLongClickListener {
fun onItemLongClicked(recyclerView: RecyclerView, position: Int, v: View): Boolean
}
companion object {
fun addTo(view: RecyclerView): ItemClickSupport {
var support: ItemClickSupport? = view.getTag(R.id.item_click_support) as ItemClickSupport
if (support == null) {
support = ItemClickSupport(view)
}
return support
}
fun removeFrom(view: RecyclerView): ItemClickSupport {
val support = view.getTag(R.id.item_click_support) as ItemClickSupport
support?.detach(view)
return support
}
}
}
This is a screenshot with the whole error and where it happens: