This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class RecyclerAdapter<M : RecyclerItem>( | |
diffCallback: DiffUtil.ItemCallback<M>, | |
@LayoutRes val placeholderId: Int?, | |
private val clickListener: Listener? = null | |
) : PagedListAdapter<M, ViewHolder<M>> | |
(diffCallback) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
override fun getItemViewType(position: Int) | |
= requireNotNull( | |
getItem(position)?.layoutId ?: placeholderId | |
){ | |
"item at $position is null" | |
} | |
override fun onCreateViewHolder( | |
parent: ViewGroup, | |
viewType: Int) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
inner class ViewHolder( | |
val binding: ViewDataBinding | |
): RecyclerView.ViewHolder(binding.root) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
override fun onBindViewHolder( | |
holder: ViewHolder, | |
position: Int | |
){ | |
getItem(position)?.run(holder.binding::bind) | |
} | |
} //end of RecyclerAdapter | |
private fun ViewDataBinding.bind( |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data class User( | |
... | |
) : RecyclerItem { | |
override val layoutId: Int | |
get() = R.layout.list_item_user | |
override val variableId: Int | |
get() = BR.user | |
override val dataToBind: Any |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MainFragment: | |
Fragment(R.layout.fragment_main) { | |
private val adapter by lazy { | |
RecyclerAdapter( | |
RecyclerItem.diffCallback<User>(), | |
R.layout.list_item_user_placeholder | |
) { _, _, item -> | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data class Comment( | |
... | |
) : RecyclerItem { | |
... | |
} | |
data class ComplexModel( | |
val model: SomeModel | |
... | |
) : RecyclerItem { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct User { | |
var name, email: String? | |
var likeKiwi = false | |
var travel = false | |
var hiking = false | |
var reading = false | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let mapping = [ | |
\User.name: nameField, | |
\User.email: gmailField, | |
\User.address: addressField, | |
\User.travel: travelBtn, | |
\User.hiking: hikingBtn, | |
\User.reading: readingBtn, | |
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension DataBinding { | |
public func update<Value>(_ keyPath: WritableKeyPath<M, Value>, _ value: Value) { | |
model[keyPath: keyPath] = value | |
_keyPathViews[keyPath]?.wrappedView?.setValue(value) | |
} | |
} |