Thực hành sử dụng RecyclerView trong android java p3

Code: Default | Auth: 03cd82

Update: Dùng cardview để trình bày các item

Bạn thay thế layout của custom_item_layout.xml bằng layout dưới đây. Cơ bản là có 1 LinearLayout bọc ngoài rồi cho cái cardView vào trong để bao gói, bên trong đó tiếp tục cho 1 cái layout để trình bày các phần tử con.

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

   xmlns:app="http://schemas.android.com/apk/res-auto"

   android:orientation="vertical"

   android:layout_width="match_parent"

   android:layout_height="wrap_content">

   <com.google.android.material.card.MaterialCardView

       android:id="@+id/card_view"

       android:layout_width="match_parent"

       android:layout_height="wrap_content"

       app:cardBackgroundColor="@color/teal_200"

       android:layout_margin="8dp"

       app:cardCornerRadius="10dp"

       android:clickable="true"

       android:focusable="true"

       android:longClickable="true"

       app:cardElevation="5dp"

       >

       <LinearLayout

           android:layout_width="match_parent"

           android:layout_height="wrap_content"

           android:orientation="vertical"

           android:padding="10dp"

           >

           <TextView

               android:id="@+id/tv_id"

               android:textColor="#BC3636"

               android:layout_width="match_parent"

               android:layout_height="wrap_content"/>

 

           <TextView

               android:id="@+id/tv_name"

               android:textColor="#2196F3"

               android:layout_width="match_parent"

               android:layout_height="wrap_content"/>

       </LinearLayout>

   </com.google.android.material.card.MaterialCardView>

</LinearLayout>

 

Một số thuộc tính của CardView:

app:cardBackgroundColor="@color/teal_200" ==> Màu nền cho cardview
android:layout_margin="8dp" ===> tạo khoảng cách giữa các phần tử
app:cardCornerRadius="10dp" ==> tạo bo tròn góc, cái này thì nên có padding kèm theo ở layout chứa trong cardview.
android:clickable="true" ==> Cho phép bấm chọn hay không, mặc định là false, nó sẽ giống textview
android:focusable="true" ==>Cho phép nhận focus thì nó sẽ giống vai trò của nút bấm
android:longClickable="true" ==> Cho phép bấm giữ lâu
app:cardElevation="5dp" ==> Khoảng cách chiều sâu (trục z trong hệ tọa độ), sẽ nhìn thấy đổ bóng