Alec's blog

唯纯粹与热爱不可辜负

0%

使用lambda封装可复用的RecyclerView.Adapter

现在App大部分都不止一个列表,而每用一次都写一个适配器继承RecyclerView.Adapter重写它的方法。多次使用列表时,这项工作就变得重复冗余。我们可以使用lambda来封装一个可复用的Adapter。

1
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
class MyAdapter<T>(
@LayoutRes
private val layoutId: Int
) : RecyclerView.Adapter<MyAdapter.MyHolder>() {

private lateinit var onBind: (list: MutableList<T>, holder: MyHolder, position: Int) -> Unit
private var itemClick: ((view: View, position: Int) -> Unit)? = null
private val list: MutableList<T> = arrayListOf()

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyHolder {
val itemView = LayoutInflater.from(parent.context)
.inflate(layoutId, parent, false)
val holder = MyHolder(itemView)
itemView.setOnClickListener {
itemClick?.invoke(it, holder.layoutPosition)
}
return holder
}

override fun onBindViewHolder(holder: MyHolder, position: Int) {
onBind.invoke(list, holder, position)
}

override fun getItemCount(): Int {
return list.size
}

fun setItemClickListener(listener: (view: View, position: Int) -> Unit) {
this.itemClick = listener
}

fun setOnBindListener(listener: (list: MutableList<T>, holder: MyHolder, position: Int) -> Unit) {
this.onBind = listener
}

fun replaceAll(data: List<T>) {
list.clear()
list.addAll(data)
notifyDataSetChanged()
}

fun removeItem(position: Int) {
list.removeAt(position)
notifyItemRemoved(position)
notifyDataSetChanged()
}

class MyHolder(itemView: View) : RecyclerView.ViewHolder(itemView)
}
  • 这个类接收一个布局的id作为参数。可以通过传入两个lambda表达式onBind和itemClick写入我们的逻辑。

  • onBind()有三个参数list、holder、position。通过list可以访问Adapter的数据,通过holder访问View

我们使用得时候就可以这样写。

1
2
3
4
5
6
7
8
9
10
val filmAdapter = MyAdapter<Film>(R.layout.list_film)
filmAdapter.setOnBindListener {
//onBindViewHolder()的逻辑
}

val layoutManager = LinearLayoutManager(context)
layoutManager.orientation = LinearLayoutManager.HORIZONTAL

recyclerView.layoutManager = layoutManager
recyclerView.adapter = filmAdapter

这样无论用多少次列表只需写一个这样的Adapter就行,可以很轻松的构建我们的代码。