Forget about writing RecyclerView adapters!
Suppose you want to show multiple types of items in a RecyclerView, for example posts with headers and ads
class PostHolder(
parent: ViewGroup,
onClick: (Post) -> Unit
) : AnyHolder<Post>(parent, R.layout.item) {
private val textTitle = itemView.findViewById<TextView>(R.id.textTitle)
init {
textTitle.setOnClickListener { onClick(currentItem) }
}
override fun onBind(item: Post) {
textTitle.text = item.message
}
}
data class Post(val message: String)
val adapter = AnyAdapter()
.map { PostHolder(it, ::onPostClick) }
.map { HeaderHolder(it) }
.map { AdHolder(it) }
recyclerView.adapter = adapter
adapter.submitList(listOf("Header", Post("Hello!"), Post("Bye!"), Ad()))
Copyright 2022 Artyom Mironov
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.