Các nội dung chính
Link tải full mã nguồn
https://github.com/maicuongtho/SOT397-MobileAppDev/tree/main/RecycleViewExample
Lớp Country
package vn.edu.tinhoc123.recycleviewexample; public class Country { private String countryName; private String countryFlag; private int population; // Constructor // getters, setters }
Lớp Country Recyclview Adaper
package vn.edu.tinhoc123.recycleviewexample; import android.annotation.SuppressLint; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; import androidx.annotation.NonNull; import androidx.recyclerview.widget.RecyclerView; import java.util.List; public class CountryRVAdapter extends RecyclerView.Adapter { List<Country> lstDataSource; public CountryRVAdapter(List<Country> lstDataSource) { this.lstDataSource = lstDataSource; } // Item view hoder class public final class CountryItemViewHoder extends RecyclerView.ViewHolder implements View.OnClickListener { ImageView flagView; TextView countryNameView; TextView populationView; public int position; public CountryItemViewHoder(View itemView) { super(itemView); itemView.setOnClickListener(this); flagView =(ImageView) itemView.findViewById(R.id.imageViewFlag); countryNameView =(TextView) itemView.findViewById(R.id.textViewCountryName); populationView = (TextView) itemView.findViewById(R.id.textViewPopulation); } @Override public void onClick(View view) { // Get the clicked item position int clickedPosition = getAdapterPosition(); // Get the corresponding Ctrounty object Country country = lstDataSource.get(clickedPosition); // Perform the desired action for the clicked item Toast.makeText(view.getContext(), "Clicked: " + country.getCountryName(), Toast.LENGTH_SHORT).show(); } } @NonNull @Override public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.country_item_layout, parent, false); CountryItemViewHoder vh = new CountryItemViewHoder(v); return vh; } @Override public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, @SuppressLint("RecyclerView") int position) { CountryItemViewHoder viewHolder = (CountryItemViewHoder) holder; viewHolder.position = position; Country country = lstDataSource.get(position); ((CountryItemViewHoder) holder).countryNameView.setText(country.getCountryName()); ((CountryItemViewHoder) holder).flagView.setImageResource( getMipmapResId(holder.itemView, country.getCountryFlag())); ((CountryItemViewHoder) holder).populationView.setText(String.valueOf(country.getPopulation()) ); } private int getMipmapResId(View itemView, String mipmapName) { String packageName = itemView.getContext().getPackageName(); return itemView.getResources().getIdentifier(mipmapName, "mipmap", packageName); } @Override public int getItemCount() { return lstDataSource.size(); } }
Thiết kế bố cục cho mỗi Item, tệp: country_item_layout.xml
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="100dp" > <ImageView android:id="@+id/imageViewFlag" android:layout_width="90dp" android:layout_height="90dp" android:layout_marginStart="5dp" android:layout_marginTop="5dp" android:layout_marginBottom="5dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" tools:srcCompat="@tools:sample/avatars" /> <TextView android:id="@+id/textViewCountryName" android:layout_width="0dp" android:layout_height="50dp" android:layout_marginStart="5dp" android:layout_marginTop="5dp" android:layout_marginEnd="5dp" android:text="Ten Quoc Gia o day" android:textStyle="bold" android:textSize="18dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.0" app:layout_constraintStart_toEndOf="@+id/imageViewFlag" app:layout_constraintTop_toTopOf="parent" /> <TextView android:id="@+id/textViewPopulation" android:layout_width="0dp" android:layout_height="38dp" android:layout_marginStart="5dp" android:layout_marginTop="2dp" android:layout_marginEnd="5dp" android:text="Population: 1000000" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toEndOf="@+id/imageViewFlag" app:layout_constraintTop_toBottomOf="@+id/textViewCountryName" /> </androidx.constraintlayout.widget.ConstraintLayout>
Xem full video hướng dẫn tại đây, hoặc xem bài về tùy biến Listview ở liên kết này https://youtu.be/Gq6XsOSQD7w?si=uBVKMQ82Tgxro0BI