如何从 android 中 recyclerview 的适配器位置访问所有图像视图?
How can I access all imageviews from a position of an adapter of recyclerview in android?
在我的程序中,有一个recyclerview。 recyclerview 的每个项目都有一个图像视图。当条件满足时,我需要更改所有项目的图像视图的图像。所以我需要从适配器的某个位置访问整个位置的图像视图。我需要从适配器内部访问。我搜索并从这里找到了答案。但我只能从我们的 fragment/activity 页面访问。不是来自适配器。该代码是
val row: View? = rvLocations?.layoutManager?.findViewByPosition(position)
如何访问适配器内的所有视图?谢谢
这取决于您构建适配器的方式,这是在 kotlin
中使用 viewBinding
的完整适配器样板
class ItemListAdapter(
private val list: List<ItemDataModel>,
private val listener: OnItemClickListener
) : RecyclerView.Adapter<ItemListAdapter.ViewHolder>() {
interface OnItemClickListener{
fun onItemClick(view: View, position: Int)
}
inner class ViewHolder(private var binding: ItemListRcvBinding) : RecyclerView.ViewHolder(binding.root) {
fun bind(item: ItemDataModel, position: Int) {
// you can get access to all your view and its position here
// e.g:
// context
val context = binding.imageView1.context
// clicklistener
binding.imageView1.setOnclickListener{ view ->
listener.onItemClick(view, position)
}
// change image by condition
when(item.condision){
conditionA -> binding.imageView1.setImageResource(R.drawable.image1)
else -> //do something
}
//or
when(position){
0 -> binding.imageView1.setImageResource(R.drawable.image1)
else -> //do something
}
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = ItemListRcvBinding.inflate(LayoutInflater.from(parent.context))
return ViewHolder(view)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val item = getItem(position)
holder.bind(item,position)
}
override fun getItemCount(): Int = list.size
}
在activity/fragment
...
adapter = ItemListAdapter(myList1, object: ItemListAdapter.OnItemClickListener{
override fun onItemClick(view: View, position: Int){
//do something
}
}
...
在我的程序中,有一个recyclerview。 recyclerview 的每个项目都有一个图像视图。当条件满足时,我需要更改所有项目的图像视图的图像。所以我需要从适配器的某个位置访问整个位置的图像视图。我需要从适配器内部访问。我搜索并从这里找到了答案。但我只能从我们的 fragment/activity 页面访问。不是来自适配器。该代码是
val row: View? = rvLocations?.layoutManager?.findViewByPosition(position)
如何访问适配器内的所有视图?谢谢
这取决于您构建适配器的方式,这是在 kotlin
viewBinding
的完整适配器样板
class ItemListAdapter(
private val list: List<ItemDataModel>,
private val listener: OnItemClickListener
) : RecyclerView.Adapter<ItemListAdapter.ViewHolder>() {
interface OnItemClickListener{
fun onItemClick(view: View, position: Int)
}
inner class ViewHolder(private var binding: ItemListRcvBinding) : RecyclerView.ViewHolder(binding.root) {
fun bind(item: ItemDataModel, position: Int) {
// you can get access to all your view and its position here
// e.g:
// context
val context = binding.imageView1.context
// clicklistener
binding.imageView1.setOnclickListener{ view ->
listener.onItemClick(view, position)
}
// change image by condition
when(item.condision){
conditionA -> binding.imageView1.setImageResource(R.drawable.image1)
else -> //do something
}
//or
when(position){
0 -> binding.imageView1.setImageResource(R.drawable.image1)
else -> //do something
}
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = ItemListRcvBinding.inflate(LayoutInflater.from(parent.context))
return ViewHolder(view)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val item = getItem(position)
holder.bind(item,position)
}
override fun getItemCount(): Int = list.size
}
在activity/fragment
...
adapter = ItemListAdapter(myList1, object: ItemListAdapter.OnItemClickListener{
override fun onItemClick(view: View, position: Int){
//do something
}
}
...