Meu Adapter:
public class MyFirstAdapter extends RecyclerView.Adapter<MyFirstAdapter.MyFirstViewHolder> {
private List<Contato> contatoList;
private MyOnItemClickListener myOnItemClickListener;
private View itemView;
@NonNull
@Override
public MyFirstViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, final int viewType) {
itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.contatos_layout, viewGroup,false);
itemView.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
if (myOnItemClickListener != null){
TextView txt = view.findViewById(R.id.contato_nome);
myOnItemClickListener.myOnItemClick(txt.getText().toString());
}
}
});
final MyFirstViewHolder holder = new MyFirstViewHolder(itemView);
itemView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
contatoList.remove(holder.position);
notifyDataSetChanged();
return true;
}
});
return holder;
}
@Override
public void onBindViewHolder(@NonNull MyFirstViewHolder myFirstViewHolder, final int position) {
myFirstViewHolder.onBind(contatoList.get(position), position);
}
@Override
public int getItemCount() {
return contatoList.size();
}
class MyFirstViewHolder extends RecyclerView.ViewHolder{
ImageView contatoFoto;
TextView contatoNome;
TextView contatoRamal;
TextView contatoEmail;
TextView contatoSetor;
int position;
public MyFirstViewHolder(@NonNull View itemView) {
super(itemView);
contatoFoto = itemView.findViewById(R.id.contato_foto);
contatoNome = itemView.findViewById(R.id.contato_nome);
contatoRamal = itemView.findViewById(R.id.contato_ramal);
contatoEmail = itemView.findViewById(R.id.contato_email);
contatoSetor = itemView.findViewById(R.id.contato_setor);
}
public void onBind(Contato contato, int pos){
position = pos;
contatoNome.setText(contato.getNome());
contatoRamal.setText(Html.fromHtml(contato.getRamal()));
contatoFoto.setImageResource(contato.getFoto());
contatoEmail.setText(contato.getEmail());
contatoSetor.setText(contato.getSetor());
}
}
public interface MyOnItemClickListener{
void myOnItemClick(String nome);
}
public MyFirstAdapter(List<Contato> contatoList){
this.contatoList = contatoList;
}
public void setMyOnItemClickListener(MyOnItemClickListener myOnItemClickListener){
this.myOnItemClickListener = myOnItemClickListener;
}
}