Last active
July 10, 2021 01:06
-
-
Save WilderMinds/d2a7084900c378e525576fb3c748bd2b to your computer and use it in GitHub Desktop.
Android Studio File Template Samples
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
#* | |
Thus begins a multi-line comment, the Velocity Templating Engine will | |
ignore it. | |
This template that can be used to quickly generate a ListAdapter and | |
DiffUtil to be used with your recyclerviews. | |
Setting imports are not essential because after the file is generated from | |
the template, the developer can always include the missing imports manually. | |
*# | |
#if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "")package ${PACKAGE_NAME}#end | |
import android.view.LayoutInflater | |
import android.view.View | |
import android.view.ViewGroup | |
import androidx.recyclerview.widget.DiffUtil | |
import androidx.recyclerview.widget.ListAdapter | |
import androidx.recyclerview.widget.RecyclerView | |
class ${NAME} : ListAdapter<${Data_Model}, ${NAME}.ItemViewHolder>(${Data_Model}DiffCallback()) { | |
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemViewHolder { | |
val view = LayoutInflater.from(parent.context) | |
.inflate(R.layout.${Item_Layout_Name}, parent, false) | |
return ItemViewHolder(view) | |
} | |
override fun onBindViewHolder(holder: ItemViewHolder, position: Int) { | |
getItem(position)?.let { | |
holder.bind(it) | |
} | |
} | |
class ItemViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { | |
fun bind(item: ${Data_Model}) = with(itemView) { | |
// Set data to your item view here | |
setOnClickListener { | |
onClick(item) | |
} | |
} | |
private fun onClick(item: ${Data_Model}) { | |
// Handle item click actions | |
} | |
} | |
} | |
class ${Data_Model}DiffCallback : DiffUtil.ItemCallback<${Data_Model}>() { | |
override fun areItemsTheSame(oldItem: ${Data_Model}, newItem: ${Data_Model}): Boolean { | |
return oldItem.id == newItem.id | |
} | |
override fun areContentsTheSame(oldItem: ${Data_Model}, newItem: ${Data_Model}): Boolean { | |
return oldItem == newItem | |
} | |
} |
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
#* | |
Thus begins a multi-line comment, the Velocity Templating Engine will | |
ignore it. | |
This template that can be used to quickly generate converter classes | |
which are use when storing a nested list of entities in a parent entity | |
schema. To be used with room db. | |
Setting imports are not essential because after the file is generated from | |
the template, the developer can always include the missing imports manually. | |
*# | |
#if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "")package ${PACKAGE_NAME}#end | |
import androidx.room.TypeConverter | |
import com.google.gson.Gson | |
import com.google.gson.reflect.TypeToken | |
class ${NAME} { | |
companion object { | |
@TypeConverter | |
@JvmStatic | |
fun toList(strValue: String) : List<${Model_Class}> { | |
val listType = object: TypeToken<List<${Model_Class}>>(){}.type | |
return Gson().fromJson(string, listType) | |
} | |
@TypeConverter | |
@JvmStatic | |
fun fromList(list: List<${Model_Class}>) : String { | |
return Gson().toJson(list) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment