使用 RecyclerView 时如何编辑父布局视图?
how to edit view of layout in parent when use RecyclerView?
我为 2 个 recyclerView 使用一个适配器和一种布局。我想在特定的 recyclerView 中编辑布局视图,如何实现?
这是我的适配器
class PictureAdapter(private val onclickToRemove: ((Int) -> Unit?)?,
private val onclickToZoom: ((String) -> Unit?)?
, private val context: Context) : RecyclerView.Adapter<PictureAdapter.MyViewHolder>() {
inner class MyViewHolder(itemView : View) : RecyclerView.ViewHolder(itemView) {
private val picture = itemView.findViewById<RoundedImageView>(R.id.itemPicture)
private val btnRemove = itemView.findViewById<RoundedImageView>(R.id.btnRemovePicture)
private val pictureName = itemView.findViewById<TextView>(R.id.itemPictureName)
fun onBind(item: PictureModel) {
Glide.with(context).load(item.urlPicture).into(picture)
pictureName.text = item.pictureName
btnRemove.setOnClickListener {
onclickToRemove?.invoke(adapterPosition)
}
itemView.setOnClickListener {
onclickToZoom?.invoke(item.urlPicture)
}
}
}
private val differCallback = object : DiffUtil.ItemCallback<PictureModel>(){
override fun areItemsTheSame(oldItem: PictureModel, newItem: PictureModel): Boolean {
return oldItem.urlPicture == newItem.urlPicture
}
override fun areContentsTheSame(oldItem: PictureModel, newItem: PictureModel): Boolean {
return oldItem == newItem
}
}
val differ = AsyncListDiffer(this,differCallback)
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_picture,parent,false)
return MyViewHolder(view)
}
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
val picture = differ.currentList[position]
holder.onBind(picture)
}
override fun getItemCount(): Int {
return differ.currentList.size
}
}
我的英语不好所以我希望你同情我,祝你有一个美好的一天!
您可以通过条件渲染隐藏或显示您的视图。
示例:
val tvName = findViewById<TextView>(R.id.tv_item_user);
tvName.Visibility = View.VISIBLE; // View.GONE, View.INVISIBLE are available too.
假设您从 ActivityA 调用您的适配器,然后您传递一个标志,例如
flag: 1
并且当您从 ActivityB 调用您的适配器时,然后传递 flag: 2
。您从适配器接收标志值并根据标志 1 或 2 呈现您的视图。
例如,您的 layout_item.xml 文件有 2 个 TextView。
一个用于 nameTextView,另一个用于 schoolTextView。当您获得标志值 1 时,您只需显示 nameTextView 并隐藏 schoolTextView。当您获得标志值 2 时,您将显示 schoolTextView 并隐藏 nameTextView。
在你的例子中,你添加了一个带适配器的标志参数 class。当你 call/initialisation 你的适配器时传递标志 no。在 MyViewHolder
class 的 onBind()
函数中,您可以根据自己的需要隐藏和显示视图。
我为 2 个 recyclerView 使用一个适配器和一种布局。我想在特定的 recyclerView 中编辑布局视图,如何实现?
这是我的适配器
class PictureAdapter(private val onclickToRemove: ((Int) -> Unit?)?,
private val onclickToZoom: ((String) -> Unit?)?
, private val context: Context) : RecyclerView.Adapter<PictureAdapter.MyViewHolder>() {
inner class MyViewHolder(itemView : View) : RecyclerView.ViewHolder(itemView) {
private val picture = itemView.findViewById<RoundedImageView>(R.id.itemPicture)
private val btnRemove = itemView.findViewById<RoundedImageView>(R.id.btnRemovePicture)
private val pictureName = itemView.findViewById<TextView>(R.id.itemPictureName)
fun onBind(item: PictureModel) {
Glide.with(context).load(item.urlPicture).into(picture)
pictureName.text = item.pictureName
btnRemove.setOnClickListener {
onclickToRemove?.invoke(adapterPosition)
}
itemView.setOnClickListener {
onclickToZoom?.invoke(item.urlPicture)
}
}
}
private val differCallback = object : DiffUtil.ItemCallback<PictureModel>(){
override fun areItemsTheSame(oldItem: PictureModel, newItem: PictureModel): Boolean {
return oldItem.urlPicture == newItem.urlPicture
}
override fun areContentsTheSame(oldItem: PictureModel, newItem: PictureModel): Boolean {
return oldItem == newItem
}
}
val differ = AsyncListDiffer(this,differCallback)
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_picture,parent,false)
return MyViewHolder(view)
}
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
val picture = differ.currentList[position]
holder.onBind(picture)
}
override fun getItemCount(): Int {
return differ.currentList.size
}
}
我的英语不好所以我希望你同情我,祝你有一个美好的一天!
您可以通过条件渲染隐藏或显示您的视图。 示例:
val tvName = findViewById<TextView>(R.id.tv_item_user);
tvName.Visibility = View.VISIBLE; // View.GONE, View.INVISIBLE are available too.
假设您从 ActivityA 调用您的适配器,然后您传递一个标志,例如
flag: 1
并且当您从 ActivityB 调用您的适配器时,然后传递 flag: 2
。您从适配器接收标志值并根据标志 1 或 2 呈现您的视图。
例如,您的 layout_item.xml 文件有 2 个 TextView。 一个用于 nameTextView,另一个用于 schoolTextView。当您获得标志值 1 时,您只需显示 nameTextView 并隐藏 schoolTextView。当您获得标志值 2 时,您将显示 schoolTextView 并隐藏 nameTextView。
在你的例子中,你添加了一个带适配器的标志参数 class。当你 call/initialisation 你的适配器时传递标志 no。在 MyViewHolder
class 的 onBind()
函数中,您可以根据自己的需要隐藏和显示视图。