This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
| import androidx.recyclerview.widget.DiffUtil | |
| interface RecyclerItem { | |
| val layoutId: Int | |
| val variableId: Int | |
| val dataToBind: Any | |
| val id: Int |
This file contains hidden or 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 IntViewModelFactory(val application: Application, val int: Int) : | |
| ViewModelProvider.NewInstanceFactory() { | |
| @Suppress("UNCHECKED_CAST") | |
| override fun <T : ViewModel?> create(modelClass: Class<T>) = | |
| IntViewModel(application, int) as T | |
| } | |
| class StringViewModelFactory(val application: Application, val string: String) : | |
| ViewModelProvider.NewInstanceFactory() { |
This file contains hidden or 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
| import androidx.lifecycle.ViewModel | |
| import androidx.lifecycle.ViewModelProvider | |
| import kotlin.reflect.full.primaryConstructor | |
| class AndroidViewModelFactory(private vararg val args: Any) : | |
| ViewModelProvider.NewInstanceFactory() { | |
| override fun <T : ViewModel> create(modelClass: Class<T>) = | |
| modelClass.kotlin.primaryConstructor?.call(*args) | |
| ?: throw IllegalArgumentException("$modelClass primaryConstructor is null") |