Skip to content

Instantly share code, notes, and snippets.

@kota1921
Created November 21, 2017 09:04
Show Gist options
  • Save kota1921/ce36d2971ce54ff1cc4e897f6f0ff80e to your computer and use it in GitHub Desktop.
Save kota1921/ce36d2971ce54ff1cc4e897f6f0ff80e to your computer and use it in GitHub Desktop.
Common RecyclerView adapter
public class ManagerContactAdapter extends RecyclerView.Adapter<ManagerContactAdapter.VH>{
private List<Manager> managers = new ArrayList<>();
private OnClickDeleteContactListener listener;
public ManagerContactAdapter(OnClickDeleteContactListener listener) {
this.listener = listener;
}
public void setItems(List<Manager> managers){
this.managers.clear();
this.managers.addAll(managers);
notifyDataSetChanged();
}
@Override
public VH onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_user_contact, parent, false);
return new VH(view);
}
@Override
public void onBindViewHolder(VH holder, int index) {
holder.bind(managers.get(index), index +1);
}
@Override
public int getItemCount() {
return managers.size();
}
class VH extends RecyclerView.ViewHolder {
@BindView(R.id.profile_contacts_item_index_number) TextView indexNumberTextView;
@BindView(R.id.profile_contacts_item_name) EditText profileItemName;
@BindView(R.id.profile_contacts_item_position) EditText profilePosition;
@BindView(R.id.profile_contacts_item_phone) EditText profilePhone;
@BindView(R.id.profile_contacts_item_email) EditText profileEmail;
@BindView(R.id.item_profile_contact_delete_contact) ImageView deleteButton;
VH(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
public void bind(Manager manager, int pos){
String indexNumber = itemView.getContext().getString(R.string.contact_index_number);
indexNumber = String.format(indexNumber, String.valueOf(pos));
indexNumberTextView.setText(indexNumber);
profileItemName.setText(manager.getName());
profilePosition.setText(manager.getJobPosition());
profilePhone.setText(manager.getPhoneNumber());
profileEmail.setText(manager.getEmail());
deleteButton.setOnClickListener(v -> listener.onDeleteClick(manager));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment