Skip to content

Instantly share code, notes, and snippets.

@cutiko
Created July 20, 2019 02:37
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save cutiko/943c60cd5170e79cbd9b3aa982316e32 to your computer and use it in GitHub Desktop.
Adapter template for Android Studio
#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.RecyclerView;
#set($model = $NAME.replace('sAdapter', ''))
#set($holder = $model+'Holder')
#set($item = $model.toLowerCase())
#set($list = $item + 's')
public class ${NAME} extends RecyclerView.Adapter<${NAME}.${holder}> {
private final List<$model> $list = new ArrayList<>();
@NonNull
@Override
public $holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_${item}, parent, false);
return new ${holder}(view);
}
@Override
public void onBindViewHolder(@NonNull $holder holder, int position) {
$model $item = ${list}.get(position);
}
@Override
public int getItemCount() {
return ${list}.size();
}
class ${holder} extends RecyclerView.ViewHolder {
${holder}(@NonNull View itemView) {
super(itemView);
}
}
}
#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.RecyclerView
#set($model = $NAME.replace('sAdapter', ''))
#set($holder = $model+'Holder')
#set($item = $model.toLowerCase())
#set($list = $item + 's')
class ${NAME} : RecyclerView.Adapter<$holder>() {
private val $list = arrayListOf<$model>()
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): $holder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.list_item_$item, parent, false)
return $holder(view)
}
override fun onBindViewHolder(holder: $holder, position: Int) {
val $item = ${list}[position]
}
override fun getItemCount(): Int = ${list}.size
inner class ${holder}(itemView: View) : RecyclerView.ViewHolder(itemView) {
init {
}
}
}
@Allan-Nava
Copy link

Is there anyway to import the template from github ?

@cutiko
Copy link
Author

cutiko commented Apr 19, 2023

@Allan-Nava these templates are supposed to be used as custom templates in Android Studio. Try to follow this instruction https://factory.dev/blog/templates-android-studio

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