其他
RecyclerView | 在 RecyclerView 中使用 header 快人一步
创建 header 布局
1<!-- Copyright 2019 Google LLC.
2 SPDX-License-Identifier: Apache-2.0 -->
3
4<LinearLayout
5 xmlns:android="http://schemas.android.com/apk/res/android"
6 android:orientation="vertical"
7 android:layout_width="match_parent"
8 android:layout_height="wrap_content">
9
10 <TextView
11 android:id="@+id/header_text"
12 android:layout_width="wrap_content"
13 android:layout_height="wrap_content"
14 android:layout_gravity="center"
15 android:text="@string/flower_finder"
16 android:textAppearance="?attr/textAppearanceHeadline3" />
17
18 <TextView
19 android:id="@+id/flower_number_text"
20 android:layout_width="wrap_content"
21 android:layout_height="wrap_content"
22 android:layout_gravity="center"
23 android:textAppearance="?attr/textAppearanceHeadline6" />
24
25 <TextView
26 android:id="@+id/flower_text"
27 android:layout_width="wrap_content"
28 android:layout_height="wrap_content"
29 android:layout_gravity="center"
30 android:text="@string/flower_string"
31 android:textAppearance="?attr/textAppearanceHeadline6" />
32
33</LinearLayout>
创建 HeaderAdapter 和 HeaderViewHolder
1<!-- Copyright 2019 Google LLC.
2 SPDX-License-Identifier: Apache-2.0 -->
3
4class HeaderAdapter: RecyclerView.Adapter<RecyclerView.ViewHolder>(){
5
6}
1<!-- Copyright 2019 Google LLC.
2 SPDX-License-Identifier: Apache-2.0 -->
3
4class HeaderViewHolder(view: View) : RecyclerView.ViewHolder(view){
5 private val flowerNumberTextView: TextView = itemView
6 .findViewById(R.id.flower_number_text)
7
8 fun bind(flowerCount: Int) {
9 flowerNumberTextView.text = flowerCount.toString()
10 }
11}
1<!-- Copyright 2019 Google LLC.
2 SPDX-License-Identifier: Apache-2.0 -->
3
4class HeaderAdapter: RecyclerView.Adapter<HeaderAdapter.HeaderViewHolder>() {
5
6}
onCreateViewHolder() 负责填充视图并且返回 HeaderViewHolder getItemCount() 仅返回数值 1,因为仅有一个 Header 元素 onBindViewHolder() 将数据绑定到 Header 上
1<!-- Copyright 2019 Google LLC.
2 SPDX-License-Identifier: Apache-2.0 -->
3
4override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): HeaderViewHolder {
5 val view = LayoutInflater.from(parent.context)
6 .inflate(R.layout.header_item, parent, false)
7 return HeaderViewHolder(view)
8}
9
10override fun onBindViewHolder(holder: HeaderViewHolder, position: Int) {
11 holder.bind(flowerCount)
12}
13
14override fun getItemCount(): Int {
15 return 1
16}
在 Activity 类中使用 ConcatAdapter
1<!-- Copyright 2019 Google LLC.
2 SPDX-License-Identifier: Apache-2.0 -->
3
4val headerAdapter = HeaderAdapter()
5val flowersAdapter = FlowersAdapter { flower -> adapterOnClick(flower) }
ConcatAdapter https://developer.android.google.cn/reference/androidx/recyclerview/widget/ConcatAdapter
1<!-- Copyright 2019 Google LLC.
2 SPDX-License-Identifier: Apache-2.0 -->
3
4val concatAdapter = ConcatAdapter(headerAdapter, flowersAdapter)
5recyclerView.adapter = concatAdapter
下一步
关于 Header 的完整示例代码,请查阅:
实战 | 认识 RecyclerView RecyclerView | 在 RecyclerView 中使用 ListAdapter RecyclerView | 处理 RecyclerView 中的点击事件
免费中文系列课程下载
系统地学习使用 Kotlin 进行 Android 开发
☟ 即刻了解课程详情 ☟
推荐阅读