Last active
December 25, 2021 12:48
-
-
Save karntrehan/a103dbdc215f1927862640ec2afb1992 to your computer and use it in GitHub Desktop.
Kotlin ListAdapter template for RecyclerView in Android
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
#if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "")package ${PACKAGE_NAME}#end | |
import androidx.recyclerview.widget.RecyclerView | |
import android.view.LayoutInflater | |
import android.view.View | |
import android.view.ViewGroup | |
import android.view.View.OnClickListener | |
import androidx.recyclerview.widget.DiffUtil | |
import androidx.recyclerview.widget.ListAdapter | |
#parse("File Header.java") | |
class ${NAME}(private val interaction: Interaction? = null) : | |
ListAdapter<${Model_Class}, ${NAME}.${ViewHolder_Class}>(${Model_Class}DC()) { | |
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = ${ViewHolder_Class}( | |
LayoutInflater.from(parent.context) | |
.inflate(R.layout.${Item_Layout_ID}, parent, false), interaction | |
) | |
override fun onBindViewHolder(holder: ${ViewHolder_Class}, position: Int) = holder.bind(getItem(position)) | |
fun swapData(data: List<${Model_Class}>) { | |
submitList(data.toMutableList()) | |
} | |
inner class ${ViewHolder_Class}(itemView: View, | |
private val interaction: Interaction?) : RecyclerView.ViewHolder(itemView), OnClickListener { | |
init { | |
itemView.setOnClickListener(this) | |
} | |
override fun onClick(v: View?) { | |
if (adapterPosition == RecyclerView.NO_POSITION) return | |
val clicked = getItem(adapterPosition) | |
} | |
fun bind(item: ${Model_Class}) = with(itemView) { | |
// TODO: Bind the data with View | |
} | |
} | |
interface Interaction { | |
} | |
private class ${Model_Class}DC : DiffUtil.ItemCallback<${Model_Class}>() { | |
override fun areItemsTheSame( | |
oldItem: ${Model_Class}, | |
newItem: ${Model_Class} | |
): Boolean { | |
TODO( | |
"not implemented" | |
) | |
} | |
override fun areContentsTheSame( | |
oldItem: ${Model_Class}, | |
newItem: ${Model_Class} | |
): Boolean { | |
TODO( | |
"not implemented" | |
) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Writeup: https://proandroiddev.com/listadapter-template-for-recyclerview-in-android-7e5003cc4367