Giới thiệu
RecyclerView là một ViewGroup, tương tự như ListView, nhưng nó được xây dựng để cho lập trình viên có thể kiểm soát, tùy chỉnh sâu hơn, hiệu suất cao hơn ListView.
Đặc trưng
* Cần một LayoutManager riêng, do đó RecyclerView có thể giống như ListView,* GirdView, cuộn ngang, dọc.* Sử dụng một ViewHolder để tái sử dụng View, tránh tạo ra View mới. Do vậy khi danh dách các Item rất nhiều thì cuộn vẫn rất mượt.* Hỗ trợ Animation tốt hơn ListView.* Không hỗ trợ setOnItemClick hay setOnItemLongCLick, lập trình viên phải tự custom
Sử dụng
Import thư viện cần thiết:implementation 'com.android.support:recyclerview-v7:28.0.0'
Chúng ta sẽ đi xây dựng 1 ứng dụng hiển thị danh sách các liên lạc:* Tên* Số điện thoại* Email
Xây dựng Layout
activity_main.xml
1 | <?xml version="1.0" encoding="utf-8"?> |

fragment_contacts.xml
1 | <?xml version="1.0" encoding="utf-8"?> |

item_layout.xml1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:layout_marginBottom="10dp"
android:layout_marginTop="5dp"
android:id="@+id/item_layout">
<ImageView
android:id="@+id/image_avatar"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:src="@drawable/ic_account"
android:scaleType="fitCenter"/>
<LinearLayout
android:id="@+id/info"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="4"
android:orientation="vertical">
<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Name: "
android:textColor="@color/colorBlack"
android:textSize="20sp"
android:textStyle="bold"/>
<TextView
android:id="@+id/phone_number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Number: "
android:textSize="17sp"/>
<TextView
android:id="@+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Email: "
android:textSize="17sp"/>
</LinearLayout>
</LinearLayout>

Data collection
ContactCollection.kt1
class ContactCollection (val name: String, val phoneNumber: String, val email: String)
Xây dựng interface cho event click
OnClickListener.kt
1 | interface OnClickListener { |
Định nghĩa hành động xẩy ra khi click
1 | private val onClickListener = object : OnClickListener { |
Adapter
* onCreateViewHolder: Nạp Layout cần hiện thị cho 1 item, trả về 1 View* MyViewHolder: Lớp quản nắm giữ View* getItemCount: Trả về kích cỡ dữ liệu, cũng là kích cỡ của RecyclerView* onBindViewHolder: Bind data tới View, hành động với View ở đây.
RecyclerViewAdapter.kt1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39class RecyclerViewAdapter(private val context: Context, private val listContacts: ArrayList<ContactCollection>, private val onClickListener: OnClickListener)
: RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder>() {
override fun onCreateViewHolder(p0: ViewGroup, p1: Int): MyViewHolder {
val inflater = LayoutInflater.from(context)
val view = inflater.inflate(R.layout.item_layout, p0, false)
return MyViewHolder(view)
}
override fun getItemCount(): Int {
return listContacts.size
}
override fun onBindViewHolder(p0: MyViewHolder, p1: Int) {
val view = p0.itemView
val contact = listContacts[p1]
// Set Data to View
view.apply {
name.text = contact.name
phone_number.text = contact.phoneNumber
email.text = contact.email
}
// Event click
view.setOnClickListener {
onClickListener.OnItemClick(p1)
}
view.setOnLongClickListener {
onClickListener.onItemLongClick(p1)
true
}
}
inner class MyViewHolder(view: View): RecyclerView.ViewHolder(view)
}
URL QR