RecyclerView usando View Binding
Código del ejercicio que muestra los contactos en un RecyclerView usando View Binding
Contacto
public class Contact { private String mName; private boolean mOnline; public Contact(String name, boolean online) { mName = name; mOnline = online; } public String getName() { return mName; } public boolean isOnline() { return mOnline; } private static int lastContactId = 0; public static ArrayList<Contact> createContactsList(int numContacts) { ArrayList<Contact> contacts = new ArrayList<Contact>(); for (int i = 1; i <= numContacts; i++) { contacts.add(new Contact("Person " + ++lastContactId, i <= numContacts / 2)); } return contacts; } }
ContactsAdapter
/** * Adaptador que gestiona una lista de Contactos */ public class ContactsAdapter extends RecyclerView.Adapter<ContactsAdapter.ContactViewHolder> { private List<Contact> list; private OnItemClickListener listener; //Interfaz para el evento onClick de un elemento del RecyclerView public interface OnItemClickListener { void onItemClick(Contact Contact); } //Constructor que se le pasa la lista de notas y el listener public ContactsAdapter(List<Contact> list, OnItemClickListener listener) { this.list = list; this.listener=listener; } @Override public ContactViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { // return new ContactViewHolder(ItemContactBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false)); LayoutInflater inflater = LayoutInflater.from(parent.getContext()); ItemContactBinding binding = ItemContactBinding.inflate(inflater, parent, false); return new ContactViewHolder(binding); } //Método que vincula cada dato con la vista @Override public void onBindViewHolder(ContactViewHolder holder, int position) { Contact Contact = list.get(position); holder.binding.contactName.setText(Contact.getName()); holder.binding.messageButton.setText(Contact.isOnline() ? "Message" : "Offline"); holder.binding.messageButton.setEnabled(Contact.isOnline()); holder.bind(list.get(position), listener); } @Override public int getItemCount() { return list.size(); } /** * Clase ViewHolder */ public class ContactViewHolder extends RecyclerView.ViewHolder { ItemContactBinding binding; //public TextView title, content; public ContactViewHolder(ItemContactBinding b) { //super(view); //title = view.findViewById(R.id.title); //content = view.findViewById(R.id.content); super(b.getRoot()); binding = b; } public void bind(final Contact Contact, final OnItemClickListener listener) { itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { listener.onItemClick(Contact); } }); } } }
MainActivity
public class MainActivity extends AppCompatActivity implements View.OnClickListener { ArrayList<Contact> contacts; RecyclerView rvContacts; Button addMoreButton; ActivityMainBinding binding; ContactsAdapter adapter; ContactsAdapter.OnItemClickListener listener; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); binding = ActivityMainBinding.inflate(getLayoutInflater()); setContentView(binding.getRoot()); // Initialize contacts contacts = Contact.createContactsList(20); // Create adapter passing in the sample user data listener = new ContactsAdapter.OnItemClickListener() { @Override public void onItemClick(Contact contact) { Toast.makeText(MainActivity.this, "Contacto seleccionado: " + contact.getName(), Toast.LENGTH_LONG).show(); } }; adapter = new ContactsAdapter(contacts, listener); // Attach the adapter to the recyclerview to populate items binding.rvContacts.setAdapter(adapter); // Set layout manager to position the items binding.rvContacts.setLayoutManager(new LinearLayoutManager(this)); binding.addContact.setOnClickListener(this); //adapter.setOnItemClickListener(this); // That's all! } @Override public void onClick(View view) { // record this value before making any changes to the existing list int curSize = adapter.getItemCount(); // Add a new contact // contacts.add(0, new Contact("Barney", true)); // Notify the adapter that an item was inserted at position 0 // adapter.notifyItemInserted(0); contacts.add(curSize, new Contact("Barney", true)); adapter.notifyItemInserted(curSize); } }
Repositorio en GitLab: Contactos
Deja una respuesta
Lo siento, debes estar conectado para publicar un comentario.