I’m implementing Android RecyclerView
with three different ViewHolder
s.
-
RecyclerView
’sAdapter
needs to implement three functions, among themfun onCreateViewHolder(parent: ViewGroup, type: Int): ViewHolder
where the returningViewHolder
must be aRecyclerView.ViewHolder
or its child. -
Each
ViewHolder
has a few properties that are common for each of them. LikeImageView
s andButton
s. They also must have their own implementation to some common functions (mainlybind()
that will be called inRecyclerView
’sonBindViewHolder()
).
My question: how do I extract those common ViewHolder
fields?
- interface - cannot store state unless it’s abstract or with provided
get()
implementation. But I cannot callfindViewById(R.id.widgetId)
in the interface - abstract class - cannot be instantiated, that’s problematic with requirement 1.
- class - works but it should never be instantiated, only its children.
By process of elimination, the individual ViewHolder
s use both interface (abstract functions) and a class (common properties). Is there a more elegant solution?