Saudações caros colegas programadores
Estou estudando algumas coisas de recycler view em android
E no exercicios passado de exemplo eu fiquei confuso sobre a ligação das classes
O código em questão é o seguinte:
package com.cursoandroid.recycleviewmasterextra.model;
public class Email {
public String user;
public String subject;
public String preview;
public String date;
public boolean stared;
public boolean unreaded;
public boolean selected;
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getPreview() {
return preview;
}
public void setPreview(String preview) {
this.preview = preview;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public boolean isStared() {
return stared;
}
public void setStared(boolean stared) {
this.stared = stared;
}
public boolean isUnreaded() {
return unreaded;
}
public void setUnreaded(boolean unreaded) {
this.unreaded = unreaded;
}
public boolean isSelected() {
return selected;
}
public void setSelected(boolean selected) {
this.selected = selected;
}
public static class EmailBuilder {
private String user;
private String subject;
private String preview;
private String date;
private boolean stared;
private boolean unreaded;
private boolean selected;
public EmailBuilder setUser(String user) {
this.user = user;
return this;
}
public EmailBuilder setSubject(String subject) {
this.subject = subject;
return this;
}
public EmailBuilder setPreview(String preview) {
this.preview = preview;
return this;
}
public EmailBuilder setDate(String date) {
this.date = date;
return this;
}
public EmailBuilder setStared(boolean stared) {
this.stared = stared;
return this;
}
public EmailBuilder setUnreaded(boolean unreaded) {
this.unreaded = unreaded;
return this;
}
public EmailBuilder setSelected(boolean selected) {
this.selected = selected;
return this;
}
private EmailBuilder(){}
public static EmailBuilder builder(){
return new EmailBuilder();
}
public Email build(){
Email email = new Email();
email.user = user;
email.subject = subject;
email.preview = preview;
email.date = date;
email.stared = stared;
email.unreaded = unreaded;
email.selected = selected;
return email;
}
}
}
public class Emails {
public static List<Email> fakeEmails(){
return Arrays.asList(
Email.EmailBuilder.builder()
.setUser("Thiaguinho")
.setSubject("Bla bla bla")
.setDate("2 jan")
.build(),
Email.EmailBuilder.builder()
.setUser("Xushow")
.setSubject("miau miau miau miau miau")
.setDate("25 dez")
.build(),
Email.EmailBuilder.builder()
.setUser("Reinan")
.setSubject("bla bla bla")
.setDate("25 dez")
.build(),
Email.EmailBuilder.builder()
.setUser("Gegege")
.setSubject("O meu deus")
.setDate("11 jan")
.build(),
Email.EmailBuilder.builder()
.setUser("Professor")
.setSubject("Sei lá o que e pá e tal?")
.setDate("11 jan")
.setStared(true)
.setUnreaded(true)
.build(),
Email.EmailBuilder.builder()
.setUser("Etec")
.setSubject("Somebody once told me whole world gonna roll me")
.setDate("2 dez")
.setStared(true)
.setUnreaded(true)
.build()
);
}
}
package com.cursoandroid.recycleviewmasterextra;
import...
public class EmailAdapter extends RecyclerView.Adapter<EmailAdapter.EmailViewHolder>{
private final List<Email> emails;
public EmailAdapter (List<Email> emails) {
this.emails = emails;
}
@NonNull
@Override
public EmailViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.email_item, parent, false);
return new EmailViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull EmailViewHolder holder, int position) {
Email email = emails.get(position);
holder.bind(email);
}
@Override
public int getItemCount() {
return emails.size();
}
class EmailViewHolder extends RecyclerView.ViewHolder {
TextView txtUsuario;
TextView txtIcon;
TextView txtSubject;
TextView txtPreview;
TextView txtDate;
ImageView imgStar;
EmailViewHolder(@NonNull View itemView) {
super(itemView);
txtUsuario = itemView.findViewById(R.id.txt_Usuario);
txtIcon = itemView.findViewById(R.id.txt_icon);
txtSubject = itemView.findViewById(R.id.txt_subject);
txtPreview = itemView.findViewById(R.id.txt_preview);
txtDate = itemView.findViewById(R.id.txt_Data);
imgStar = itemView.findViewById(R.id.img_star);
}
void bind(Email email){
int hash = email.getUser().hashCode();
txtIcon.setBackground(oval(Color.rgb(hash, hash / 2,0), txtIcon));
txtIcon.setText(String.valueOf(email.getUser().charAt(0)));
txtUsuario.setText(email.getUser());
txtSubject.setText(email.getSubject());
txtPreview.setText(email.getDate());
txtUsuario.setTypeface(Typeface.DEFAULT, email.isUnreaded() ? Typeface.BOLD : Typeface.NORMAL);
txtSubject.setTypeface(Typeface.DEFAULT, email.isUnreaded() ? Typeface.BOLD : Typeface.NORMAL);
txtDate.setTypeface(Typeface.DEFAULT, email.isUnreaded() ? Typeface.BOLD : Typeface.NORMAL);
imgStar.setImageResource(email.isStared() ? R.drawable.ic_star_border_black_24dp : R.drawable.ic_star);
}
}
private static ShapeDrawable oval(@ColorInt int color, View view){
ShapeDrawable shapeDrawable = new ShapeDrawable(new OvalShape());
shapeDrawable.setIntrinsicHeight(view.getHeight());
shapeDrawable.setIntrinsicWidth(view.getWidth());
shapeDrawable.getPaint().setColor(color);
return shapeDrawable;
}
}
Pra sem bem sincero com vocês, a minha maior duvida é como que o List do adapter pode simplesmente receber como tipo o Email, e não Emails, e mesmo assim continuar a ter acesso a todas as instancias que estão inseridas no Emails.
Perdão pelos blocos de codigos extensos, mas a duvida é mto genuína, já fazem alguns dias que eu tendo entender todas essas relações hahaha, e estão todas deveras abstratas ainda na minha cabeça