如何在 Adapter 中优雅的使用 Context
}
override fun getItemCount(): Int {}
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {}
}
WT,还可以这么操作!!第一次见到这样的写法,有点意思,由此有了这一篇文章。
2. 获取到 Context 的四种方式
=====================
很多时候我们需要在 RecyclerView.Adapter 中使用到 context,比如:利用 Glide 来加载网络图片的时候。
这时,我们该如何拿到 context 给 Glide 呢?
2.1 通过 Adapter 构造函数传入 Context
=============================
这是我之前最常用的一种方式,通过 Adapter 构造函数将当前 Activity Context 传进来,如下:
class MyAdapter(
private val context: Context,
private val dataList: List<MyData>
) : RecyclerView.Adapter<MyAdapter.MyViewHolder>() {
class MyViewHolder(val binding: RcyItemViewBinding) :
RecyclerView.ViewHolder(binding.root) {
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
val binding = RcyItemViewBinding.inflate(
LayoutInflater.from(parent.context),
parent,
false
)
return MyViewHolder(binding)
}
override fun getItemCount(): Int = dataList.size
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
val data = dataList[position]
holder.binding.apply {
Glide.with(context).load(data.imageUrl).into(holder.binding.imageIv)
holder.binding.contentTv.text = data.content
}
}
}
2.2 通过 Parent.context 获取
========================
这个也是文章开头中提到的我的同事的一种写法,如下:
class MyAdapter(private val dataList: List<MyData>) :
RecyclerView.Adapter<MyAdapter.MyViewHolder>() {
private lateinit var mContext: Context
class MyViewHolder(val binding: RcyItemViewBinding) :
RecyclerView.ViewHolder(binding.root) {
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
mContext = parent.context
val binding = RcyItemViewBinding.inflate(
LayoutInflater.from(parent.context),
parent,
false
)
return MyViewHolder(binding)
}
override fun getItemCount(): Int = dataList.size
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
val data = dataList[position]
holder.binding.apply {
Glide.with(mContext).load(data.imageUrl).into(holder.binding.imageIv)
holder.binding.contentTv.text = data.content
}
}
}
2.3 通过 onAttachedToRecyclerView() 方法获取
======================================
对于 2.2 的方法,通过 parent.context 对 mContext 进行赋值,有人说,不可以这么操作!这样会导致内存泄露!!(留个疑问?你觉得 2.2 方法这样操作会导致内存泄漏吗?) 所以你需要覆写 onAttachedToRecyclerView(recyclerView: RecyclerView) 方法,在这里对 mContext 进行赋值。如下:
class MyAdapter(private val dataList: List<MyData>) :
RecyclerView.Adapter<MyAdapter.MyViewHolder>() {
private lateinit var mContext: Context
override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
super.onAttachedToRecyclerView(recyclerView)
mContext = recyclerView.context
}
class MyViewHolder(val binding: RcyItemViewBinding) :
RecyclerView.ViewHolder(binding.root) {
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
val binding = RcyItemViewBinding.inflate(
LayoutInflater.from(parent.context),
parent,
false
)
return MyViewHolder(binding)
}
override fun getItemCount(): Int = dataList.size
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
val data = dataList[position]
holder.binding.apply {
Glide.with(mContext).load(data.imageUrl).into(holder.binding.imageIv)
holder.binding.contentTv.text = data.content
}
}
}
2.4 通过 ImageView 获取 context
===========================
当当就我们举的这个例子,因为我们需要用到 Glide 来展示网络图片,所以我们需要传递 Context 给 Glide,其实我们可以直接通过 ImageView 来拿到 context,然后传给 Glide,如下:
class MyAdapter(private val dataList: List<MyData>) :
RecyclerView.Adapter<MyAdapter.MyViewHolder>() {
class MyViewHolder(val binding: RcyItemViewBinding) :
RecyclerView.ViewHolder(binding.root) {
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
val binding = RcyItemViewBinding.inflate(
LayoutInflater.from(parent.context),
parent,
false
)
return MyViewHolder(binding)
}
override fun getItemCount(): Int = dataList.size
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
val data = dataList[position]
holder.binding.apply {
Glide.with(imageIv.context).load(data.imageUrl).into(holder.binding.imageIv)
holder.binding.contentTv.text = data.content
}
}
}
3. 问题的本质
=========
上面介绍了四种方法来获取 Context,想必大家都想弄清楚上面几种方法有什么区别吧,那我们就打印他们获取到的 context 出来瞧瞧看吧,如下:
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
···
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
Log.e("jctest", "onCreate: this::class.java = ${this::class.java}")
Log.e("jctest", "onCreate: binding.recyclerView.context::class.java = ${binding.recyclerView.context::class.java}")
val adapter = MyAdapter(listData)
binding.recyclerView.adapter = adapter
}
class MyAdapter(private val dataList: List<MyData>) :
RecyclerView.Adapter<MyAdapter.MyViewHolder>() {
class MyViewHolder(val binding: RcyItemViewBinding) :
RecyclerView.ViewHolder(binding.root) {
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
Log.e("jctest", "onCreateViewHolder: parent::class.java = ${parent::class.java}")
Log.e("jctest", "onCreateViewHolder: parent.context::class.java = ${parent.context::class.java}")
val binding = RcyItemViewBinding.inflate(
LayoutInflater.from(parent.context),
parent,
false
)
return MyViewHolder(binding)
}
override fun getItemCount(): Int = dataList.size
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
val data = dataList[position]
holder.binding.apply {
Log.e("jctest", "onBindViewHolder: imageIv.context::class.java = ${imageIv.context::class.java}")
Glide.with(imageIv.context).load(data.imageUrl).into(holder.binding.imageIv)
holder.binding.contentTv.text = data.content
}
}
override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
super.onAttachedToRecyclerView(recyclerView)
Log.e("jctest", "onAttachedToRecyclerView: recyclerView.context::class.java = ${recyclerView.context::class.java}")
}
}
}
打印出来的 Log 如下:
评论