Skip to content

Instantly share code, notes, and snippets.

@haseebpvt
Created October 22, 2020 15:31
Show Gist options
  • Select an option

  • Save haseebpvt/0ed52600ce099c447266189fe1918b92 to your computer and use it in GitHub Desktop.

Select an option

Save haseebpvt/0ed52600ce099c447266189fe1918b92 to your computer and use it in GitHub Desktop.
Intellij live template to create ListAdapter
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
class $CLASS_NAME$() : ListAdapter<$MODEL_CLASS$, $VIEW_HOLDER$>($DIFF_UTIL_CLASS$()) {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): $VIEW_HOLDER$ {
return $VIEW_HOLDER$(
$ITEM_BINDING$.inflate(LayoutInflater.from(parent.context), parent, false)
)
}
override fun onBindViewHolder(holder: $VIEW_HOLDER$, position: Int) {
holder.bind(getItem(position))
}
}
class $VIEW_HOLDER$(
private val binding: $ITEM_BINDING$
) : RecyclerView.ViewHolder(binding.root) {
fun bind($MODEL_CLASS_VARIABLE_NAME$: $MODEL_CLASS$) {
binding.$MODEL_CLASS_VARIABLE_NAME$ = $MODEL_CLASS_VARIABLE_NAME$
binding.executePendingBindings()
}
}
open class $DIFF_UTIL_CLASS$ : DiffUtil.ItemCallback<$MODEL_CLASS$>() {
override fun areItemsTheSame(oldItem: $MODEL_CLASS$, newItem: $MODEL_CLASS$): Boolean {
return oldItem.$UNIQUE_ITEM$ == newItem.$UNIQUE_ITEM$
}
override fun areContentsTheSame(oldItem: Service, newItem: Service): Boolean {
return oldItem == newItem
}
}
@haseebpvt
Copy link
Author

In Android Studio go to Settings > Editor > Live Template > + (plus button) > Add Live Template

You may click Edit Variables to add default values for the text that is enclosed with tow dollar signs like $THIS$

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment