Last active
August 20, 2020 16:22
-
-
Save Schadenfeude/b1eb9e77421e1948d65a206eb8e60bc8 to your computer and use it in GitHub Desktop.
Live Template for RecyclerView Adapter with ListAdapter
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 android.view.View | |
import android.view.ViewGroup | |
import androidx.recyclerview.widget.DiffUtil | |
import androidx.recyclerview.widget.ListAdapter | |
import androidx.recyclerview.widget.RecyclerView | |
import com.dronesense.pilotkit.common.extension.inflate | |
#parse("File Header.java") | |
internal class ${NAME}(private val onClick: (${Model_Class}) -> Unit) : ListAdapter<${Model_Class}, ${Class_Object_Name}ViewHolder>(${Class_Object_Name}ItemCallback) { | |
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ${Class_Object_Name}ViewHolder = | |
${Class_Object_Name}ViewHolder(parent.inflate(R.layout.${Layout_Name}), onClick) | |
override fun onBindViewHolder(holder: ${Class_Object_Name}ViewHolder, position: Int) = holder.bind(getItem(position)) | |
} | |
internal class ${Class_Object_Name}ViewHolder(view: View, private val onClick: (${Model_Class}) -> Unit) : RecyclerView.ViewHolder(view) { | |
private val sampleView by lazy { view.findViewById<View>(R.id.sampleView) } | |
fun bind(item: ${Model_Class}) { | |
itemView.setOnClickListener { onClick.invoke(item) } | |
} | |
} | |
internal object ${Class_Object_Name}ItemCallback : DiffUtil.ItemCallback<${Model_Class}>() { | |
override fun areItemsTheSame(first: ${Model_Class}, second: ${Model_Class}): Boolean { | |
return first == second | |
} | |
override fun areContentsTheSame(first: ${Model_Class}, second: ${Model_Class}): Boolean { | |
return first == second | |
} | |
} |
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
//This is what the parent.inflate() extension function does | |
fun ViewGroup.inflate(@LayoutRes layoutId: Int): View { | |
return LayoutInflater.from(context).inflate(layoutId, this, false) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
More info on how to set this up here.