}
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) :
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) :
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) :
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) :
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 如下:
E/jctest: onCreate: this::class.java = class com.jc.test.MainActivity
E/jctest: onCreate: binding.recyclerView.context::class.java = class com.jc.test.MainActivity
E/jctest: onAttachedToRecyclerView: recyclerView.context::class.java = class com.jc.test.MainActivity
E/jctest: onCreateViewHolder: parent::class.java = class androidx.recyclerview.widget.RecyclerView
E/jctest: onCreateViewHolder: parent.context::class.java = class com.jc.test.MainActivity
E/jctest: onBindViewHolder: imageIv.context::class.java = class com.jc.test.MainActivity
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)
![](https://i-blog.csdnimg.cn/blog_migrate/22e6984dd24394565acf159f6b6430e8.jpeg)
文末
面试:如果不准备充分的面试,完全是浪费时间,更是对自己的不负责!
不管怎么样,不论是什么样的大小面试,要想不被面试官虐的不要不要的,只有刷爆面试题题做好全面的准备,当然除了这个还需要在平时把自己的基础打扎实,这样不论面试官怎么样一个知识点里往死里凿,你也能应付如流啊
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》,点击传送门即可获取!
**
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)
![](https://i-blog.csdnimg.cn/blog_migrate/22e6984dd24394565acf159f6b6430e8.jpeg)
文末
面试:如果不准备充分的面试,完全是浪费时间,更是对自己的不负责!
不管怎么样,不论是什么样的大小面试,要想不被面试官虐的不要不要的,只有刷爆面试题题做好全面的准备,当然除了这个还需要在平时把自己的基础打扎实,这样不论面试官怎么样一个知识点里往死里凿,你也能应付如流啊