Created
April 3, 2020 11:53
-
-
Save mbobiosio/35e8d0b77d9ca75ce00008def82612ae to your computer and use it in GitHub Desktop.
RecyclerViewAdapter live template for Android Studio with androidx imports, ButterKnife and notifyDataSetChanged();
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.content.Context; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import androidx.recyclerview.widget.RecyclerView; | |
import java.util.ArrayList; | |
import butterknife.ButterKnife; | |
#parse("File Header.java") | |
public class ${NAME} extends RecyclerView.Adapter<${NAME}.${VIEWHOLDER_CLASS}> { | |
private static final String TAG = ${NAME}.class.getSimpleName(); | |
private Context mContext; | |
private ArrayList<${LIST_MODEL}> mDataList; | |
public ${NAME}() { | |
mDataList = new ArrayList<>(); | |
} | |
public void setData(ArrayList<${LIST_MODEL}> dataList) { | |
dataList.clear(); | |
dataList.addAll(dataList); | |
notifyDataSetChanged(); | |
} | |
@Override | |
public ${VIEWHOLDER_CLASS} onCreateViewHolder(ViewGroup parent, int viewType) { | |
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.${LAYOUT_RES_ID}, parent, false); | |
mContext = parent.getContext(); | |
return new ${VIEWHOLDER_CLASS}(view); | |
} | |
@Override | |
public void onBindViewHolder(${VIEWHOLDER_CLASS} holder, int position) { | |
${LIST_MODEL} data = mDataList.get(position); | |
holder.set(data); | |
} | |
@Override | |
public int getItemCount() { | |
if (mDataList == null){ | |
return 0; | |
} | |
return mDataList.size(); | |
} | |
public class ${VIEWHOLDER_CLASS} extends RecyclerView.ViewHolder { | |
public ${VIEWHOLDER_CLASS}(View itemView) { | |
super(itemView); | |
ButterKnife.bind(this, itemView); | |
} | |
public void set(${LIST_MODEL} dataList) { | |
//UI setting code | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment